skip to Main Content

I’ve created 2 Inline-Button and I want to evaluate the callback_data. But unfortunately, the callback_data is not sent.

<?php
$bot_token = 'There is no Token seen'; // Telegram bot token
$url = "https://api.telegram.org/bot$bot_token/sendMessage";
$content = file_get_contents('php://input');
$update = json_decode($content, TRUE);
$callback_query = $update['callbackQuery'];
$callback_data = $callback_query['data'];

$ser_update = serialize($update);
db_query("INSERT INTO prefix_telegram (text) VALUES ('".$ser_update."')");

if (isset($update['message']['text'])) {
    $text    = $update['message']['text'];
    $chat_id = $update['message']['chat']['id'];

    if (strpos($text, 'outi') !== false) {
        $reply = utf8_encode("Wähle einen Button!");
        $keyboard = array(
            "keyboard" =>   array(
                                    array(
                                        array(
                                            "text"  => "Button1",
                                            "callback_data" => "1",
                                        ),
                                        array(
                                            "text"  => "Button2",
                                            "callback_data" => "2",
                                        ),
                                    )
                                ),
                                "one_time_keyboard" => true,
                                "resize_keyboard" => true
        );

        $postfields = array(
            'chat_id'       => "$chat_id",
            'text'          => "$reply",
            'reply_markup'  => json_encode($keyboard)
        );

        if (!$curld = curl_init()) {
            exit;
        }

        curl_setopt($curld, CURLOPT_POST, true);
        curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
        curl_setopt($curld, CURLOPT_URL,$url);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

        $output = curl_exec($curld);

        curl_close ($curld);
    }
}

Result of file_get_contents

Could anyone help me with this problem? I have sent more information than just the text of the clicked button. Thanks!

2

Answers


  1. There is muuuuch to correct.
    In line 7, it is $callback_query = $update['callback_query'];
    From line 19 to 34:

    $keyboard = array(
      "inline_keyboard" => array( // inline_keyboard not keyboard
        array(
          array( 
            "text" => "Button1", 
            "callback_data" => "1", 
          ), 
          array( 
            "text" => "Button2", 
            "callback_data" => "2", 
          ), 
        )
      ) //Removed onetimekeyboard etc.
    );
    

    From 36 to 40:

    $postfields = array( 
      'chat_id' => $chat_id, // you don't have to use quotes when you're using variables or numbers.
      'text' => $reply, 
      'reply_markup' => json_encode($keyboard) 
    );
    
    Login or Signup to reply.
  2. The callback_data is not supported for reply_markup.keyboard. It is supported only for reply_markup.inline_keyboard.

    You can vote for the issue Add callback_data to KeyboardButton (reply_markup.keyboard).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search