Код кнопок
function sendMessageWithButtons($message, $tg_user_id = self::TG_USER_ID)
{
$arrayQuery = array(
'chat_id' => $tg_user_id,
'text' => $message,
'reply_markup' => json_encode(
array(
//массив клавитуры
'inline_keyboard' => array(
//массив ряда кнопок
array(
//массив кнопки
array(
'text' => 'Button 1',
'callback_data' => 'button_1',
),
array(
'text' => 'Button 2',
'callback_data' => 'button_2',
),
),
),
)
),
);
$ch = curl_init(self::TG_API_URL . self::TG_TOKEN . self::TG_METHOD_SENDMESSAGE . http_build_query($arrayQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$resultQuetry = curl_exec($ch);
curl_close($ch);
return json_decode($resultQuetry, true);
}
Код обработки
function getWebhook()
{
$data = file_get_contents('php://input');
$data = json_decode($data, true);
$this->writeWebhook($data, $clear_file = false);
//тут перехватываем нажатие кнопок
if (!empty($data['callback_query']))
{
$this->answerCallbackQuery($data['callback_query']['id']);
$tg_user_id = $data['callback_query']['message']['from']['id'];
$message = 'ТЫ нажал кнопки';
$this->sendMessage($message, $tg_user_id);
$buttonOrCommand = $data['callback_query']['data'];
if ($buttonOrCommand == 'button_1')
{
$message = 'ТЫ нажал кнопку 1';
$this->sendMessage($message, $tg_user_id);
}
elseif ($buttonOrCommand == 'button_2')
{
$message = 'ТЫ нажал кнопку 2';
$this->sendMessage($message, $tg_user_id);
}
}
При нажатии кнопки приходит такой ответ
spoiler
Array
(
[update_id] => 3434343434343434
[callback_query] => Array
(
[id] => 34343434343434343434343434343434
[from] => Array
(
[id] => 43434343434343434
[is_bot] =>
[first_name] => Sergey
[username] => 3434343434343434343
[language_code] => en
)
[message] => Array
(
[message_id] => 1149
[from] => Array
(
[id] => 343434343434343434343
[is_bot] => 1
[first_name] => Bot34343434
[username] => BotT3434343434
)
[chat] => Array
(
[id] => 34343434343434343434
[first_name] => Sergey
[username] => 343444444434434
[type] => private
)
[date] => 1684046960
[text] => держи свои кнопки
[reply_markup] => Array
(
[inline_keyboard] => Array
(
[0] => Array
(
[0] => Array
(
[text] => Button 1
[callback_data] => button_1
)
[1] => Array
(
[text] => Button 2
[callback_data] => button_2
)
)
)
)
)
[chat_instance] => -343434343434
[data] => button_1
)
)
Php скрипт не обрабатывает нажатие кнопок, как будто $data['callback_query'] не существует в ответе.