N
N
newaitix2022-03-29 16:06:09
PHP
newaitix, 2022-03-29 16:06:09

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);
}

Made a simple echo bot, trying to add buttons.
How to handle next button click?
What happens when you click on the next button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2022-03-29
@nokimaro

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 CallbackQuerywill appear in the list that returns the method /getUpdates
If a webhook was configured, then a POST request containing object data CallbackQueryin 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 question

Ask a Question

731 491 924 answers to any question