Reputation: 41
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
@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
Upvotes: 4
Views: 10584
Reputation: 371
You could use JobQueue from class telegram.ext
It has a function called run_daily.
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
Upvotes: 4
Reputation: 6202
For regularly scheduling batch tasks, you should you the system built-ins:
On Windows, it's the Task Scheduler, formerly a command called "at":
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. 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:
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 \\computername time /interactive | /every:date,... /next:date,... command
at \\computername id /delete | /delete/yes
On Linux, it's "cron":
https://opensource.com/article/17/11/how-use-cron-linux
Upvotes: 1