A
A
Alexander2017-02-05 00:00:54
Python
Alexander, 2017-02-05 00:00:54

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

3 answer(s)
E
Error4uk, 2019-04-16
@Error4uk

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)

N
nllm, 2017-02-05
@nllm

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".

D
danis_2014, 2017-02-05
@danis_2014

You can use time.sleep(seconds). For example:

import time
while True:
    send_message()  # типа отправляешь сообщение
    time.sleep(60)  # останавливает выполнение кода на минуту

But it's better to use the datetime module:
import datetime
datetime.datetime.now().time()  # вернет настоящее время

And already by the conclusion of the time to do checks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question