[英]how to make my python telegram bot to send message at certain time every day?
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?
如何让机器人每天在特定时间发送通知?
I've tried to use while loop but it s我尝试使用 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
i don't have any idea of how can i solve my problem我不知道如何解决我的问题
You could use JobQueue from class telegram.ext您可以使用class telegram.ext中的 JobQueue
It has a function called run_daily.它有一个名为 run_daily 的 function。
run_daily(callback, time, days=(0, 1, 2, 3, 4, 5, 6), context=None, name=None)
Here is an example:这是一个例子:
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))
This run_daily function calls the callback_alarm function daily at 10:10:10 AM此run_daily function 每天上午10:10:10 调用callback_alarm function
For regularly scheduling batch tasks, you should you the system built-ins:对于定期调度批处理任务,您应该系统内置:
On Windows, it's the Task Scheduler, formerly a command called "at":在 Windows 上,它是任务计划程序,以前称为“at”命令:
https://www.windowscentral.com/how-create-task-using-task-scheduler-command-prompt https://www.windowscentral.com/how-create-task-using-task-scheduler-command-prompt
On Windows 10, Task Scheduler is a tool that allows you to create and run virtually any task automatically.
在 Windows 10 上,任务计划程序是一种工具,可让您自动创建和运行几乎任何任务。 Typically, the system and certain apps use the scheduler to automate maintenance tasks (such as disk defragmentation, disk cleanup, and updates), but anyone can use it.
通常,系统和某些应用程序使用调度程序来自动执行维护任务(例如磁盘碎片整理、磁盘清理和更新),但任何人都可以使用它。 With this experience, you can start applications, run commands, and execute scripts at a particular day and time, or you can also trigger tasks when a specific event occurs.
有了这种体验,您可以在特定的日期和时间启动应用程序、运行命令和执行脚本,也可以在特定事件发生时触发任务。
Task Scheduler works by keeping tabs of the time and events on your computer and executes the task as soon as the condition is met.
任务计划程序通过密切关注计算机上的时间和事件来工作,并在满足条件后立即执行任务。
Whether you're trying to use the Task Scheduler to run a task at a specific time or when an event occurs, you can create a task in at least two different ways using the basic and advanced settings.
无论您是尝试使用任务计划程序在特定时间或事件发生时运行任务,您都可以使用基本设置和高级设置以至少两种不同的方式创建任务。
Related to this is the "at" command:与此相关的是“at”命令:
https://support.microsoft.com/en-us/help/313565/how-to-use-the-at-command-to-schedule-tasks https://support.microsoft.com/en-us/help/313565/how-to-use-the-at-command-to-schedule-tasks
The at command uses the following syntax:
at 命令使用以下语法:
at \\computername time /interactive | /every:date,... /next:date,... command
at \\computername id /delete | /delete/yes
On Linux, it's "cron":在 Linux 上,它是“cron”:
https://opensource.com/article/17/11/how-use-cron-linux https://opensource.com/article/17/11/how-use-cron-linux
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.