如何使用PHP中的callback_data或方法解决错误

问题描述 投票:-1回答:1

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

我用了一个webhook

<?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;
}
?>
php button bots telegram php-telegram-bot
1个回答
0
投票

如果请求是回调,您应该获得如下数据:

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.