未发送电报Bot回调查询

时间:2019-02-10 08:06:17

标签: php telegram

我创建了2个Inline-Button,我想评估callback_data。但不幸的是,没有发送callback_data。

<?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

有人可以帮助我解决这个问题吗?我发送的信息不仅仅是单击按钮的文本。谢谢!

1 个答案:

答案 0 :(得分:0)

有很多要纠正的地方。 第7行为$callback_query = $update['callback_query'];
从第19行到第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.
);

从36到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) 
);