use*_*643 0 python-3.x telegram python-telegram-bot telegram-bot
给出下面的代码:
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
Run Code Online (Sandbox Code Playgroud)
是否可以定期调用此函数并使我的机器人自动将消息发送给用户,而不是用户键入“/start”
小智 6
you need to create a job object which is delivered by python-telegram-bot
so to simply run the function start let's say every minute you could use this approach:
j= updater.job_queue
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
j.run_repeating(start,interval = 60 ,first= 0 )
updater.start_polling()
Run Code Online (Sandbox Code Playgroud)
and if you wanted to run it every day at specific time you could use:
import datetime
j= updater.job_queue
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
t= datetime.time(6, 15, 00, 000000)
j.run_daily(start, t, days=(0, 1, 2, 3, 4, 5, 6), context=None, name=None)
updater.start_polling()
Run Code Online (Sandbox Code Playgroud)
note that there's no handlers to be added to the dispatcher.
and you probably should know that the datetime.time object uses the UTC time if not modified.
有关更多信息,请在此处查看扩展 – JobQueue