python-3.x - 使用机器人定期发送电报消息
问题描述
给出下面的代码:
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
是否可以定期调用此函数并让我的机器人自动将消息发送给用户,而不是用户键入“/start”
解决方案
您需要创建一个job
由它提供的对象,python-telegram-bot
以便简单地运行该函数,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 here
推荐阅读
- javascript - 如何共享一个对话框窗口以跨多个子组件加载不同的数据
- css - 为什么我尝试覆盖引导导航链接类在 next.js 中不起作用?
- python - Corey Schafer Django(第 3 部分):HTML 格式不正确
- c++ - 给定 N 作为输入,有没有一种方法可以在没有除法和没有三角的情况下生成常规 N 边形的角
- javascript - 尝试通过 json 文件在反应中导入图像
- javascript - 如何按每个数组项中具有不同名称的单个属性进行排序?
- javascript - 无法通过动态导入访问对象属性。Vue.js + ts
- jboss - 无论部署扫描器中设置的默认超时如何,JBoss AS 7.1 服务器在启动过程中都无法部署 ear 应用程序
- python - 使用 R 数据表的 .SDcols 样式的 pydatatable 更新或添加多列
- javascript - 使用带有选项元素的表单生成自定义 iframe