【问题标题】:PHP - How to read the text of a button that was clicked on ReplyKeyboardMarkup (Telegram bot)PHP - 如何读取在ReplyKeyboardMarkup(电报机器人)上单击的按钮的文本
【发布时间】:2017-12-17 22:24:03
【问题描述】:

我想知道热门阅读并将与我的电报机器人聊天的用户单击的按钮的文本保存到变量中。

问题

我有这段代码可以帮助我为我的机器人获取 webhook 响应。

$getupdates = file_get_contents("php://input");
$updates = json_decode($getupdates, TRUE);

然后我将与我的机器人聊天的用户的 chat_id 保存到一个变量 ($chatID) 中,另一个变量 $text 是消息的文本。

然后我添加这个开关

switch($text)
{
    case "/aggiungi":
    sendMessageButtons($chatID, "Sample text", $keyboard);
}

$keyboard 是我的数组数组,其中包含我的 ReplyKeyboardMarkup 键盘。

我的函数“sendMessageButtons”如下:

function sendMessageButtons($chatID, $message, $keyboard)
{
    $url = $GLOBALS['website']."/sendMessage?chat_id=".$chatID."&text=".urlencode($message)."&reply_markup=".$keyboard;
    file_get_contents($url);
}

结果是 this.

但是我如何知道点击了哪个按钮,然后将按钮的文本保存到变量中

我尝试在case下添加代码

case "/aggiungi":
sendMessageButtons($chatID, "Sample text", $keyboard);
**$ButtonText = $updates["message"]["text"];
sendMessage($chatID, $ButtonText);**

但结果是 this.

机器人返回“/aggiungi”的情况,在屏幕上打印最后一个文本(即“/aggiungi”),它不会等待我从键盘输入。

问题:

如何保存来自被点击按钮的文本?

希望您能理解问题并感谢大家的宝贵时间!

【问题讨论】:

标签: javascript php telegram telegram-bot php-telegram-bot


【解决方案1】:

使用下面的代码:

'inline_keyboard'=>[
        [
         ['text'=>"mt btn text",'callback_data'=>"bbv"]

        ]

然后使用if($data == "bbv"){do somthing} 可以找到点击了哪个按钮。

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 2019-03-27
    • 2022-11-09
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    相关资源
    最近更新 更多