diff --git a/inc/bot.class.php b/inc/bot.class.php index 8410aeb..8fb6d99 100644 --- a/inc/bot.class.php +++ b/inc/bot.class.php @@ -40,9 +40,22 @@ static public function setConfig($key, $value) { } static public function sendMessage($to, $content) { - $chat_id = self::getChatID($to); - $telegram = self::getTelegramInstance(); - $result = Request::sendMessage(['chat_id' => $chat_id, 'text' => $content]); + global $DB; + + $test_conn = self::isSiteAvailable("https://api.telegram.org/", 2) ? true : false; + if (!$test_conn) { + $logfile = GLPI_LOG_DIR."/telegrambot.log"; + if (!file_exists($logfile)) { + $newfile = fopen($logfile, 'w+'); + fclose($newfile); + } + error_log(date("Y-m-d H:i:s")." - ERROR: Telegram API is unavailable now!\n", 3, $logfile); + return; + } else { + $chat_id = self::getChatID($to); + $telegram = self::getTelegramInstance(); + $result = Request::sendMessage(['chat_id' => $chat_id, 'text' => $content]); + } } static public function getUpdates() { @@ -102,4 +115,19 @@ static private function getDBCredentials() { ); } + static private function isSiteAvailable($url, $timeout) { + if(!filter_var($url, FILTER_VALIDATE_URL)){ + return false; + } + + $curlInit = curl_init($url); + curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,$timeout); + curl_setopt($curlInit,CURLOPT_HEADER,true); + curl_setopt($curlInit,CURLOPT_NOBODY,true); + curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); + $response = curl_exec($curlInit); + curl_close($curlInit); + return $response ? true : false; + } + }