Skip to content
Advertisement

Telegram bot inline keyboard callback not working

I’m using PHP for the telegram bot. Reply callback is working when the callback hit the callback URL but the inline keyboard callback not working. When I click on the inline keyboard button, nothing responds, why it’s happening? Please help me to fix the issues.

$update = file_get_contents('php://input');
$update = json_decode($update, true);
$userChatId = $update["message"]["chat"]["id"]?$update["message"]["chat"]["id"]:null;

if($userChatId){
    $userMessage = $update["message"]["text"]?$update["message"]["text"]:"Nothing";
    $firstName = $update["message"]["from"]["first_name"]?$update["message"]["from"]["first_name"]:"N/A";
    $lastName = $update["message"]["from"]["last_name"]?$update["message"]["from"]["last_name"]:"N/A";
    $fullName = $firstName." ".$lastName;
$callback_query = $update['callback_query'];
$callback_query_data = $callback_query['data'];


$url = "https://webhook.site/f695055e-5a65-4120-9ea2-0581667bbd61?kk=";
if(isset($callback_query)){
    file_get_contents($url.$callback_query_data);
}

if($userMessage == "/start"){


    $replyMsg = "Welcome to Bot";


    $keyboard = [
        'inline_keyboard' => [
            [
                ['text' => 'Button 1', 'callback_data' => '1'],['text' => 'Button 2', 'callback_data' => '2']
            ],
            [
                ['text' => 'Button 3', 'callback_data' => '3']
            ]
        ]
    ];
    $encodedKeyboard = json_encode($keyboard);

    $parameters = array(
        "chat_id" => $userChatId,
        "text" => $replyMsg,
        'reply_markup' => $encodedKeyboard
    );

    send("sendMessage", $parameters);
}
}
function send($method, $data){
    $BOT_TOKEN = "Telegram_key";
    $url = "https://api.telegram.org/bot$BOT_TOKEN/$method";

    if(!$curld = curl_init()){
        exit;
    }
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curld, CURLOPT_URL, $url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curld);
    curl_close($curld);
    return $output;
}

?> ```

Advertisement

Answer

Have you implemented something to respond the callback query?!

When you receive an update object, you can check callback_query field for a CallbackQuery object that has data field containing a string. Handle it as you handle the normal messages: https://core.telegram.org/bots/api#callbackquery

NOTE: After the user presses a callback button, Telegram clients will display a progress bar until you call answerCallbackQuery. It is, therefore, necessary to react by calling answerCallbackQuery even if no notification to the user is needed (e.g., without specifying any of the optional parameters).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement