0% found this document useful (0 votes)
40 views4 pages

Glpi Telegram Full

The document outlines the configuration and functionality of a Telegram Notifier plugin for GLPI, which sends notifications when new tickets are created. It includes PHP scripts for managing the plugin's settings, sending notifications, and defining the plugin's metadata. The plugin requires a bot token and chat ID for operation and provides a form for users to input these configurations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Glpi Telegram Full

The document outlines the configuration and functionality of a Telegram Notifier plugin for GLPI, which sends notifications when new tickets are created. It includes PHP scripts for managing the plugin's settings, sending notifications, and defining the plugin's metadata. The plugin requires a bot token and chat ID for operation and provides a form for users to input these configurations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// /front/config.

php
<?php
include('../../../inc/includes.php');

$plugin = new Plugin();


if (!$plugin->isActivated('telegramnotifier')) {
Html::displayNotFoundError();
}

Html::header('Telegram Notifier', $_SERVER['PHP_SELF'], 'config');


$config = new PluginTelegramnotifierConfig();

if (isset($_POST['update'])) {
$config->update($_POST);
Html::back();
}

$config->display(['id' => 1]);


Html::footer();

// /front/config.form.php
<?php
include('../../../inc/includes.php');

$plugin = new Plugin();


if (!$plugin->isActivated('telegramnotifier')) {
Html::displayNotFoundError();
}

$config = new PluginTelegramnotifierConfig();


if (isset($_POST['update'])) {
$config->update($_POST);
Html::back();
}

Html::redirect($CFG_GLPI['root_doc'] .
'/plugins/telegramnotifier/front/config.php');

// /inc/config.class.php
<?php
class PluginTelegramnotifierConfig extends CommonDBTM {
static $rightname = 'config';

function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {


return 'Telegram Notifier';
}

function showConfigForm() {
global $CFG_GLPI;

$config = self::getConfig();

echo "<form name='form' action='" . $CFG_GLPI['root_doc'] .


"/plugins/telegramnotifier/front/config.form.php' method='post'>";
echo "<div class='center' id='tabsbody'>";
echo "<table class='tab_cadre_fixe'>";

echo "<tr class='tab_bg_1'>";


echo "<th colspan='4'>" . __('Configurações do Telegram',
'telegramnotifier') . "</th>";
echo "</tr>";

echo "<tr class='tab_bg_2'>";


echo "<td>" . __('Bot Token', 'telegramnotifier') . "</td>";
echo "<td colspan='3'>";
Html::autocompletionTextField($this, 'bot_token', ['value' =>
$config['bot_token']]);
echo "</td></tr>";

echo "<tr class='tab_bg_2'>";


echo "<td>" . __('Chat ID', 'telegramnotifier') . "</td>";
echo "<td colspan='3'>";
Html::autocompletionTextField($this, 'chat_id', ['value' =>
$config['chat_id']]);
echo "</td></tr>";

echo "<tr class='tab_bg_2'>";


echo "<td colspan='4' class='center'>";
echo "<input type='submit' name='update' class='submit' value='" .
__('Salvar', 'telegramnotifier') . "'>";
echo "</td></tr>";

echo "</table></div>";
Html::closeForm();
}

static function getConfig() {


$config = new self();
$config->getFromDB(1);

if (!$config->fields) {
return [
'bot_token' => '',
'chat_id' => ''
];
}

return $config->fields;
}
}

// /inc/notifier.class.php
<?php
class PluginTelegramnotifierNotifier {
static function sendNotification($ticket) {
$config = PluginTelegramnotifierConfig::getConfig();

if (empty($config['bot_token']) || empty($config['chat_id'])) {
Toolbox::logError('Configurações do Telegram não definidas');
return false;
}

$message = self::formatMessage($ticket);
return self::sendToTelegram($config['bot_token'], $config['chat_id'],
$message);
}

static private function formatMessage($ticket) {


return sprintf(
"🎫 Novo Chamado #%d\n\nTítulo: %s\nDescrição: %s\nPrioridade: %s\
nCategoria: %s",
$ticket->fields['id'],
$ticket->fields['name'],
Toolbox::substr($ticket->fields['content'], 0, 200) . '...',
Ticket::getPriorityName($ticket->fields['priority']),
Dropdown::getDropdownName('glpi_itilcategories', $ticket-
>fields['itilcategories_id'])
);
}

static private function sendToTelegram($botToken, $chatId, $message) {


$url = "https://api.telegram.org/bot{$botToken}/sendMessage";
$data = [
'chat_id' => $chatId,
'text' => $message,
'parse_mode' => 'HTML'
];

$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data)
];

$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200) {


Toolbox::logError("Erro ao enviar mensagem Telegram: $response");
return false;
}

return true;
}
}

// /locales/pt_BR.php
<?php
$LANG['telegramnotifier'] = [
'config' => 'Configuração do Telegram Notifier',
'bot_token' => 'Token do Bot',
'chat_id' => 'ID do Chat',
'save' => 'Salvar',
'test' => 'Testar Configuração'
];

// /pics/telegram.png
// [Aqui você precisará adicionar um ícone 32x32 para o plugin]

// XML de definição do plugin


<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>Telegram Notifier</name>
<key>telegramnotifier</key>
<state>stable</state>
<logo>pics/telegram.png</logo>
<description>
<short>
<pt_BR>Notificações do GLPI via Telegram</pt_BR>
</short>
<long>
<pt_BR>Plugin para envio de notificações do GLPI via Telegram quando
novos chamados são abertos.</pt_BR>
</long>
</description>
<homepage>https://github.com/seu-usuario/glpi-telegram-notifier</homepage>
<download>https://github.com/seu-usuario/glpi-telegram-notifier/releases</
download>
<issues>https://github.com/seu-usuario/glpi-telegram-notifier/issues</issues>
<readme>https://github.com/seu-usuario/glpi-telegram-notifier/blob/master/
README.md</readme>
<authors>
<author>Seu Nome</author>
</authors>
<versions>
<version>
<num>1.0.0</num>
<compatibility>10.0.x</compatibility>
</version>
</versions>
<langs>
<lang>pt_BR</lang>
</langs>
<license>GPL v3+</license>
<tags>
<pt_BR>
<tag>Notificação</tag>
<tag>Telegram</tag>
</pt_BR>
</tags>
</root>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy