且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何让我的 python 电报机器人每天在特定时间发送消息?

更新时间:2023-11-19 12:30:16

你可以使用 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