Telegram bot request_contact php

143
27 мая 2019, 03:40

Помогите пожалуйста разобраться с проблемой. Не могу найти ошибку. Делаю запрос контакта пользователя, контакт отправляется, но вытащить номер не получается. Ответ возвращает null.

    <?php
    define('BOT_TOKEN', '<TOKEN>');
    define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
    function apiRequestWebhook($method, $parameters) {
      if (!is_string($method)) {
        error_log("Method name must be a string\n");
        return false;
      }
      if (!$parameters) {
        $parameters = array();
      } else if (!is_array($parameters)) {
        error_log("Parameters must be an array\n");
        return false;
      }
      $parameters["method"] = $method;
      header("Content-Type: application/json");
      echo json_encode($parameters);
      return true;
    }
    function exec_curl_request($handle) {
      $response = curl_exec($handle);
      if ($response === false) {
        $errno = curl_errno($handle);
        $error = curl_error($handle);
        error_log("Curl returned error $errno: $error\n");
        curl_close($handle);
        return false;
      }
      $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
      curl_close($handle);
      if ($http_code >= 500) {
        // do not wat to DDOS server if something goes wrong
        sleep(10);
        return false;
      } else if ($http_code != 200) {
        $response = json_decode($response, true);
        error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
        if ($http_code == 401) {
          throw new Exception('Invalid access token provided');
        }
        return false;
      } else {
        $response = json_decode($response, true);
        if (isset($response['description'])) {
          error_log("Request was successfull: {$response['description']}\n");
        }
        $response = $response['result'];
      }
      return $response;
    }
    function apiRequest($method, $parameters) {
      if (!is_string($method)) {
        error_log("Method name must be a string\n");
        return false;
      }
      if (!$parameters) {
        $parameters = array();
      } else if (!is_array($parameters)) {
        error_log("Parameters must be an array\n");
        return false;
      }
      foreach ($parameters as $key => &$val) {
        // encoding to JSON array parameters, for example reply_markup
        if (!is_numeric($val) && !is_string($val)) {
          $val = json_encode($val);
        }
      }
      $url = API_URL.$method.'?'.http_build_query($parameters);
      $handle = curl_init($url);
      curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($handle, CURLOPT_TIMEOUT, 60);
      return exec_curl_request($handle);
    }
    function apiRequestJson($method, $parameters) {
      if (!is_string($method)) {
        error_log("Method name must be a string\n");
        return false;
      }
      if (!$parameters) {
        $parameters = array();
      } else if (!is_array($parameters)) {
        error_log("Parameters must be an array\n");
        return false;
      }
      $parameters["method"] = $method;
      $handle = curl_init(API_URL);
      curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($handle, CURLOPT_TIMEOUT, 60);
      curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
      curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
      return exec_curl_request($handle);
    }
    function processMessage($message) {
      // process incoming message
      $message_id = $message['message_id'];
      $chat_id = $message['chat']['id'];




      if (isset($message['text'])) {
        // incoming text message
        $text = $message['text'];
        if (strpos($text, "/start") === 0) {
          apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
            'keyboard' => array(array('Hello', 'Hi')),
            'one_time_keyboard' => true,
            'resize_keyboard' => true)));
        }elseif ($text === "Hello" || $text === "Hi") {
          apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
        }elseif ($text === "Салом" || $text === "Salom" || $text === "салом"){
          apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Алейкум салом!'));
        }elseif ($text === "123456"){
          //apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => ''));
          apiRequest("sendMessage", array(
            'chat_id' => $chat_id, 
            "text" => 'Ci vuoi inviare il tuo numero di telefono?', 
            'reply_markup' => array(
                    'keyboard' => array(
                                       array(
                                            array( 
                                              'text'=>"SHOW PHONE",
                                              'request_contact'=>true
                                            )
                                         )
                                  ),
                'one_time_keyboard' => true,
                'resize_keyboard' => true
            )
            ));
        }elseif ($text === "ism"){
          apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => ",kz", 'reply_markup' => array(
            'keyboard' => array(
                                       array(
                                            array( 
                                              'text'=>"SHOW PHONE",
                                              'request_contact'=>true
                                            )
                                         )
                                  ),,
            'one_time_keyboard' => true,
            'resize_keyboard' => true)));


        }elseif (strpos($text, "/stop") === 0) {
          // stop now
        } else {
            //apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $msg_text));
            apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => "Текст не определен"));

          //apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool'));
        }
      }elseif(isset($update["message"]["contact"])) {
        apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => "Контакт"));
      }elseif(isset($update["callback_query"])){
         apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => "Колбэк"));
      }else{
        //$user_phone = $message["message"]["contact"]["phone_number"];
      apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => "Другое"));



        //apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $userName));
    //echo $homepage;
      }

    }

    define('WEBHOOK_URL', 'https://<URL>/index.php');
    if (php_sapi_name() == 'cli') {
      // if run from console, set or delete webhook
      apiRequest('setWebhook', array('url' => isset($argv[1]) && $argv[1] == 'delete' ? '' : WEBHOOK_URL));
      exit;
    }

    $content = file_get_contents("php://input");
    $update = json_decode($content, true);

    if (!$update) {
      // receive wrong update, must not happen
      exit;
    }
    if (isset($update["message"])) {
      processMessage($update["message"]);
    }
    ?>
READ ALSO
WordPress поисковая форма по метабоксам

WordPress поисковая форма по метабоксам

Хочу создать поисковую форму по данным метабоксовПоиск должен выводить на экран нужные посты по нужным метаданным

129
Private Instagram API|ChallengeRequired

Private Instagram API|ChallengeRequired

Пользуюсь приватным АПИ инстаграма

102
Код выдает ошибку Notice: Undefined variable:

Код выдает ошибку Notice: Undefined variable:

При заходе на сайт выдает ошибки:

106