php - 如何在 Telegram 中获取聊天 ID 和文本?

标签 php telegram telegram-bot php-telegram-bot

当我在 PHP 中使用 file_get_contents 函数来获取我的 Telegram Bot 的更新时,我知道如何获取第一个聊天 ID 或文本但是这个页面不仅适用于一个用户 & 每个输入文本的用户都会来到这个页面.此外,每个用户都有其聊天 ID 和文本。那么我如何才能检测到最新的文本和聊天 ID 来对此使用react呢?例如:当我使用这段代码时:

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

它获取在我的 Bot 中输入的第一个用户聊天 ID。当我对他的文字使用react时,我会将我的消息发送给最新的用户使用我的机器人...... 我该怎么做?

最佳答案

我想你使用的是 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.

要检索用户发送的短信,请使用:$update->message->text 或类似的。 对于检索发件人 chat_id 使用:$update->message->chat->id

已更新

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.

关于php - 如何在 Telegram 中获取聊天 ID 和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478823/

相关文章:

bots - 我的 Telegram Bot 不断地发送消息

javascript - 是否可以等待WizardScene?

java - 电报机器人——使用 API

c# - Telegram answerInlineQuery 无法解析数字

python - Telegram:从不同的脚本向机器人发送消息

javascript - 在动态添加的字段中保留 $_POST 值

php - 检查文件的最后一行下面是否有东西

php - 将 mysql 结果导入 Jpgraph

Telegram 机器人只允许在事件发生时发帖?

javascript - 在 PHP 中转义字符串以供 JavaScript 使用