EMD
EMD

Reputation: 201

How to use offset on Telegram bot API webHook

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?

Upvotes: 6

Views: 11902

Answers (4)

Morteza Sepehri Niya
Morteza Sepehri Niya

Reputation: 414

You must say to telegram that you get updates successfully with this:

 - 200 response code
 &
 - empty json like this {}

Upvotes: 5

Emad Rahnama
Emad Rahnama

Reputation: 1238

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);

Upvotes: 2

EMD
EMD

Reputation: 201

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.

Upvotes: 13

Chris Brand
Chris Brand

Reputation: 1990

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.

Upvotes: 1

Related Questions