Telegram – How to use a callback_query to make a action (curl_exec) in PHP

I want to execute a URL (Like curl_exec) in PHP according to the user’s response. Is it possible with the Telgram’s API? I didn’t find a way to do that.

I.E.: If a user click in confirm button, I execute a curl_exec with a url+token to confirm the booking to this user in my db (server side).

My bot code is:

     <?php          define('BOT_TOKEN', '00000000:0000000000000');     define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');          function processMessage($message) {       // process the message received       $message_id = $message['message_id'];       $chat_id = $message['chat']['id'];       if (isset($message['text'])) {                  $text = $message['text'];//text received              if (strpos($text, "/start") === 0) {             //send the message to the user           sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Hi, '. $message['from']['first_name'].                   '! Get your Chat ID.',                     'reply_markup' => array('inline_keyboard' => array(                                                          array(                                                              array('text'=>'Get your ID','url'=>'https://t.me/my_id_bot'), //button 1                                                           ),                                             )                                     )));         } else if ($text === "go") { // This message with the book will sent by other code and is only here to understand the context.           sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Hi, '. $message['from']['first_name'].               '! Your booking is tomorrow (XX/XX) XX:XX. Click to cancel or confirm.','reply_markup' => array('inline_keyboard' => array(                                                  array(                                              array('text'=>'Confirm','callback_data'=>'token_confirm','show_alert'=>'true'), //button 1                                                              array('text'=>'Cancel','callback_data'=>'token_cancel','show_alert'=>'true')//button 2                                                           ),                                                          array(                                                              array('text'=>'Waze','url'=>'https://www.waze.com/ul?ll=-20.20202020%2C-10.10101010&navigate=yes&zoom=17')//button 3                                                           )                                             )                                     )));         } else {           sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Sorry, I dont understand the text. I am a robot'));         }       } else {         sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Sorry, I dont understand the text. I am a robot'));       }     }          if (isset($update["message"])) {       processMessage($update["message"]);     }else if (isset($update["callback_query"])) { //Here you check if a answer to buttons 1 and 2            sendMessage("answerCallbackQuery", array('callback_query_id' => $update["callback_query"]["id"], 'text' => ''));             sendMessage("sendMessage", array('chat_id' => $update["callback_query"]["from"]["id"], "text" => getResult($update["callback_query"]["data"], $update["callback_query"]["data"])));     }          function sendMessage($method, $parameters) {       $options = array(       'http' => array(         'method'  => 'POST',         'content' => json_encode($parameters),         'header'=>  "Content-Type: application/json\r\n" .                     "Accept: application/json\r\n"         )     );          $context  = stream_context_create( $options );     file_get_contents(API_URL.$method, false, $context );     }          $update_response = file_get_contents("php://input");          $update = json_decode($update_response, true);          if (isset($update["message"])) {       processMessage($update["message"]);     }          ?> 
Asked on July 16, 2020 in php.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.