Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 213
EMD Avatar asked Jun 27 '15 14:06

EMD


People also ask

What is offset in Telegram bot?

The offset refers to the update_id value. Other answers claim that message_id is the offset, this is not the case at the time of writing. Save this answer. Show activity on this post.

How do you link bots on Webhook?

Go to bot settings, then the "Webhooks" tab. Activate the setting and insert the URL to which you want to send data. Select the events you want to send alerts for and click Save to apply the settings.

How does Telegram bot API work?

An important function of a Telegram bot is the possibility to execute commands in a Telegram chat, which then directly trigger actions or request information. For example, it is possible to send the bot the command “/help” or “/help”, which then outputs the commands possible for this bot in the chat as text feedback.


2 Answers

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.

like image 63
EMD Avatar answered Nov 02 '22 23:11

EMD


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

 - 200 response code
 &
 - empty json like this {}
like image 39
Morteza Sepehri Niya Avatar answered Nov 02 '22 22:11

Morteza Sepehri Niya