Answer the question
In order to leave comments, you need to log in
How to make a bot automatically send messages at a given time?
Beginner
Making the first telegram bot for experience. You need to make him send messages in the morning and in the evening.
While I get out through while which is launched after the / start command, but I'm sure that there are easier ways.
Answer the question
In order to leave comments, you need to log in
via schedule
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
The easiest option.
Write the date, time, user id and message text to the database. For each message there is a status: new, sending, sent.
Run a script every minute (or other period of time) using cron. At startup, look at which users it is already possible / time to send messages.
If it's time, then we make a mark in the database "sending in progress" and start sending messages.
After sending, we write to the database "sent".
You can use time.sleep(seconds). For example:
import time
while True:
send_message() # типа отправляешь сообщение
time.sleep(60) # останавливает выполнение кода на минуту
import datetime
datetime.datetime.now().time() # вернет настоящее время
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question