= 500) { // do not want 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) { apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Hi, dnsbot lets you do basic DNS lookups like get host name, get reverse DNS and a MX lookup.')); } else if (strpos($text, "/mx") === 0) { $domain = explode(" ", $text)[1]; //The command is written /mx example.com so extract example.com $hosts = array(); getmxrr($domain, $hosts); //Get MX record command, returns a array with the domain names if($hosts != null) { error_log("command from telegram: \n"); //Log to /var/log/apache2/error.log error_log($hosts); foreach($hosts as $host) { //Loop throught the array $result = $result . $host . ' ' . gethostbyname($host) . chr(10); //add domain name and IP address to the string. } } else { $result = "unknown host or server error"; } apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $result)); //Send the message back to the user } else if (strpos($text, "/getip") === 0) { $ip = explode(" ", $text)[1]; //get the domain error_log("command from telegram: \n"); $result = gethostbyname($ip); if($result != null) //Check if there is a result otherwise print a error. Either the user put in garbage or the domain dont exist { error_log($result); } else { $result = "unknown host or server error"; } apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $result)); } else if (strpos($text, "/gethost") === 0) { $ip = explode(" ", $text)[1]; error_log("command from telegram: \n"); $result = gethostbyaddr($ip);; error_log($result); if($result != null) { error_log($result); } else { $result = "unknown host or server error"; } apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $result)); } else if (strpos($text, "/stop") === 0) { // stop now } else { apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Unknown command!')); //User send a command that we dont understand } } else { apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'I understand only text messages')); //User send a file or something else... } } $content = file_get_contents("php://input"); $update = json_decode($content, true); if (php_sapi_name() == 'cli') { // if run from console, set or delete webhook echo 'test'; exit; } if (!$update) { // receive wrong update, must not happen exit; } if (isset($update["message"])) { processMessage($update["message"]); }