Ilya
Ilya

Reputation: 25

How to implement countdown timer with PyTelegrambotAPI?

I'm trying to implement something "looks like" animated countdown in my telegram bot:

sent=bot.send_message(message.chat.id,'5')
time.sleep(1)
bot.edit_message_text('4',message.chat.id,sent.message_id)
time.sleep(1)
bot.edit_message_text('3',message.chat.id,sent.message_id)
time.sleep(1)
bot.edit_message_text('2',message.chat.id,sent.message_id)
time.sleep(1)
bot.edit_message_text('1',message.chat.id,sent.message_id)
time.sleep(1)
bot.edit_message_text('0',message.chat.id,sent.message_id)

Sometimes it works well, but sometimes I get the error:

A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:error_code:400,description:"Bad Request: message is not modified

Upvotes: 1

Views: 2373

Answers (1)

Jonas Fowl
Jonas Fowl

Reputation: 115

I personally don't recommend you to send requests to telegram so often in a small timespan. You might end up with a time out for too many requests. My two suggestions for you here:

  1. Just skip any update that failed. A retry will simply take too long
  2. Use a bigger interval (e.g. 2 seconds) for updating the messages to give the Telegram Servers enough time to realize that the message has been updated.

Upvotes: 0

Related Questions