Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions inc/bot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}

}