skip to Main Content

Since 2 days I’ve been exploring the Telegram bot API, which is pretty neat. But there is one thing I can’t figure out.

When you don’t use the webHook but the /getUpdates call, you can tell the API via the offset parameter which message where processed by the server.

But how do you do this with the webHook in place? I keep getting the same message as an update. Which results in the server spamming the user with the same message.

The solution I came up with is as follows:

  1. Receive an update from the webhook
  2. Save the update_id
  3. Reply to the user /sendMessage
  4. disable the webHook /setWebhook?url=
  5. Set the offset /getUpdates?offset={update_id+1}
  6. Reinstate the webHook /setWebhook?url=https://mywebhook.domain.com

There must be a better way right? Anyone?

4

Answers


  1. Chosen as BEST ANSWER

    Ok, problem solved. It appeared that just a 200 (OK) wasn't enough (the body of my response was null. I've added a body to the response {}, and know it works fine.


  2. What HTTP status code are you returning on the page handling your webhook? It is possible that Telegram is attempting to retry your webhook endpoint because it’s not receiving a status 200 (OK) from you.

    Login or Signup to reply.
  3. use This on webHook to get data from telegram servers:

    // get the raw POST data
    $rawData = file_get_contents("php://input");
    
    // this returns null if not valid json
    $jsonData = json_decode($rawData);
    
    Login or Signup to reply.
  4. You must say to telegram that you get updates successfully with this:

     - 200 response code
     &
     - empty json like this {}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search