简体   繁体   English

如何使用 PyTelegrambotAPI 实现倒数计时器?

[英]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.对 Telegram API 的请求未成功。 The server returned HTTP 400 Bad Request.服务器返回 HTTP 400 错误请求。 Response body:error_code:400,description:"Bad Request: message is not modified响应正文:error_code:400,描述:“错误请求:消息未修改

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 (eg 2 seconds) for updating the messages to give the Telegram Servers enough time to realize that the message has been updated.使用更大的间隔(例如 2 秒)来更新消息,以便给 Telegram 服务器足够的时间来意识到消息已被更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM