1

Need help with inline keyboard. I made a button, but what to do with the callback? I understand I need to somehow get a callback_data and issue a new message.

     <?php
$access_token = 'xxx';
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);
$chat_id = $output['message']['chat']['id'];
$first_name = $output['message']['chat']['first_name'];
$message = $output['message']['text'];
$callback_query = $output['callback_query'];
$data = $callback_query['data'];
$message_id = ['callback_query']['message']['message_id'];
switch($message) {
    case '/test':  
    $inline_button1 = array("text"=>"Google url","url"=>"http://google.com");
    $inline_button2 = array("text"=>"work plz","callback_data"=>'/plz');
    $inline_keyboard = [[$inline_button1,$inline_button2]];
    $keyboard=array("inline_keyboard"=>$inline_keyboard);
    $replyMarkup = json_encode($keyboard); 
     sendMessage($chat_id, "ok", $replyMarkup);
    break;
}
switch($data){
    case '/plz':
    sendMessage($chat_id, "plz");
    break;
}
function sendMessage($chat_id, $message, $replyMarkup) {
  file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);
}
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
batman
  • 123
  • 1
  • 6

1 Answers1

4

If someone clicks the button you will receive a callback_query object instead of a message object. The callback_query itself contains the originally sent message.

$output = json_decode(file_get_contents('php://input'), TRUE);
$callback_query = $output["callback_query"]
$data = $callback_query["data"] // in your case $data is "/plz"
Maak
  • 4,720
  • 3
  • 28
  • 39
  • I'm sorry, but I do not understand what to do c message_id, I thought it must be so, but it did not work. It can be an example to my code, please? `switch($message_id){ case '/plz': sendMessage($chat_id, "plz"); break; }` – batman Aug 25 '16 at 19:37
  • You need to use your switch on the `$data` to find the `/plz` – Maak Aug 25 '16 at 19:44
  • It doesn't work, what's wrong? updated code in message. – batman Aug 26 '16 at 05:15
  • Your `$chat_id` isn't defined since `$output['message']` doesn't exist. you need ta take the `$chat_id` from the `callback_query["message"]['chat']['id']` – Maak Aug 26 '16 at 10:31