2017-10-29 470 views
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" 
     ]); 
} 

} 

回答

0

你只需要proccess更新從電報到您的網絡掛接傳入的,這意味着你需要的條件如果更新型callback_query和分析數據。

請檢查電報文件,或者看一些例子爲PHP,如:

How can I differentiate between a 'Message' update and a 'Callback Query' update? (Telegram Bot API)

https://jqueryajaxphp.com/telegram-bot-api-keyboards-callbacks/

祝你好運! &保持冷靜併發送電報:D

+0

我編輯了代碼,我添加了callback_query部分,但仍然不起作用。 –