diff --git a/front/notificationwebsocketsetting.form.php b/front/notificationwebsocketsetting.form.php index d1579d4..2a71d77 100644 --- a/front/notificationwebsocketsetting.form.php +++ b/front/notificationwebsocketsetting.form.php @@ -12,6 +12,10 @@ } else if (!empty($_POST['update'])) { PluginTelegrambotBot::setConfig('token', $_POST['token']); PluginTelegrambotBot::setConfig('bot_username', $_POST['bot_username']); +PluginTelegrambotBot::setConfig('messagecount', $_POST['errorcount_limit']); +PluginTelegrambotBot::setConfig('minutes_to_store_mess', $_POST['store_limit']); +PluginTelegrambotBot::setConfig('base_uri', $_POST['base_uri']); +PluginTelegrambotBot::setConfig('http_delay', $_POST['http_delay']); Html::back(); } diff --git a/hook.php b/hook.php index c8c921c..162a1ff 100644 --- a/hook.php +++ b/hook.php @@ -37,7 +37,7 @@ function plugin_telegrambot_install() { $DB->runFile(GLPI_ROOT . '/plugins/telegrambot/db/install.sql'); Config::setConfigurationValues('core', ['notifications_websocket' => 0]); - Config::setConfigurationValues('plugin:telegrambot', ['token' => '', 'bot_username' => '']); + Config::setConfigurationValues('plugin:telegrambot', ['token' => '', 'bot_username' => '', 'http_delay' => 2,'base_uri' => 'https://api.telegram.org','messagecount' => 20,'minutes_to_store_mess'=>20]); CronTask::register( 'PluginTelegrambotCron', @@ -60,7 +60,7 @@ function plugin_telegrambot_uninstall() { $config = new Config(); $config->deleteConfigurationValues('core', ['notifications_websocket']); - $config->deleteConfigurationValues('plugin:telegrambot', ['token', 'bot_username']); + $config->deleteConfigurationValues('plugin:telegrambot', ['token', 'bot_username', 'http_delay','base_uri','messagecount']); return true; } diff --git a/inc/bot.class.php b/inc/bot.class.php index 8410aeb..16650b3 100644 --- a/inc/bot.class.php +++ b/inc/bot.class.php @@ -28,6 +28,7 @@ require GLPI_ROOT . '/plugins/telegrambot/vendor/autoload.php'; use Longman\TelegramBot\Request; +use GuzzleHttp\Client; class PluginTelegrambotBot { @@ -41,10 +42,16 @@ 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]); +// $telegram = self::getTelegramInstance(); +// Request::setClient (new Client(['base_uri' => 'https://api.telegram.org','timeout' => 2])); + Request::sendMessage(['chat_id' => $chat_id, 'text' => $content,'parse_mode'=>'Markdown']); } + static public function setTelegramBot() { + $telegram = self::getTelegramInstance(); + Request::setClient (new Client(['base_uri' => self::getConfig('base_uri'),'timeout' => self::getConfig('http_delay')])); + } + static public function getUpdates() { $response = 'ok'; @@ -87,8 +94,9 @@ static public function getChatID($user_id) { static private function getTelegramInstance() { $bot_api_key = self::getConfig('token'); $bot_username = self::getConfig('bot_username'); - - return new Longman\TelegramBot\Telegram($bot_api_key, $bot_username); + $telegrambotLongman = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username); + + return $telegrambotLongman; } static private function getDBCredentials() { diff --git a/inc/notificationeventwebsocket.class.php b/inc/notificationeventwebsocket.class.php index 5b80dfe..4f4bfdf 100644 --- a/inc/notificationeventwebsocket.class.php +++ b/inc/notificationeventwebsocket.class.php @@ -45,7 +45,7 @@ static public function getTargetField(&$data) { } static public function canCron() { - return false; + return true; } static public function getAdminData() { @@ -57,8 +57,52 @@ static public function getEntityAdminsData($entity) { } static public function send(array $data) { - Toolbox::logDebug(__METHOD__ . ' should not be called!'); - return false; + global $CFG_GLPI, $DB; + $processed = []; + $errorcount=0; + +// $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")."INFO: Starting sending messages to telegramm...\n", 3, $logfile); + PluginTelegrambotBot::setTelegramBot(); + $errorcount_limit = PluginTelegrambotBot::getConfig('messagecount'); + $store_limit = PluginTelegrambotBot::getConfig('minutes_to_store_mess'); + foreach ($data as $row) { + $current = new QueuedNotification(); + $current->getFromResultSet($row); + $to = $current->getField('recipient'); + $content = $current->getField('body_text'); + $temp = $current->getField('name'); + try { + PluginTelegrambotBot::sendMessage($to, $content); + $processed[] = $current->getID(); + $current->update(['id' => $current->fields['id'], + 'sent_time' => $_SESSION['glpi_currenttime']]); + $current->delete(['id' => $current->fields['id']]); + } catch (Exception $e) { + $create_time= strtotime($current->getField('create_time')); + $current_time= strtotime($_SESSION['glpi_currenttime']); + $minutes = round(abs($current_time - $create_time)/60,0); + if ($minutes>$store_limit) { + $current->update(['id' => $current->fields['id']]); + $current->delete(['id' => $current->fields['id']]); + } + $errorcount+=1; + if ($errorcount>$errorcount_limit){ + $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: ".$e->getMessage()."\n", 3, $logfile); + return 0; + } + } + // error_log(date("Y-m-d H:i:s")."INFO: End sending messages to telegramm.\n", 3, $logfile); } - + return count($processed); + } } diff --git a/inc/notificationwebsocket.class.php b/inc/notificationwebsocket.class.php index 27d1660..ffac40e 100644 --- a/inc/notificationwebsocket.class.php +++ b/inc/notificationwebsocket.class.php @@ -37,11 +37,36 @@ static function testNotification() { } function sendNotification($options=array()) { - $to = $options['to']; - $content = $options['content_text']; + + $data = array(); + $data['itemtype'] = $options['_itemtype']; + $data['items_id'] = $options['_items_id']; + $data['notificationtemplates_id'] = $options['_notificationtemplates_id']; + $data['entities_id'] = $options['_entities_id']; + $data['sendername'] = $options['fromname']; + $data['name'] = $options['subject']; + $data['body_text'] = $options['content_text']; + $data['recipient'] = $options['to']; + if (!empty($options['content_html'])) { + $data['body_html'] = $options['content_html']; + } + + $data['mode'] = Notification_NotificationTemplate::MODE_WEBSOCKET; + + $mailqueue = new QueuedNotification(); + + if (!$mailqueue->add(Toolbox::addslashes_deep($data))) { + Session::addMessageAfterRedirect(__('Error inserting Telegram notification to queue', 'Telegrambot'), true, ERROR); + return false; + } else { + //TRANS to be written in logs %1$s is the to email / %2$s is the subject of the mail + Toolbox::logInFile("notification", + sprintf(__('%1$s: %2$s'), + sprintf(__('An Telegram notification to %s was added to queue', 'Telegrambot'), + $options['to']), + $options['subject']."\n")); + } - PluginTelegrambotBot::sendMessage($to, $content); - return true; } } diff --git a/inc/notificationwebsocketsetting.class.php b/inc/notificationwebsocketsetting.class.php index 3cfaee5..62c8014 100644 --- a/inc/notificationwebsocketsetting.class.php +++ b/inc/notificationwebsocketsetting.class.php @@ -49,9 +49,12 @@ static public function getMode() { function showFormConfig($options = []) { global $CFG_GLPI; - + $errorcount_limit = PluginTelegrambotBot::getConfig('messagecount'); + $store_limit = PluginTelegrambotBot::getConfig('minutes_to_store_mess'); $bot_token = PluginTelegrambotBot::getConfig('token'); $bot_username = PluginTelegrambotBot::getConfig('bot_username'); + $base_uri = PluginTelegrambotBot::getConfig('base_uri'); + $http_delay = PluginTelegrambotBot::getConfig('http_delay'); $out = "