我正在使用PHP进行Telegram Bot,当callback_data为“dataArrivo_”时,bot会发送消息。 这是我的代码:
<?php
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if(!$update)
{
echo "Non puoi <code>:)</code>\n";
echo "Dati ricevuti:\n".json_encode($_POST);
exit;
}
$message = isset($update['message']) ? $update['message'] : "";
$messageId = isset($message['message_id']) ? $message['message_id'] : "";
$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : "";
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : "";
$cbData = isset($update["callback_query"]["data"]) ? $update["callback_query"]["data"] : "";
$cbmid = isset($update["callback_query"]["message"]["message_id"]) ? $update["callback_query"]["message"]["message_id"] : "";
$cbid = isset($update["callback_query"]["id"]) ? $update["callback_query"]["id"] : "";
$cbcid = isset($update["callback_query"]["from"]["id"]) ? $update["callback_query"]["from"]["id"] : "";
$username = isset($message['chat']['username']) ? $message['chat']['username'] : "";
$date = isset($message['date']) ? $message['date'] : "";
$text = isset($message['text']) ? $message['text'] : "";
$text = trim($text);
header("Content-Type: application/json");
if($username){
if($cbid){
switch($cbData){
case "dataArrivo_":
$btn1 = array("text" => "Torna indietro", "callback_data" => "start");
$keyboard = array(array($btn1));
$keyboard = array("inline_keyboard" => $keyboard);
$parameters = array(
"chat_id" => $cbcid,
"text" => "Data di arrivo prevista:\n`01 Settembre 2016`",
"parse_mode" => "Markdown",
"message_id" => $cbmid,
"reply_markup" => $keyboard
);
$parameters["method"] = "editMessageText";
break;
case "start":
$btn1 = array("text" => "Versione Chatfuel", "url" => "https://telegram.me/HumanEPandaBot");
$btn2 = array("text" => "Data di arrivo prevista", "callback_data" => "dataArrivo_");
$keyboard = array(array($btn1), array($btn2));
$keyboard = array("inline_keyboard" => $keyboard);
$parameters = array('chat_id' => $chatId, "text" => "Salve, avventuriero @". $username.", il bot al momento non è disponibile ma se vuoi puoi giocare sulla [versione Chatfuel!](https://telegram.me/HumanEPandaBot)", "parse_mode" => "Markdown", "reply_markup" => $keyboard, "disable_web_page_preview" => true, "message_id" => $cbmid);
$parameters["method"] = "editMessageText";
break;
default:
$btn1 = array("text" => "Versione Chatfuel", "url" => "https://telegram.me/HumanEPandaBot");
$btn2 = array("text" => "Data di arrivo prevista", "callback_data" => "dataArrivo_");
$buttons = array(array($btn1), array($btn2));
$keyboard = array("inline_keyboard" => $buttons);
$parameters = array('chat_id' => $chatId, "reply_to_message_id" => $messageId, "text" => "Salve, avventuriero @". $username.", il bot al momento non è disponibile ma se vuoi puoi giocare sulla [versione Chatfuel!](https://telegram.me/HumanEPandaBot)", "parse_mode" => "Markdown", "reply_markup" => $keyboard, "disable_web_page_preview" => true);
$parameters["method"] = "sendMessage";
}
}
}else{
$parameters = array("chat_id" => $chatId, "reply_to_message_id" => $messageId, "text" => "Mi dispiace, ma devi impostare un username per utilizzarmi.\nImpostazioni > Username");
$parameters["method"] = "sendMessage";
}
echo json_encode($parameters);
我用$ cbData(callback_data)而不是if,但我认为我错过了部分代码... 抱歉我的英语不好我是意大利语:P
答案 0 :(得分:0)
如果要解析按下生成的响应按钮,则应该读取callback_query字段,因为响应不是普通消息。它包含您在按钮中设置的回调数据(字段数据)。我建议你在每个输出中写一个文件,这样你就可以看到每个响应的整个结构。