2016-08-18 155 views
0

我正在做一個帶有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),而不是如果,但我想我錯過了部分代碼... 對不起我的英文不好我是意大利人:P

回答

0

如果您正在解析按鈕生成的響應,您應該閱讀callback_query字段,因爲響應不是普通消息。它包含您在按鈕中設置的回調數據(字段數據)。我建議你在每個輸出中寫一個文件,這樣你就可以看到每個響應的整個結構。

相關問題