Clear "pending_update_count" in Telegram Bot
Asked Answered
M

8

19

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 ?

Modestomodesty answered 27/12, 2016 at 16:41 Comment(3)
is your bot echoing messages? that's the only reason i can think ofFertile
what prevents you to get this updates and clear counter?Crosstie
Please see the updatesModestomodesty
Z
12

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.

Zeralda answered 28/12, 2016 at 3:45 Comment(2)
Method 2 didnt work for me, I don't know what could i Possibly be doing wrong, as I'm getting the info using getupdates, but turning the webhook on again doesn't fix it. Method 1 I'm not sure how to implement it, is the problem setting the webhook, or where?Capriccio
@Danil Pyatnitsev, i also facing the same problem ... can you give us simple example for method 1 ? greatly appreciate your help ...Obryan
N
18

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.

Naseberry answered 24/11, 2020 at 9:2 Comment(1)
try to hit the URL like this: api.telegram.org/botYOURTOKEN/setWebhook?url=https://…Fatwitted
Z
12

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.

Zeralda answered 28/12, 2016 at 3:45 Comment(2)
Method 2 didnt work for me, I don't know what could i Possibly be doing wrong, as I'm getting the info using getupdates, but turning the webhook on again doesn't fix it. Method 1 I'm not sure how to implement it, is the problem setting the webhook, or where?Capriccio
@Danil Pyatnitsev, i also facing the same problem ... can you give us simple example for method 1 ? greatly appreciate your help ...Obryan
W
4

Just add return 1; at the end of your hook method.

Update:

Commonly this happens because of queries delay with the database.

Washcloth answered 10/4, 2017 at 8:41 Comment(6)
hi there, i also facing the same problem ... can u give us example for this ?Obryan
@Obryan in your function that call by telegram, add only a line of code like return 1; and send something to bot after do.Washcloth
something like this ? public function index() { $content = file_get_contents("php://input"); $update = json_decode($content, true); $chatID = "12345678"; $reply = "ok"; return 1; $API_URL = "https://api.telegram.org/botTOKEN/"; $sendto =$API_URL."sendmessage?chat_id=".$chatID."&text=".$reply; file_get_contents($sendto); }Obryan
@Obryan Yes! But if you just want to reset pending count do Like this: ```public function(){return 1;}Washcloth
@Obryan It was clear! you must! every name that you want.Washcloth
im sorry, but it didnt work, i used to do the same thing like you do, and it usually work, but its not working anymore... i even try to unset the webhook and set it again ...Obryan
J
4

I solved is like this

POST tg.api/bottoken/setWebhook to emtpy "url"
POST tg.api/bottoken/getUpdates
POST tg.api/bottoken/getUpdates with "offset" last update_id appeared before

doing this serveral times

POST tg.api/bottoken/getWebhookInfo

had a look if all away.

POST tg.api/bottoken/setWebhook with filled "url"
Junction answered 10/1, 2019 at 22:41 Comment(0)
P
4

If you are using webhook, you can follow these steps

  1. On your web browser, enter the following url with your right value of bot

    https://api.telegram.org/bot/getWebhookInf

  2. You will get a result like this on your screen

    {"ok":true,"result":{"url":"url_value",...}}

  3. On the displayed result, copy the entire url_value without quotes and replace it on this second url https://api.telegram.org/bot/setWebhook?url=url_value&drop_pending_updates=True

  4. Enter the second url with right bot and url_value in your web browser then press ENTER

    Done!

Pasta answered 12/9, 2022 at 12:50 Comment(0)
A
0

i solve it by Change file access permissions file - set permissions file to 755

and second increase memory limit in php.ini file

Adkins answered 22/2, 2020 at 10:17 Comment(0)
F
0

A quick&dirty way is to get a temporary webhook here: https://webhook.site/ and set your webhook to that (it will answer with a HTTP/200 code everytime, reseting your pending messages to zero)

Foxy answered 1/2, 2021 at 15:35 Comment(0)
B
0

I faced the same issue for my tele bot after user edited existing message. My bot receives update with editedMessage continuously, but update.hasMessage() was empty. As a result number of updates rocketly increased and my bot stack.

I solved this issue by adding handling for use case when message is missing - send 200 code:

    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {

        update = MAPPER.readValue(event.getBody(), Update.class);

        if (!update.hasMessage()) {
            return new APIGatewayProxyResponseEvent()
                    .withStatusCode(200) // -> !!!!!! return code 200
                    .withBody("message is missing")
                    .withIsBase64Encoded(false);
        }
        ... ... ...
Baring answered 16/4, 2022 at 16:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.