How to solve a error with callback_data or method in PHP

天大地大妈咪最大 提交于 2019-12-13 03:18:51

问题


The idea is to have some buttons to do something, I got the following code, and something is wrong with the method (sendMessage) or callback_data because I received a undefined index error in all the rows where is the $message, if I use a url instead of a call back data works fine

I use a 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;
}
?>

回答1:


In case of the request being a callback you should get the data like this:

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

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

you can check isset($update['callback_query']) and get the data based on the result.



来源:https://stackoverflow.com/questions/55130412/how-to-solve-a-error-with-callback-data-or-method-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!