清除Telegram Bot中的"​​pending_update_count"

Ham*_*ava 9 webhooks telegram telegram-bot telegram-webhook

我想pending_update_count在我的机器人中清除所有内容!

以下命令的输出:

https://api.telegram.org/botxxxxxxxxxxxxxxxx/getWebhookInfo
Run Code Online (Sandbox Code Playgroud)

显然我用xxx替换了真正的API令牌

这是 :

{
 "ok":true,"result":
    {
     "url":"",
     "has_custom_certificate":false,
     "pending_update_count":5154
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我5154现在有未读更新!(我很确定这个挂起的更新是错误的!因为没有人使用这个Bot!它只是一个测试Bot)

顺便说一下,这个pending_update_count数字正在快速增长!现在我正在写这篇文章,这个数字增加了51,达到了5205!

我只想清除这些待定更新.我很确定这个Bot已陷入无限循环!

有没有办法摆脱它?

PS:

我也清除了webhook网址.但没有改变!

更新:

输出getWebhookInfo是这样的:

{
   "ok":true,
   "result":{
      "url":"https://somewhere.com/telegram/webhook",
      "has_custom_certificate":false,
      "pending_update_count":23,
      "last_error_date":1482910173,
      "last_error_message":"Wrong response from the webhook: 500 Internal Server Error",
      "max_connections":40
   }
}
Run Code Online (Sandbox Code Playgroud)

为什么我得到Wrong response from the webhook: 500 Internal Server Error

mcm*_*m69 14

对于 2020 年及以后关注此问题的任何人来说,根据API 文档, Telegram API 现在支持通过和drop_pending_updates中的参数清除待处理消息。setWebhookdeleteWebhook

  • 尝试点击这样的 URL:https://api.telegram.org/botYOURTOKEN/setWebhook?url=https://yoururl.com&drop_pending_updates=true (4认同)

Dan*_*sev 6

我想你有两个选择:

  1. 设置无所事事的webhook,只需对电报的服务器说200 OK即可.电报将发送所有更新到该URL,并清除该队列.
  2. 禁用webhook并在使用getUpdates方法获取更新后,再次打开webhook

更新:

webhook的问题在你身边.您可以尝试在URL上模拟电报的POST查询.它可以是这样的:

{"message_id":1,"from":{"id":1,"first_name":"FirstName","last_name":"LastName","username":"username"},"chat":{"id":1,"first_name":"FirstName","last_name":"LastName","username":"username","type":"private"},"date":1460957457,"text":"test message"}
Run Code Online (Sandbox Code Playgroud)

例如,您可以使用PostMan将此文本作为POST查询主体发送,然后尝试调试后端.

  • 方法 2 对我不起作用,我不知道我可能做错了什么,因为我使用 getupdates 获取信息,但再次打开 webhook 并不能解决问题。方法1我不知道如何实现,是设置webhook的问题,还是哪里? (2认同)
  • @Danil Pyatnitsev,我也面临着同样的问题......你能给我们方法1的简单例子吗?非常感谢你的帮助... (2认同)