3

When I use file_get_contents function in PHP to get UPDATES of my Telegram Bot, I know how to get first Chat ID or Text But this page is not just for a one user & every user that types a text, comes to this page. Also every user has its Chat ID & Text. So How can I detect the latest Text & Chat ID to react to that ? For Example: When I use this code :

$Updates['result'][0]['message']['chat']['id']

It gets the first user Chat ID that has typed in my Bot. When I react to his text, I will send my message to the latest USER uses my bot ... How can I do that ?

1 Answers1

1

I suppose you use webhooks.

Each session starts with some sort of data(text message, photo,...) that user send to bot and ends with process of these data(your bot could send something in reply to user or not)

Keep in mind : In each session your bot script just have the latest message and its sender data, No more no less.

for retrieve text message that user sent,use: $update->message->text or similar. And for retrieve sender chat_id use : $update->message->chat->id

UPDATED

If you do not use webhooks:

process is similar except that you ask telegram to send you messages one by one. Each time your script process the first unread message and script ends. If you make a loop you should save data in a db and refresh chat_id and other message data to lates READ message to not loose correspondence.Else your message and its sender wouldn't be in relation.

Community
  • 1
  • 1
Seyfi
  • 1,832
  • 1
  • 20
  • 35
  • Thanks, What about If I don't use WebHooks ? –  Sep 14 '16 at 18:29
  • @AliBahaari Updated, – Seyfi Sep 14 '16 at 19:12
  • Excuse me But It is a little confusing. Can you please give me an example for not using webhooks ? Thanks ... –  Sep 15 '16 at 21:12
  • @AliBahaari Bb the way, let me share my own experience: Dive in `webhooks` simply. But in polling system(opposite of webhooks) some one sends message #1. Then this user or some one else sends a message, name it message #2. And this repeats, M(essage)#3, M#4; in an unknown time your scripts retrieve messages from telegram. Telegram sends #1 message at first step. Then your script requests again . Now telegram sends #2 message , then M#3 and so on. They are similar to a queue . First message in, first outs.(FIFO) – Seyfi Sep 16 '16 at 14:13