Answer the question
In order to leave comments, you need to log in
How to handle telegram button click?
define('TOKEN','...');
$data=file_get_contents('php://input');
$data=json_decode($data,true);
if(!empty($data['message']['text'])){
$text=$data['message']['text'];
$keyboard_step_1=array(
array(
array(
'text'=>'next',
'callback_data'=>'{"action":"go_step_2","count":0,"text":"next"}'
),
array(
'text'=>'link',
'url'=>'https://google.com'
)
)
);
$response=array(
'chat_id'=>$data['message']['chat']['id'],
'text'=>$text,
'reply_markup'=>json_encode(
array(
'inline_keyboard'=>$keyboard_step_1
)
)
);
$ch = curl_init('https://api.telegram.org/bot'.TOKEN.'/sendMessage');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);
}
Answer the question
In order to leave comments, you need to log in
The answers to your questions are in the documentation. I recommend it for thoughtful reading.
https://core.telegram.org/bots/api#getting-updates
https://core.telegram.org/bots/api#callbackquery
When you click on the button, the view object CallbackQuery
will appear in the list that returns the method /getUpdates
If a webhook was configured, then a POST request containing object data CallbackQuery
in JSON format will be sent to the url specified in the webhook .
Handle the button click based on your preferences and the required logic. In the simplest case - they saw the necessary event - they reacted (sent a message).
PHP code examples - https://core.telegram.org/bots/samples#php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question