【问题标题】:How to get callback_data from Telegram in PHP如何在 PHP 中从 Telegram 获取 callback_data
【发布时间】:2020-07-04 23:59:09
【问题描述】:

我想从响应中获取回调数据,但数组为空。

我试图在消息callback_data 数组中显示。

这是我的代码:

$botToken = "token";
$botAPI = "https://api.telegram.org/bot" . $botToken;
$update = json_decode(file_get_contents('php://input'), TRUE);
$message = $update["message"]["text"]
$chatId = $update["message"]["chat"]["id"];
$callback_query = $update['callback_query'];
$callback_query_data = $callback_query['data'];
$url = $botAPI . '/sendMessage?chat_id=' . $chatId . '&text=';

if(isset($callback_query)){
    file_get_contents($url . $callback_query_data);
}

if($message == "/start"){
    $parameters = array('chat_id' => $chatId, "text" => "Здравствуйте. Выберите язык. \nАссалом. Забонро интихоб кунед.\nHi! Select your language.");
    $parameters["method"] = "sendMessage";
    $keyboard = ['inline_keyboard' => [[
        ['text' => "????????Точики", 'callback_data' => 'tajik'],
        ['text' =>  "????????Русский", 'callback_data' => 'russian'],
        ['text' => '????????English', 'callback_data' => 'english']
    ]]];
    $parameters["reply_markup"] = json_encode($keyboard, true);
    echo json_encode($parameters);
}

【问题讨论】:

  • if(isset($callback_query)) 使您使用它的方式变得零意义。 当然这个变量总是设置在这一点上 - 你自己在代码的前四行做了那四行。
  • if($message == "/start")$message 是什么,它应该是从哪里来的?
  • @CBroe 我忘了写。 $message = $update["message"]["text"]。那么如何打印callback_query
  • 什么意思,如何“打印”?如果您不确定是否会设置$update['callback_query'],在您从外部收到的数据中 - 那么您需要检查那个,而不是完全不同的东西。
  • @CBroe 检查什么? $update['callback_query],我说的对吗?

标签: php telegram-bot php-telegram-bot


【解决方案1】:

代码中存在一些小错误,我会尝试解决它们;


只有webhooks 才能使用php://input,你告诉Telegram 你的脚本的位置了吗?
https://api.telegram.org/bot<MY-TOKEN>/setWebhook?url=https://example.com/telegram/script.php

$callback_query_data = $callback_query['data']

仅在按下按钮时设置,应在检查是否有回调数据后调用

if (isset($update['callback_query'])) {
    $callback_query_data = $update['callback_query']['data'];
}


工作示例:
<?php

    $update = json_decode(file_get_contents('php://input'), TRUE);

    $botToken = "<MY-TOKEN>";
    $botAPI = "https://api.telegram.org/bot" . $botToken;

    // Check if callback is set
    if (isset($update['callback_query'])) {

        // Reply with callback_query data
        $data = http_build_query([
            'text' => 'Selected language: ' . $update['callback_query']['data'],
            'chat_id' => $update['callback_query']['from']['id']
        ]);
        file_get_contents($botAPI . "/sendMessage?{$data}");
    }

    // Check for normal command
    $msg = $update['message']['text'];
    if ($msg === "/start") {

        // Create keyboard
        $data = http_build_query([
            'text' => 'Please select language;',
            'chat_id' => $update['message']['from']['id']
        ]);
        $keyboard = json_encode([
            "inline_keyboard" => [
                [
                    [
                        "text" => "english",
                        "callback_data" => "english"
                    ],
                    [
                        "text" => "russian",
                        "callback_data" => "russian"
                    ]
                ]
            ]
        ]);

        // Send keyboard
        file_get_contents($botAPI . "/sendMessage?{$data}&reply_markup={$keyboard}");
    }

如果有不清楚的地方请告诉我!

【讨论】:

  • 谢谢你!有用。我设置了 webhook,这是真的。但我复制了你的代码,它可以工作。非常感谢。
  • 太棒了!你也知道它为什么有效吗?你应该学习一些东西,而不仅仅是复制粘贴哈哈。很高兴它起作用了;)
  • 是的,明白了。再次感谢
猜你喜欢
  • 2018-04-10
  • 1970-01-01
  • 2020-05-24
  • 2016-04-04
  • 1970-01-01
  • 2017-11-25
  • 2019-02-14
  • 2022-07-14
  • 2022-07-28
相关资源
最近更新 更多