如何在 Telegram Bot Web 服务器关闭时获取未传送的消息?

How to get messages that were not delivered while Telegram Bot web-server was down?

很好奇,如果有方法可以避免在接受 Webhooks 的网络服务器关闭(由于重新部署、故障或维护)时跳过从 Telegram Bot 发送的消息。

当您使用轮询时 - Telegram API 从上次检索开始发送消息,不会跳过任何消息。

但是Webhooks怎么用呢?使用轮询还是有一些特殊的机制?

我最近遇到了同样的问题,但我只是通过在服务器启动时将启动时间保存到一个变量然后使用 Telegrambot.Message.date 并比较时间是否在服务器启动时间之前发送或没有。

Telegram 会将收到的消息保留 24 小时,如果您的 Webhook 已关闭(即重新部署),那么消息将在再次在线后发送。

它可以在 Heroku 上运行,例如当您的 Dyno 出现故障时:一旦它启动,Chatbot 将再次向 Telegram 注册并接收队列中仍然可用的消息。

There are two mutually exclusive ways of receiving updates for your bot — the getUpdates method on one hand and Webhooks on the other. Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.

有关详细信息,请参阅电报 documentation