Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions front/notificationwebsocketsetting.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
}
Expand Down
16 changes: 12 additions & 4 deletions inc/bot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

require GLPI_ROOT . '/plugins/telegrambot/vendor/autoload.php';
use Longman\TelegramBot\Request;
use GuzzleHttp\Client;

class PluginTelegrambotBot {

Expand All @@ -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';

Expand Down Expand Up @@ -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() {
Expand Down
52 changes: 48 additions & 4 deletions inc/notificationeventwebsocket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static public function getTargetField(&$data) {
}

static public function canCron() {
return false;
return true;
}

static public function getAdminData() {
Expand All @@ -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);
}
}
33 changes: 29 additions & 4 deletions inc/notificationwebsocket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
25 changes: 24 additions & 1 deletion inc/notificationwebsocketsetting.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<form action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "' method='post'>";
$out .= "<div>";
Expand All @@ -68,6 +71,26 @@ function showFormConfig($options = []) {
$out .= "<input type='text' name='bot_username' value='" . $bot_username . "' style='width: 100%'>";
$out .= "</td><td colspan='2'>&nbsp;</td></tr>";

$out .= "<tr class='tab_bg_2'>";
$out .= "<td> " . __('Number of attempts to send messages from query') . "</td><td>";
$out .= "<input type='text' name='errorcount_limit' value='" . $errorcount_limit . "' style='width: 100%'>";
$out .= "</td><td colspan='2'>&nbsp;</td></tr>";

$out .= "<tr class='tab_bg_2'>";
$out .= "<td> " . __('Delete message older than (min))') . "</td><td>";
$out .= "<input type='text' name='store_limit' value='" . $store_limit . "' style='width: 100%'>";
$out .= "</td><td colspan='2'>&nbsp;</td></tr>";

$out .= "<tr class='tab_bg_2'>";
$out .= "<td> " . __('Base URI(telegram)') . "</td><td>";
$out .= "<input type='text' name='base_uri' value='" . $base_uri . "' style='width: 100%'>";
$out .= "</td><td colspan='2'>&nbsp;</td></tr>";

$out .= "<tr class='tab_bg_2'>";
$out .= "<td> " . __('Delay of http request(sec)') . "</td><td>";
$out .= "<input type='text' name='http_delay' value='" . $http_delay . "' style='width: 100%'>";
$out .= "</td><td colspan='2'>&nbsp;</td></tr>";

echo $out;
$this->showFormButtons($options);
}
Expand Down
4 changes: 3 additions & 1 deletion inc/user.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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'));
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,7 +68,12 @@ function plugin_version_telegrambot() {
'author' => '<a href="http://trulymanager.com" target="_blank">Truly Systems</a>',
'license' => 'GPLv2+',
'homepage' => 'https://github.com/pluginsGLPI/telegrambot',
'minGlpiVersion' => '9.2'
'requirements' => [
'glpi' => [
'min' => PLUGIN_TELEGRAMBOT_MIN_GLPI,
'max' => PLUGIN_TELEGRAMBOT_MAX_GLPI,
]
]
];
}

Expand Down