python - 如何让我的 python 电报机器人每天在特定时间发送消息?
问题描述
我正在尝试制作一个能够在每天特定时间通知用户的机器人。如何让机器人每天在特定时间发送通知?
我尝试使用 while 循环,但它是
@bot.callback_query_handler(func=lambda c:True)
def CalendarAnswer(c):
Cid = c.message.chat.id
if c.data == 'ShowTime':
bot.send_message(Cid, timeToday)
if c.data == 'ShowDate':
bot.send_message(Cid, dateToday)
if c.data == 'SetNotification':
Ask = bot.send_message(Cid, 'Напиши мне время')
bot.register_next_step_handler(Ask,SettingNotificationTime)
def SettingNotificationTime(message):
NotificationTime = message.text
bot.send_message(message.chat.id, "that's your time:" + NotificationTime)v
我不知道如何解决我的问题
解决方案
您可以使用类 telegram.ext中的JobQueue
它有一个名为 run_daily 的函数。
run_daily(callback, time, days=(0, 1, 2, 3, 4, 5, 6), context=None, name=None)
这是一个例子:
def callback_alarm(context: telegram.ext.CallbackContext):
bot.send_message(chat_id=id, text='Hi, This is a daily reminder')
def reminder(update,context):
bot.send_message(chat_id = update.effective_chat.id , text='Daily reminder has been set! You\'ll get notified at 8 AM daily')
context.job_queue.run_daily(callback_alarm, context=update.message.chat_id,days=(0, 1, 2, 3, 4, 5, 6),time = time(hour = 10, minute = 10, second = 10))
此run_daily函数每天上午10:10:10调用callback_alarm函数
推荐阅读
- java - 为什么 Java 上默认的 MaxMetaspaceSize 非常大?
- java - 使用 Java 为移动应用程序创建 Paypal 钱包
- php - 使用 PHP 中的 curl 连接到在证书包中显示过期根证书的站点
- spring - 无法打开类路径资源 [application.properties],因为它不存在。Application.properties 不可访问
- javascript - 如何在此代码中获取 innerHTML 属性值“r”?
- node.js - 如何关闭winston的日志以开玩笑
- kubernetes - 如何在 Kubernates 部署中为日志定义卷挂载并通过公共 IP 访问服务?
- javascript - 如何在移动分辨率下更改图像下方的标题位置 - Fancybox 3
- python - Maya – 如何按字母顺序重新排列货架?
- scala - 无法在 Spark SQL 中进行 DB2 COALESCE