我希望我的电报机器人在准确的时间发送消息,但当我运行我的代码消息立即发送,没有错误弹出



这是我试图使用调度器的代码部分

scheduler = BackgroundScheduler(timezone="Europe/Istanbul")
@bot.message_handler(content_types=['text', ])
def get_name(message):
keyboard = types.InlineKeyboardMarkup()
cont = types.InlineKeyboardButton(text='re.README', callback_data='yes3')
keyboard.add(cont)
current_user = message.chat.username
res = engine.execute(f"SELECT COUNT(*) FROM members WHERE username = '{current_user}'").fetchall()[0][0]
if res == 0:
engine.execute(f'''INSERT INTO members (username, user_id, name, score) VALUES ('{current_user}', '{message.chat.id}', '{message.text}', '{0}');''')
bot.send_message(message.chat.id, 're.README')
@bot.message_handler(content_types=['text',])
def prom(message):
bot.send_message(message.chat.id, 'gyuk')
scheduler.add_job(prom, 'date', run_date=datetime(2023, 1, 20, 14, 30))
if __name__ == '__main__':
try:
scheduler.start()
bot.polling(none_stop=True)
while True:
sleep(1)
except:
pass

我从这里尝试了不同的建议,但仍然不起作用

在这种情况下,bot不知道您想要发送消息的chat.id参数,因为没有消息发送给他。

如果你想要安排消息的发送,你必须遍历数据库中正确激活bot的人,并从中选择聊天。Id参数,以便bot知道发送消息的聊天对象。

此外,被调度的函数不必是handler,否则bot将等待消息来激活该函数,而不是调度程序。

最新更新