如何让我的 python 电报机器人每天在特定时间发送消息?
问题描述:
我正在尝试制作一个能够每天在特定时间通知用户的机器人.如何让机器人每天在特定时间发送通知?
I'm trying to make a bot which will be able to notify users at a certain time every day. how can I make bot to send notification at certain time every day?
我尝试使用 while 循环,但它 s
I've tried to use while loop but it s
@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
我不知道如何解决我的问题
i don't have any idea of how can i solve my problem
答
你可以使用 JobQueue 来自类 telegram.ext
You could use JobQueue from class telegram.ext
它有一个名为 run_daily 的函数.
It has a function called 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