$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; } }