Telegram - Как использовать callback_query для выполнения действия (curl_exe c) в PHP - PullRequest
1 голос
/ 13 июля 2020

Я хочу выполнить URL-адрес (например, curl_exe c) в PHP в соответствии с ответом пользователя. Возможно ли это с API Telgram? Я не нашел способа сделать это.

IE: если пользователь нажимает кнопку подтверждения, я выполняю curl_exe c с токеном url +, чтобы подтвердить бронирование для этого пользователя в моем db ( на стороне сервера).

Мой код бота:


    <?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"]);
    }
    
    ?>
...