【问题标题】:How to solve a error with callback_data or method in PHP如何使用 PHP 中的 callback_data 或方法解决错误
【发布时间】:2019-08-03 10:51:49
【问题描述】:

这个想法是有一些按钮来做某事,我得到了以下代码,方法 (sendMessage) 或 callback_data 有问题,因为我在 $message 所在的所有行中都收到未定义的索引错误,如果我使用 url 而不是回调数据工作正常

我使用网络钩子

<?php

$botToken = "TOKEN";
$website = "https://api.telegram.org/bot".$botToken;

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

$chatId = $update["message"]["chat"]["id"];

$message = $update["message"]["text"];

$response ="testing";
$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => 'This is a test', 'callback_data' => 'testcompleted']
        ]
    ]
];

$parameters = 
    array(
        'chat_id' => $chatId, 
        'text' => $response, 
        'reply_markup' => json_encode($keyboard)
    );

send($parameters);

function send($data)
{
    $url = "https://api.telegram.org/botTOKEN/sendMessage";

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

【问题讨论】:

  • 你能显示你得到的完整错误吗?
  • $message 所在的所有行中的未定义索引错误

标签: php button bots telegram php-telegram-bot


【解决方案1】:

如果请求是回调,你应该得到这样的数据:

$chatId = $update['callback_query']['message']['chat']['id'];

$message = $update['callback_query']["message"]["text"];

您可以查看isset($update['callback_query'])并根据结果获取数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2022-11-11
    • 2016-04-10
    • 2020-04-19
    • 2021-12-26
    • 1970-01-01
    • 2022-11-01
    相关资源
    最近更新 更多