K
K
Kreichik2021-09-28 18:28:00
Python
Kreichik, 2021-09-28 18:28:00

How to make sure that the bot's work is not interrupted?

I am writing a bot that should remind the user about training on certain days. I use Shedule and everything seems to be working, but I need other functions to be performed in addition to the reminder. And since I'm using a while True loop, I can't continue to perform actions. Code below

import telebot
import sqlite3
import schedule
import time

bot = telebot.TeleBot("TOKEN")

def reminder(message):
    people_id = message.chat.id
    connect = sqlite3.connect('users.db')
    cursor = connect.cursor()
    cursor.execute(f"SELECT time FROM info_user WHERE id = {people_id}")
    time_watch = str(cursor.fetchone()[0])
    def remind():
        bot.send_message(people_id, f'Напоминание!!!\nВремя {time_watch}')
    schedule.every().day.at(time_watch).do(remind)


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

bot.polling()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-09-28
@Kreichik

You have two eternal loops - one is bot.polling(), the other contains schedule.run_pending(), and they will definitely conflict.
I don't quite understand why your bot works at all - it shouldn't, if only because reminder() isn't called anywhere, and isn't decorated with anything. Unless you show something.
There are two options to make friends humanly.
1. push one of the loops (preferably schedule) into a separate thread
2. use an asynchronous library like pyrogram, and replace time.sleep() with await asyncio.sleep().

S
s19s93, 2021-09-28
@s19s93

I would
add bot.polling(none_stop=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question