电报API Bot-对按钮触摸的CallbackQuery响应

时间:2019-10-18 08:30:09

标签: php api bots telegram telegram-bot

实际上,问题是如何在单击按钮时从用户那里得到答案? 什么应该更改\重写代码?

$access_token = '...';
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);
$message = $output['message']['text'];
$chat_id = $output['message']['chat']['id'];

if ($output['callback_query'] != null){
    $data = $output['callback_query']['data'];
    $data_id = $output['callback_query']['id'];
    $chat = $output['callback_query']['message']['chat']['id'];
    switch($data){
        case "/123":
           answerCallback($data_id, '123');
           sendMessage($chat, "123", null);
        break;
        case "/plz":
           answerCallback($data_id, 'plz');
           sendMessage($chat, "plz", null);
        break;
     }
   }
elseif ($message != null) {
    switch($message) {
        case '/test':  
        $inline_button1 = array("text"=>"123","callback_data"=>"/123");
        $inline_button2 = array("text"=>"work plz","callback_data"=>'/plz');
        $inline_keyboard = [[$inline_button1,$inline_button2]];
        $keyboard=array("inline_keyboard"=>$inline_keyboard);
        $replyMarkup = json_encode($keyboard); 
         sendMessage($chat_id, "ok", $replyMarkup);
        break;
    }
}

function sendMessage($chat_id, $message, $replyMarkup) {
  file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);
}
function answerCallback($id, $text) {
    file_get_contents($GLOBALS['api'] . '/answerCallbackQuery?callback_query_id='.$id.'&text='.$text);
}

此代码仅允许您从消息/test中获得响应,而不响应按钮的按下。 使用webhook。

1 个答案:

答案 0 :(得分:0)

查看您的错误日志。您忘记提供$replyMarkup的值。

PHP Warning: Missing argument 3 for sendMessage()