0
我在電報中有一個簡單的機器人。我的「/ select」命令顯示兩個按鈕,每個按鈕都有自己的值。所以,如果用戶點擊一個按鈕,我可以得到文本,但我可以得到callback_data值。不知道我做錯了什麼。Telegram Bot:如何獲得callback_data值?
下面的代碼:
$update = json_decode(file_get_contents('php://input'));
$callback_query = $update['callback_query'];
if (isset($callback_query)){
//Fetching callback
$data = $callback_query->data;
$message = $callback_query->message;
$message_id = $callback_query->message->message_id;
$chat_id = $message->chat->id;
switch($data){
case "1":
bot('SendMessage',[
'chat_id' => $chat_id,
'text' => "1"
]);
break;
case "2":
bot('SendMessage',[
'chat_id' => $chat_id,
'text' => "2"
]);
break;
}
}else{
$message = $update->message;
$message_id = $update->message->message_id;
$text = $message->text;
$chat_id = $message->chat->id;
//Statement beginning
switch($text){
case "/select":
$keyboard = array(
'keyboard' => [[['text' => "one", 'callback_data' => "1"]],[['text' => "two", 'callback_data' => "2"]]],
'resize_keyboard' => true,
'one_time_keyboard' => true
);
$markup = json_encode($keyboard, true);
bot('SendMessage',[
'chat_id' => $chat_id,
'reply_markup' => $markup,
'text' => "Choose your option"
]);
break;
default:
bot('SendMessage',[
'chat_id' => $chat_id,
'text' => "This is a test"
]);
}
}
我編輯了代碼,我添加了callback_query部分,但仍然不起作用。 –