H
H
h1nlav2020-05-01 13:46:09
Python
h1nlav, 2020-05-01 13:46:09

telebot. Python. How to protect yourself from spam?

The essence of the problem is that when you press the same button very quickly several times, the menu splits into two, because the handler receives two messages instead of the planned one.
I am writing a conditional code on which an error occurs, since there are a lot of lines in the original.

def menu_show (message):
    # В оригинале тут работа с БД

    markup = types.ReplyKeyboardMarkup(True, one_time_keyboard = True)
    markup.row("menu")
    
    bot.send_message(message.chat.id, "*Меню*", reply_markup=markup)
    bot.register_next_step_handler(message, menu_processing)

def menu_processing(message):
    if message.text == "menu":
        menu_show(message)
    else:
        bot.send_message(message.chat.id, "Неправильный ввод, нажми на кнопку.")
        bot.register_next_step_handler(message, menu_show)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey_Dolg, 2020-05-02
@Andrey_Dolg

I do not quite understand. It seems like the buttons do hold for the duration of the processing themselves.
But if not, in theory there is an id property in the message, you can globally (through the database or something else) I think it’s a good job for redis or sqlite in memory to keep those id that are being processed and release them only after the response is sent, as a consequence of the condition on handler input.

if message_id not in processing_ids:
    ...

Upd
Oh yes, for completeness, it is also desirable to check the user 's from_user field like.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question