如何让我的 python 电报机器人每天在特定时间发送消息? [英] how to make my python telegram bot to send message at certain time every day?
本文介绍了如何让我的 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
这篇关于如何让我的 python 电报机器人每天在特定时间发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文