Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot: using offset in getUpdates method

I want to build a telegram bot for feed subscription, so the subscribers can get site update. But I need user to start chat with my bot. I'll using deep linking according to this url: https://core.telegram.org/bots#deep-linking (assuming there are 2 users)

  1. Show the below link to user #2 https://telegram.me/MyBot?start=$unique_code
  2. User #2 clicks on link and start chat with bot.
  3. User #2 comes back to my site and clicks on check button.
  4. Site makes a getUpdates request and find the chat_id that associated with the user's unique_code.
  5. Offset will be increased by 1.

Now there is a problem. When offset increased what about user #1 that starts chat with bot just before the user #2. If #1 clicks on check button after increasing offset by #2, the bot will not received the #1 message.

p.s. I don't want using ssl and webhook

Sorry for bad English.

like image 821
Ameer Mousavi Avatar asked Sep 27 '22 04:09

Ameer Mousavi


1 Answers

You are almost right in what you are trying to achieve. Two things:

  • Step 3 is unnecessary.
  • You should store this $unique_id somewhere, together with the user's username on your website. Then, when this person clicks on your link with your unique id, you can link the user's userId to the user's username.

So the steps become:

  1. Generate a unique code (let's call it $unique_code). Save this code together with the username of the person currently logged on on your website (let's call that $username) in a database.
  2. Show user #2 a link with this unique code (https://telegram.me/MyBot?start=$unique_code)
  3. The user clicks the link, after which your bot receives a message with the $unique_code ('/start $unique_code').
  4. The bot associates the $unique_code with $username and stores the chat_id of the user that sent the message, in the database. (message.chat.id - see https://core.telegram.org/bots/api#message)

Now, whenever you want to send a message to $username, simply look up their chat_id in the database and send a message to that chatId (https://core.telegram.org/bots/api#sendmessage).

like image 126
Pieter van den Ham Avatar answered Oct 04 '22 21:10

Pieter van den Ham