I
I
Irina2019-05-25 22:40:55
Python
Irina, 2019-05-25 22:40:55

Why doesn't telegram bot work when using schedule?

Help me understand what is the problem? To send messages on a schedule, I want to use the schedule module. The code looks something like the one below. With this launch, scheduled sending works, but the handlers themselves stop working. For example @bot.message_handler(commands=['info']) doesn't respond. What needs to be changed in the code? I guess this part needs to be changed:

while True:
    schedule.run_pending()
    time.sleep(1)

But what to change?
import schedule
import time

def job():
    print("I'm working...")

schedule.every(1).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

@bot.message_handler(commands=['info'])
def handle_text(message):
    answer = 'БЛА-БЛА-БЛА.'
    bot.send_message(message.chat.id, answer)

if __name__ == '__main__':
    bot.polling(none_stop=True, interval=0)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Irina, 2019-05-27
@vera_ira

Here's an option that works. Multithreading and the threading module helped.

import schedule
import time
import threading

def job():
    print("I'm working...")

schedule.every(1).minutes.do(job)

def go():
    while 1:
        schedule.run_pending()
        time.sleep(1)

t = threading.Thread(target=go, name="тест")
t.start()

@bot.message_handler(commands=['info'])
def handle_text(message):
    answer = 'БЛА-БЛА-БЛА.'
    bot.send_message(message.chat.id, answer)

if __name__ == '__main__':
    bot.polling(none_stop=True, interval=0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question