PHP - 如何读取在ReplyKeyboardMarkup(Telegram bot)上单击的按钮的文本

时间:2017-07-13 08:37:15

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

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

问题

我有这段代码可以帮助我对我的机器人进行webhook响应。

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

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

然后我添加这个swtich

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 "/aggiungi":
sendMessageButtons($chatID, "Sample text", $keyboard);
**$ButtonText = $updates["message"]["text"];
sendMessage($chatID, $ButtonText);**

但结果是 this

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

问题:

如何保存点击按钮的文字?

我希望你理解这个问题,谢谢大家的时间!

1 个答案:

答案 0 :(得分:0)

使用以下代码:

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

        ]

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

相关问题