K
K
Kir Mozor2021-02-12 08:37:36
Python
Kir Mozor, 2021-02-12 08:37:36

Not possible mailing to users in pytelegrambotapi?

def spam_message(message):
    chat_id = message.chat.id
    text = message.text
    
    cursor.execute('SELECT count(*) FROM users')
    kolvo_strok_tuple = cursor.fetchone()
    kolvo_strok = int(kolvo_strok_tuple[0])
    
    cursor.execute('SELECT user_id FROM users')
    vse_user = cursor.fetchall()
    if message.text != None:
        user = 0
        for i in range(kolvo_strok):
            try:
                bot.send_message(vse_user[0][0], f'{text}')
                vse_user.pop(0)
                user += 1
            except telebot.apihelper.ApiException:
                pass

In general, there is a simple code written in python. It pulls from the sqlite database the id of users who have registered in the bot. But the script breaks as soon as it stumbles upon the user who blocked the bot. The question is how to bypass the blocked user and continue sending.
Nothing comes to mind. Please help me with this easy question
By the way, the error itself is:
ERROR - TeleBot: "A request to the Telegram API was unsuccessful. Error code: 403. Description: Forbidden: bot was blocked by the user"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2021-02-12
@150_Kirill_150

Throw out kolvo_strok and iterate over vse_user, then pop is not needed and your except will work fine. Well, you don’t need to get into bots, and especially mailing lists, with such knowledge.

A
Alex K, 2021-02-12
@alexk111

If python is not a mandatory requirement, then it may be faster, easier and more visual to make such a mailer in Botodrome, for example. I can share the finished mailer stream in Telegram, if necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question