简体   繁体   English

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

[英]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 AMrun_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.

相关问题 用 Python 编写 Discord 机器人 - 如何让它每天在特定时间发送消息? - Programming a Discord bot in Python- How do I make it send a message every day at a certain time? 如何安排 python 电报机器人在特定时间/之后发送消息? - How do I schedule python telegram bot to send message at/after certain time? 如何通过python电报机器人库在固定时间或间隔时间从机器人向用户发送消息? - How to send message from bot to user at a fixed time or at intervals through python telegram bot library? 如何使用 Python 在 Telegram 中从我的姓名(我的帐户,而不是机器人)发送消息? - How to send a message from my name (my account, not the bot) in Telegram using Python? 电报机器人 Api / Python:试图通过我的电报机器人发送语音消息 - Telegram Bot Api / Python: Trying to send voice message via my telegram bot 如何在ReplyKeyboardMarkup中按下按钮以不发送消息(python-telegram-bot) - How to make a press on a button in ReplyKeyboardMarkup not to send a message (python-telegram-bot) 如何让 Python-Telegram_bot 在没有逗号的情况下发送消息? - How to make Python-Telegram_bot send a message without getting a commad? 如果Python脚本异常,如何从Telegram机器人发送消息? - How to send a message from a Telegram bot in case of python script exception? 如何使用 python-telegram-bot 发送消息 - How to send message using python-telegram-bot 如何在 Python 中的电报发送消息机器人中使用解析模式 - How to use parse mode in telegram send message bot in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM