问题
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