B
B
barder2020-11-08 00:36:42
PHP
barder, 2020-11-08 00:36:42

How to read callback_query in Transmitted JSON from Telegram?

Hi all.
I decided to learn PHP and TELEGRAM, that is, I'm studying creating a bot in PHP
. In general, the sent $data['message']['text'] works fine, the bot responds to texts, and so I decided to make a button insight, but the bot does not respond to them.
I'm trying to send a message like this:

if (mb_stripos($data['callback_query']['data'], '/yes_name') !== false) {
    sendTelegram(
      'sendMessage', 
      array(
        'chat_id' => $chat_id,
        'text' => $data['message']['chat']['first_name'] . " Отлично!!",
        'reply_markup' => json_encode(array('inline_keyboard' => $keyboard))

      )
    );
    exit();	
  }


Just in case, the whole code:
$data = file_get_contents('php://input');
$data = json_decode($data, true);

$fname = $data['message']['chat']['first_name']; // выделяем имя собеседника
$lname = $data['message']['chat']['last_name'];  // выделяем фамилию собеседника
$uname = $data['message']['chat']['username'];   // выделяем ник собеседника
$chat_id = $data['message']['chat']['id'];

if (true) {
  error_reporting(E_ALL & ~(E_NOTICE | E_USER_NOTICE | E_DEPRECATED));
  ini_set('display_errors', 1);
}
if (empty($data['message']['chat']['id'])) {
  exit();
}
// Функция вызова методов API.
function sendTelegram($method, $response)
{
  $ch = curl_init('https://api.telegram.org/bot' . TOKEN . '/' . $method);  
  curl_setopt($ch, CURLOPT_POST, 1);  
  curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, false);
  $res = curl_exec($ch);
  curl_close($ch);
        return $res;
}
$keyboard = array(
  array(
    array("text"=>"Да","callback_data"=>'/yes_name'),
    array("text"=>"Справочник бота ","callback_data"=>'/help')
  )
);

// Ответ на текстовые сообщения.
if (!empty($data['message']['text'])) {
  $name = $data['message']['chat']['first_name'];

  if (mb_stripos($data['message']['text'], '/start') !== false) {
    sendTelegram(
      'sendMessage', 
      array(
        'chat_id' => $chat_id,
        'text' => $data['message']['chat']['first_name'] . " привет!  \nДавай знакомиться - твое имя " . $data['message']['chat']['first_name'] ."?\n" . $data['callback_query']['id'],
        'reply_markup' => json_encode(array('inline_keyboard' => $keyboard))
        
      )
    );
    
    
    exit();	
  }
//Ответ на кнопки инсайт
} else if (!empty($data['callback_query']['data'])) {
  
  if (mb_stripos($data['callback_query']['data'], '/yes_name') !== false) {
    sendTelegram(
      'sendMessage', 
      array(
        'chat_id' => $chat_id,
        'text' => $data['message']['chat']['first_name'] . " Отлично!!",
        'reply_markup' => json_encode(array('inline_keyboard' => $keyboard))

      )
    );
    exit();	
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question