Home>

Have a nice day, morning, night.

I am developing a telegram bot using the telebot library. I ran into a problem where the bot does not send mailing to all users if one of them stopped the bot.

I have a database (text file with ID), to which users are added after starting the bot, and they are written without repetitions, everything is okay with that.

@ bot.message_handler (commands= ['start'])
def welcome (message):
    if not message.chat.id in joinUser:
        join= open ("join.txt", "a")
        join.write (str (message.chat.id) + "\ n")
        joinUser.add (message.chat.id)
        join.close ()

After that, I want to send a mailing list to all subscribers, open the file with the ID and send a message to everyone in order from this file, and then a problem arises, if a person has unsubscribed, then his ID from the database is not deleted and the bot hangs over the sending of this message and does not sends it to anyone.

bot.message_handler (commands= ["rs"])
def mess (message):
    if ADMIN== message.chat.id:
        for user in joinUser:
            bot.send_message (user, message.text [message.text.find (""):])

In this regard, the question is, how can you check that the user has stopped the bot and remove it from the database?

  • Answer # 1

    I recommend using mysql for this. In general, when a person unsubscribes from the bot, you receive an update, which you can catch and remove the bot from your "database"