= 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", 3, "lipa.log"); 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", 3, "lipa.log"); } $response = $response['result']; } return $response; } function apiRequest($method, $parameters) { if (!is_string($method)) { error_log("Method name must be a string\n", 3, "lipa.log"); return false; } if (!$parameters) { $parameters = array(); } else if (!is_array($parameters)) { error_log("Parameters must be an array\n", 3, "lipa.log"); 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", 3, "lipa.log"); return false; } if (!$parameters) { $parameters = array(); } else if (!is_array($parameters)) { error_log("Parameters must be an array\n", 3, "lipa.log"); 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))); } else if ($text === "Hello" || $text === "Hi") { apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you')); } else if (strpos($text, "/stop") === 0) { // stop now } else { apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool')); } } else { apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'I understand only text messages')); } } function ExceptionHandler($exception) { error_log("Exception: " . $exception->getMessage() . "\n", 3, "lipa.log"); exit; } set_exception_handler('ExceptionHandler'); if(isset($_GET['register']) && $_GET['register'] == 1) { define('WEBHOOK_URL', 'https://lipa.kvewijk.nl/telegram/bot.php'); apiRequest('setWebhook', array('url' => WEBHOOK_URL)); echo "Success!"; exit; } //$content = file_get_contents("php://input"); $content = $_POST; error_log("Raw input: " . $content . "\n", 3, "lipa.log"); $update = json_decode($content, true); error_log("This page has been called!\n", 3, "lipa.log"); if (!$update) { error_log("Received wrong update!\n", 3, "lipa.log"); echo "hello"; exit; } if (isset($update["message"])) { error_log("Received message: " . $update['message'] . "\n", 3, "lipa.log"); processMessage($update["message"]); }