D
D
Doniyor Mamatkulov2017-10-12 20:35:33
PHP
Doniyor Mamatkulov, 2017-10-12 20:35:33

Inline buttons in Telegram - Callback?

Hello!
It is necessary to arrange a handler for clicking on inline buttons in Telegram, which does not quite work out ...
The scenario is as follows: the user clicks on the predefined command "/stop", and the bot answers with the question "Are you sure?" and displays two options: "Yes" and "No". If you click on "Yes" - then you need to intercept this command and perform a certain action on the server.
My code:

require_once('TelegramUserBot.php');
        
        $api = new TelegramUserBot();
        $api->token = '433336723:AAF*******************';
        $updates = json_decode(file_get_contents('php://input') ,true);

        $chat_id =   $updates['message']['chat']['id'];
    
    $callback_query = $updates['callback_query'];
    $data = $callback_query['data'];

        if($updates['update_id']) {
            switch($updates['message']['text']) {		
        case '/stop':
          //disable notifications for this user
          $inline_button1 = array("text"=>"Да","callback_data"=>'/stop-confirm');
          $inline_button2 = array("text"=>"Нет","callback_data"=>'/stop-continue');
          $inline_keyboard = ;
          $keyboard=array("inline_keyboard"=>$inline_keyboard);
          $api->sendMessage($chat_id, "Вы уверены?", json_encode($keyboard));
          break;
                    
                default:
                    $api->sendMessage($chat_id, "Команда \"".$updates['message']['text']."\" не найдена");
                    break;
          
        if($updates['message']['text'] == '/stop-confirm') {
          $api->sendMessage($chat_id, "Вы ответили подтверждением");
                                        //TODO - disable notifications
        }
            }
        }

Everything except checking the callback data works. Can't intercept click on inline button(((

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2017-10-12
@doniyorbekm

Good afternoon.
Callback requests are handled differently.
Get $callback_query = $updates['callback_query'];
Later

$cq_command = mb_strtolower($callback_query['data']);

Chat id will be here $callback_query['message']['chat']['id'];
So look towards $cq_command

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question