From d6a009a2130ddfbf688adc1b0ff3155b8ce79d85 Mon Sep 17 00:00:00 2001 From: yakirillko Date: Tue, 22 Jun 2021 14:24:22 +0300 Subject: [PATCH] hanges to be committed: Add support for Glpi 9.5.4+ Fix Issue Undefined index: telegram_username Add support of Glpi notification model (all notification pushed to notification query) Add come configuration properties --- front/notificationwebsocketsetting.form.php | 4 ++ hook.php | 4 +- inc/bot.class.php | 16 +++++-- inc/notificationeventwebsocket.class.php | 52 +++++++++++++++++++-- inc/notificationwebsocket.class.php | 33 +++++++++++-- inc/notificationwebsocketsetting.class.php | 25 +++++++++- inc/user.class.php | 4 +- setup.php | 11 ++++- 8 files changed, 131 insertions(+), 18 deletions(-) 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 = "
"; $out .= "
"; @@ -68,6 +71,26 @@ function showFormConfig($options = []) { $out .= ""; $out .= " "; + $out .= ""; + $out .= " " . __('Number of attempts to send messages from query') . ""; + $out .= ""; + $out .= " "; + + $out .= ""; + $out .= " " . __('Delete message older than (min))') . ""; + $out .= ""; + $out .= " "; + + $out .= ""; + $out .= " " . __('Base URI(telegram)') . ""; + $out .= ""; + $out .= " "; + + $out .= ""; + $out .= " " . __('Delay of http request(sec)') . ""; + $out .= ""; + $out .= " "; + echo $out; $this->showFormButtons($options); } diff --git a/inc/user.class.php b/inc/user.class.php index efe7cd2..0405e4f 100644 --- a/inc/user.class.php +++ b/inc/user.class.php @@ -49,7 +49,7 @@ static public function showUsernameField($item) { } static public function item_add_user(User $item) { - if ($item->input['telegram_username']) { + if (isset($item->input['telegram_username'])) { $user = new self; $user->fields['id'] = $item->fields['id']; $user->fields['username'] = $item->input['telegram_username']; @@ -64,8 +64,10 @@ static public function item_update_user(User $item) { if ($user->isNewItem()) { self::item_add_user($item); } else { + if (isset($item->input['telegram_username'])) { $user->fields['username'] = $item->input['telegram_username']; $user->updateInDB(array('username')); +} } } diff --git a/setup.php b/setup.php index 3f49d3b..4917355 100644 --- a/setup.php +++ b/setup.php @@ -26,7 +26,9 @@ -------------------------------------------------------------------------- */ -define('PLUGIN_TELEGRAMBOT_VERSION', '2.0.0'); +define('PLUGIN_TELEGRAMBOT_VERSION', '3.0.0'); +define('PLUGIN_TELEGRAMBOT_MIN_GLPI','9.5.0'); +define('PLUGIN_TELEGRAMBOT_MAX_GLPI','9.6'); /** * Init hooks of the plugin. @@ -66,7 +68,12 @@ function plugin_version_telegrambot() { 'author' => 'Truly Systems', 'license' => 'GPLv2+', 'homepage' => 'https://github.com/pluginsGLPI/telegrambot', - 'minGlpiVersion' => '9.2' + 'requirements' => [ + 'glpi' => [ + 'min' => PLUGIN_TELEGRAMBOT_MIN_GLPI, + 'max' => PLUGIN_TELEGRAMBOT_MAX_GLPI, + ] + ] ]; }