-1

I am writing my bot on telebot. If I quickly press the button several times, the bot will respond with a bunch of messages. Moreover, to fix this, you need to restart the bot. How can I fix this?

Example: enter image description here

Example code:

bot = telebot.TeleBot(token, skip_pending=True)

def start(message):
    button = types.ReplyKeyboardMarkup(resize_keyboard=True)
    kbot = types.KeyboardButton("Погода на сегодня"), types.KeyboardButton("Погода на завтра")
    button.add(kbot)
    msg = bot.send_message(message.chat.id, 'Добро пожаловать!', reply_markup=button)
    bot.register_next_step_hundler(msg, vote)
    
def vote(message):
    if message.text == 'Погода на сегодня':
        pogoda_segodnya(message)
    elif message.text == 'Погода на завтра':
        pogoda_na_zavtra(message)

def pogoda_segodnya(message):
    button = types.ReplyKeyboardMarkup(resize_keyboard=True)
    kbot = types.KeyboardButton("Назад")
    button.add(kbot)
    msg = bot.send_message(message.chat.id, 'Погода сегодня -230', reply_markup=button)
    bot.register_next_step_hundler(msg, nazad)
    
def nazad(message):
    if message.text == 'Назад':
        start(message)
        

if __name__ == '__main__':
    while True:
        try:
            bot.polling(none_stop=True)
        except Exception as e:
            time.sleep(3)
            print(e)

Used

skip_pending=True 
  • does not work.

1 Answers1

0

You're using the wrong way of polling.

Instead off setting bot.polling(none_stop=True) inside a while True:, use infinity_polling without a while loop:

if __name__ == '__main__':
    bot.infinity_polling()

Take a look at these examples for more info.

0stone0
  • 34,288
  • 4
  • 39
  • 64