Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear "pending_update_count" in Telegram Bot

I want to clear all pending_update_count in my bot!

The output of below command :

https://api.telegram.org/botxxxxxxxxxxxxxxxx/getWebhookInfo

Obviously I replaced the real API token with xxx

is this :

{
 "ok":true,"result":
    {
     "url":"",
     "has_custom_certificate":false,
     "pending_update_count":5154
    }
}

As you can see, I have 5154 unread updates til now!! ( I'm pretty sure this pending updates are errors! Because no one uses this Bot! It's just a test Bot)

By the way, this pending_update_count number are increasing so fast! Now that I'm writing this post the number increased 51 and reached to 5205 !

I just want to clear this pending updates. I'm pretty sure this Bot have been stuck in an infinite loop!

Is there any way to get rid of it?

P.S:

I also cleared the webhook url. But nothing changed!

UPDATE:

The output of getWebhookInfo is this :

{
   "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
   }
}

Why I get Wrong response from the webhook: 500 Internal Server Error ?

like image 448
Hamed Kamrava Avatar asked Dec 27 '16 16:12

Hamed Kamrava


People also ask

How do I delete Webhook Telegram?

The method simply makes a call to setWebhook using empty url param. This tells Telegram to remove the webhook (If there's one). You need not specify any params. The helper method will take care for you.

Can Telegram bots see messages?

All bots, regardless of settings, will receive: All service messages. All messages from private chats with users.


2 Answers

I think you have two options:

  1. set webhook that do nothing, just say 200 OK to telegram's servers. Telegram wiil send all updates to this url and the queque will be cleared.
  2. disable webhook and after it get updates by using getUpdates method, after it, turn on webhook again

Update:

Problem with webhook on your side. You can try to emulate telegram's POST query on your URL. It can be something like this:

{"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"}

You can send this text as a POST query body with PostMan for example, and after it try to debug your backend.

like image 195
Danil Pyatnitsev Avatar answered Sep 24 '22 22:09

Danil Pyatnitsev


For anyone looking at this in 2020 and beyond, the Telegram API now supports clearing the pending messages via a drop_pending_updates parameter in both setWebhook and deleteWebhook, as per the API documentation.

like image 42
mcm69 Avatar answered Sep 24 '22 22:09

mcm69