123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Created by IntelliJ IDEA.
- * User: solu
- * Date: 2019/3/26
- * Time: 5:38 PM
- */
- class Telegram {
- /**
- * @param $message
- * @throws Exception
- */
- public static 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) {
- self::apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
- 'keyboard' => array(array('Hello', 'Hi')),
- 'one_time_keyboard' => true,
- 'resize_keyboard' => true)));
- } else if ($text === "Hello" || $text === "Hi") {
- self::apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $message['text']));
- } else {
- self::apiRequest("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool'));
- }
- } else {
- self::apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'I understand only text messages'));
- }
- }
- /**
- * @param $method
- * @param $parameters
- * @return bool|mixed
- * @throws Exception
- * @return mixed
- */
- public static function apiRequestJson($method, $parameters) {
- if (!is_string($method)) {
- throw new Exception("Method name must be a string\n");
- }
- if (!$parameters) {
- $parameters = array();
- } else if (!is_array($parameters)) {
- throw new Exception("Parameters must be an array\n");
- }
- $parameters["method"] = $method;
- $handle = curl_init(TELEGRAM_API_URL);
- curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($handle, CURLOPT_TIMEOUT, 60);
- curl_setopt($handle, CURLOPT_POST, true);
- curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
- curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
- return self::exec_curl_request($handle);
- }
- /**
- * @param $method
- * @param $parameters
- * @return mixed
- * @throws Exception
- */
- public static function apiRequest($method, $parameters) {
- if (!is_string($method)) {
- throw new Exception("Method name must be a string\n");
- }
- if (!$parameters) {
- $parameters = array();
- } else if (!is_array($parameters)) {
- throw new Exception("Parameters must be an array\n");
- }
- 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 = TELEGRAM_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 self::exec_curl_request($handle);
- }
- /**
- * @param $handle
- * @return bool|mixed
- * @throws Exception
- * @return mixed
- */
- public static function exec_curl_request($handle) {
- $response = curl_exec($handle);
- if ($response === false) {
- $errno = curl_errno($handle);
- $error = curl_error($handle);
- curl_close($handle);
- throw new Exception("Curl returned error $errno: $error\n");
- }
- $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);
- throw new Exception("Request has failed with error {$response['error_code']}: {$response['description']}\n");
- } else {
- $response = json_decode($response, true);
- if (isset($response['description'])) {
- throw new Exception("Request was successful: {$response['description']}\n");
- }
- $response = $response['result'];
- }
- return $response;
- }
- }
|