电报机器人内联键盘回调不起作用
Telegram bot inline keyboard callback not working
我正在为电报机器人使用 PHP。当回调命中回调 URL 但内联键盘回调无效时,回复回调有效。当我点击内联键盘按钮时,没有任何反应,为什么会这样?请帮我解决问题。
$update = file_get_contents('php://input');
$update = json_decode($update, true);
$userChatId = $update["message"]["chat"]["id"]?$update["message"]["chat"]["id"]:null;
if($userChatId){
$userMessage = $update["message"]["text"]?$update["message"]["text"]:"Nothing";
$firstName = $update["message"]["from"]["first_name"]?$update["message"]["from"]["first_name"]:"N/A";
$lastName = $update["message"]["from"]["last_name"]?$update["message"]["from"]["last_name"]:"N/A";
$fullName = $firstName." ".$lastName;
$callback_query = $update['callback_query'];
$callback_query_data = $callback_query['data'];
$url = "https://webhook.site/f695055e-5a65-4120-9ea2-0581667bbd61?kk=";
if(isset($callback_query)){
file_get_contents($url.$callback_query_data);
}
if($userMessage == "/start"){
$replyMsg = "Welcome to Bot";
$keyboard = [
'inline_keyboard' => [
[
['text' => 'Button 1', 'callback_data' => '1'],['text' => 'Button 2', 'callback_data' => '2']
],
[
['text' => 'Button 3', 'callback_data' => '3']
]
]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = array(
"chat_id" => $userChatId,
"text" => $replyMsg,
'reply_markup' => $encodedKeyboard
);
send("sendMessage", $parameters);
}
}
function send($method, $data){
$BOT_TOKEN = "Telegram_key";
$url = "https://api.telegram.org/bot$BOT_TOKEN/$method";
if(!$curld = curl_init()){
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close($curld);
return $output;
}
?> ```
你有没有实现一些东西来响应回调查询?!
当您收到更新对象时,您可以检查 callback_query
字段以查找具有包含字符串的 data
字段的 CallbackQuery
对象。像处理普通消息一样处理它:
https://core.telegram.org/bots/api#callbackquery
NOTE: After the user presses a callback button, Telegram clients will
display a progress bar until you call answerCallbackQuery. It is,
therefore, necessary to react by calling answerCallbackQuery even if
no notification to the user is needed (e.g., without specifying any of
the optional parameters).
我正在为电报机器人使用 PHP。当回调命中回调 URL 但内联键盘回调无效时,回复回调有效。当我点击内联键盘按钮时,没有任何反应,为什么会这样?请帮我解决问题。
$update = file_get_contents('php://input');
$update = json_decode($update, true);
$userChatId = $update["message"]["chat"]["id"]?$update["message"]["chat"]["id"]:null;
if($userChatId){
$userMessage = $update["message"]["text"]?$update["message"]["text"]:"Nothing";
$firstName = $update["message"]["from"]["first_name"]?$update["message"]["from"]["first_name"]:"N/A";
$lastName = $update["message"]["from"]["last_name"]?$update["message"]["from"]["last_name"]:"N/A";
$fullName = $firstName." ".$lastName;
$callback_query = $update['callback_query'];
$callback_query_data = $callback_query['data'];
$url = "https://webhook.site/f695055e-5a65-4120-9ea2-0581667bbd61?kk=";
if(isset($callback_query)){
file_get_contents($url.$callback_query_data);
}
if($userMessage == "/start"){
$replyMsg = "Welcome to Bot";
$keyboard = [
'inline_keyboard' => [
[
['text' => 'Button 1', 'callback_data' => '1'],['text' => 'Button 2', 'callback_data' => '2']
],
[
['text' => 'Button 3', 'callback_data' => '3']
]
]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = array(
"chat_id" => $userChatId,
"text" => $replyMsg,
'reply_markup' => $encodedKeyboard
);
send("sendMessage", $parameters);
}
}
function send($method, $data){
$BOT_TOKEN = "Telegram_key";
$url = "https://api.telegram.org/bot$BOT_TOKEN/$method";
if(!$curld = curl_init()){
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close($curld);
return $output;
}
?> ```
你有没有实现一些东西来响应回调查询?!
当您收到更新对象时,您可以检查 callback_query
字段以查找具有包含字符串的 data
字段的 CallbackQuery
对象。像处理普通消息一样处理它:
https://core.telegram.org/bots/api#callbackquery
NOTE: After the user presses a callback button, Telegram clients will display a progress bar until you call answerCallbackQuery. It is, therefore, necessary to react by calling answerCallbackQuery even if no notification to the user is needed (e.g., without specifying any of the optional parameters).