Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Chat ID & Text in Telegram?

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 Answers

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.

like image 139
Seyfi Avatar answered Nov 27 '25 05:11

Seyfi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!