使用机器人定期发送电报消息
Send telegram message periodically using bot
给出下面的代码:
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
是否可以定期调用此函数并让我的机器人自动向用户发送消息而不是用户键入“/start”
也许 helps with your question. Secondly what I can recommend you is to have a look at Flask and Node-RED. From this tutorial,我想你会明白如何轻松地创建你的结构。
您需要创建一个由 python-telegram-bot
交付的 job
对象
所以简单地 运行 函数 start
让我们说每一分钟你都可以使用这种方法:
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()
如果你想每天在特定时间运行它,你可以使用:
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()
请注意,没有要添加到调度程序的处理程序。
您可能应该知道 datetime.time
对象如果未修改则使用 UTC 时间。
有关详细信息,请在此处查看 Extensions – JobQueue
给出下面的代码:
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
是否可以定期调用此函数并让我的机器人自动向用户发送消息而不是用户键入“/start”
也许
您需要创建一个由 python-telegram-bot
交付的 job
对象
所以简单地 运行 函数 start
让我们说每一分钟你都可以使用这种方法:
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()
如果你想每天在特定时间运行它,你可以使用:
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()
请注意,没有要添加到调度程序的处理程序。
您可能应该知道 datetime.time
对象如果未修改则使用 UTC 时间。
有关详细信息,请在此处查看 Extensions – JobQueue