feature: Added support for sending alerts to Telegram #2114 (#6202)

This commit is contained in:
Neil Lathwood 2017-03-22 09:02:24 +00:00 committed by GitHub
parent 124fee657e
commit f6d8859bb4
4 changed files with 229 additions and 7 deletions

View File

@ -21,6 +21,7 @@ Table of Content:
- [PagerDuty](#transports-pagerduty)
- [Pushover](#transports-pushover)
- [Boxcar](#transports-boxcar)
- [Telegram](#transports-telegram)
- [Pushbullet](#transports-pushbullet)
- [Clickatell](#transports-clickatell)
- [PlaySMS](#transports-playsms)
@ -453,6 +454,26 @@ $config['alert']['transports']['boxcar'][] = array(
```
~~
## <a name="transports-telegram">Telegram</a>
[Using a proxy?](../Support/Configuration.md#proxy-support)
> Thank you to [snis](https://github.com/snis) for these instructions.
1. First you must create a telegram account and add BotFather to you list. To do this click on the following url: https://telegram.me/botfather
2. Generate a new bot with the command "/newbot" BotFather is then asking for a username and a normal name. After that your bot is created and you get a HTTP token. (for more options for your bot type "/help")
3. Add your bot to telegram with the following url: `http://telegram.me/<botname>` and send some text to the bot.
4. Now copy your token code and go to the following page in chrome: `https://api.telegram.org/bot<tokencode>/getUpdates`
5. You see a json code with the message you sent to the bot. Copy the Chat id. In this example that is “-9787468”
`"message":{"message_id":7,"from":"id":656556,"first_name":"Joo","last_name":"Doo","username":"JohnDoo"},"chat":{"id":-9787468,"title":"Telegram Group"},"date":1435216924,"text":"Hi"}}]}`
6. Now create a new "Telegram transport" in LibreNMS (Global Settings -> Alerting Settings -> Telegram transport).
Click on 'Add Telegram config' and put your chat id and token into the relevant box.
## <a name="transports-pushbullet">Pushbullet</a>
[Using a proxy?](../Support/Configuration.md#proxy-support)

View File

@ -37,7 +37,7 @@ $config_token = mres($_POST['config_token']);
$status = 'error';
$message = 'Error with config';
if ($action == 'remove' || $action == 'remove-slack' || $action == 'remove-rocket' || $action == 'remove-hipchat' || $action == 'remove-pushover' || $action == 'remove-boxcar' || $action == 'remove-clickatell' || $action == 'remove-playsms' || $action == 'remove-smseagle') {
if ($action == 'remove' || preg_match('/^remove-.*$/', $action)) {
$config_id = mres($_POST['config_id']);
if (empty($config_id)) {
$message = 'No config id passed';
@ -53,6 +53,8 @@ if ($action == 'remove' || $action == 'remove-slack' || $action == 'remove-rocke
dbDelete('config', "`config_name` LIKE 'alert.transports.pushover.$config_id.%'");
} elseif ($action == 'remove-boxcar') {
dbDelete('config', "`config_name` LIKE 'alert.transports.boxcar.$config_id.%'");
} elseif ($action == 'remove-telegram') {
dbDelete('config', "`config_name` LIKE 'alert.transports.telegram.$config_id.%'");
} elseif ($action == 'remove-clickatell') {
dbDelete('config', "`config_name` LIKE 'alert.transports.clickatell.$config_id.%'");
} elseif ($action == 'remove-playsms') {
@ -169,6 +171,20 @@ if ($action == 'remove' || $action == 'remove-slack' || $action == 'remove-rocke
$message = 'Could not create config item';
}
}
} elseif ($action == 'add-telegram') {
if (empty($config_value)) {
$message = 'No Telegram chat id provided';
} else {
$config_id = dbInsert(array('config_name' => 'alert.transports.telegram.', 'config_value' => $config_value, 'config_group' => $config_group, 'config_sub_group' => $config_sub_group, 'config_default' => $config_value, 'config_descr' => 'Telegram Transport'), 'config');
if ($config_id > 0) {
dbUpdate(array('config_name' => 'alert.transports.telegram.'.$config_id.'.chat_id'), 'config', 'config_id=?', array($config_id));
dbInsert(array('config_name' => 'alert.transports.telegram.'.$config_id.'.token', 'config_value' => $config_extra, 'config_group' => $config_group, 'config_sub_group' => $config_sub_group, 'config_default' => $config_extra, 'config_descr' => 'Telegram token'), 'config');
$status = 'ok';
$message = 'Config item created';
} else {
$message = 'Could not create config item';
}
}
} elseif ($action == 'add-clickatell') {
if (empty($config_value)) {
$message = 'No Clickatell token provided';

View File

@ -183,11 +183,11 @@ $no_refresh = true;
<span class="message"></span>
</div>
<div class="form-group">
<label for="pushover_value">Boxcar Access token</label>
<label for="boxcar_value">Boxcar Access token</label>
<input type="text" class="form-control" name="boxcar_value" id="boxcar_value" placeholder="Enter the Boxcar Access token">
</div>
<div class="form-group">
<label for="pushover_extra">Boxcar options (specify one per line key=value)</label>
<label for="boxcar_extra">Boxcar options (specify one per line key=value)</label>
<textarea class="form-control" name="boxcar_extra" id="boxcar_extra" placeholder="Enter the config options"></textarea>
</div>
</form>
@ -201,6 +201,34 @@ $no_refresh = true;
</div>
<!-- End Boxcar Modal -->
<!-- Telegram Modal -->
<div class="modal fade" id="new-config-telegram" role="dialog" aria-hidden="true" title="Create new config item">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<form role="form" class="new_config_form">
<div class="form-group">
<span class="message"></span>
</div>
<div class="form-group">
<label for="telegram_value">Telegram Chat ID</label>
<input type="text" class="form-control" name="telegram_value" id="telegram_value" placeholder="Enter the Telegram Chat ID">
</div>
<div class="form-group">
<label for="telegram_token">Telegram Token</label>
<input type="text" class="form-control" name="telegram_token" id="telegram_token" placeholder="Enter the Telegram Token">
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-success" id="submit-telegram">Add config</button>
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
</div>
</div>
</div>
</div>
<!-- End Telegram Modal -->
<?php
if (isset($_GET['error'])) {
print_error('We had issues connecting to your Pager Duty account, please try again');
@ -253,10 +281,6 @@ $general_conf = array(
'descr' => 'Default contact',
'type' => 'text',
),
array('name' => 'alert.default_if_none',
'descr' => 'Send mail to default contact if no other contacts are found',
'type' => 'checkbox',
),
array('name' => 'alert.tolerance_window',
'descr' => 'Tolerance window for cron',
'type' => 'text',
@ -868,6 +892,69 @@ echo '<div id="boxcar_appkey_template" class="hide">
</div>
</div>
</div>
<!-- Telegram -->
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#telegram_transport_expand"><i class="fa fa-caret-down"></i> Telegram transport</a> <button name="test-alert" id="test-alert" type="button" data-transport="telegram" class="btn btn-primary btn-xs pull-right">Test transport</button>
</h4>
</div>
<div id="telegram_transport_expand" class="panel-collapse collapse">
<div class="panel-body">
<div class="form-group">
<div class="col-sm-8">
<button class="btn btn-success btn-xs" type="button" name="new_config" id="new_config_item" data-toggle="modal" data-target="#new-config-telegram">Add Telegram config</button>
</div>
</div>';
$telegram_chatids = get_config_like_name('alert.transports.telegram.%.chat_id');
foreach ($telegram_chatids as $index => $chat_id) {
$telegram_token = get_config_by_name('alert.transports.telegram.'.$chat_id['config_id'].'.token');
echo '<div id="'.$chat_id['config_id'].'">
<div class="form-group has-feedback">
<label for="telegram_chat_id" class="col-sm-4 control-label">Telegram Chat ID</label>
<div class="col-sm-4">
<input id="telegram_chat_id" class="form-control" type="text" name="global-config-input" value="'.$chat_id['config_value'].'" data-config_id="'.$chat_id['config_id'].'">
<span class="form-control-feedback">
<i class="fa" aria-hidden="true"></i>
</span>
</div>
</div>
<div class="form-group has-feedback">
<label for="telegram_token" class="col-sm-4 control-label">Telegram token</label>
<div class="col-sm-4">
<input id="telegram_token" class="form-control" type="text" name="telegram_token" value="'.$telegram_token['config_value'].'" data-config_id="'.$chat_id['config_id'].'">
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-danger del-telegram-config" name="del-telegram-call" data-config_id="'.$chat_id['config_id'].'"><i class="fa fa-minus"></i></button>
</div>
</div>
</div>';
}//end foreach
echo '<div id="telegram_chat_id_template" class="hide">
<div class="form-group has-feedback">
<label for="telegram_chat_id" class="col-sm-4 control-label api-method">Telegram Chat ID</label>
<div class="col-sm-4">
<input id="telegram_chat_id" class="form-control" type="text" name="global-config-input" value="" data-config_id="">
<span class="form-control-feedback">
<i class="fa" aria-hidden="true"></i>
</span>
</div>
</div>
<div class="form-group has-feedback">
<label for="telegram_token" class="col-sm-4 control-label">Telegram token</label>
<div class="col-sm-4">
<input id="telegram_token" class="form-control" type="text" name="telegram_token" value="" data-config_id="">
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-danger del-telegram-config" id="del-telegram-call" name="del-telegram-call" data-config_id=""><i class="fa fa-minus"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Pushbullet -->
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
@ -1492,6 +1579,42 @@ echo '
});
});// End Add Boxcar config
// Add Telegram config
itemIndex = 0;
$("button#submit-telegram").click(function(){
var config_value = $('#telegram_value').val();
var config_extra = $('#telegram_token').val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: {type: "config-item", action: 'add-telegram', config_group: "alerting", config_sub_group: "transports", config_extra: config_extra, config_value: config_value},
dataType: "json",
success: function(data){
if (data.status == 'ok') {
itemIndex++;
var $template = $('#telegram_chat_id_template'),
$clone = $template
.clone()
.removeClass('hide')
.attr('id',data.config_id)
.attr('telegram-appkey-index', itemIndex)
.insertBefore($template);
$clone.find('[id="telegram_chat_id"]').attr('data-config_id',data.config_id);
$clone.find('[id="del-telegram-call"]').attr('data-config_id',data.config_id);
$clone.find('[name="global-config-input"]').attr('value', config_value);
$clone.find('[name="telegram_token"]').attr('value', config_extra);
$("#new-config-telegram").modal('hide');
} else {
$("#message").html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function(){
$("#message").html('<div class="alert alert-info">Error creating config item</div>');
}
});
});// End Add Telegram config
// Delete api config
$(document).on('click', 'button[name="del-api-call"]', function(event) {
var config_id = $(this).data('config_id');
@ -1618,6 +1741,28 @@ echo '
});
});// End delete Boxcar config
// Delete Telegram config
$(document).on('click', 'button[name="del-telegram-call"]', function(event) {
var config_id = $(this).data('config_id');
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: {type: "config-item", action: 'remove-telegram', config_id: config_id},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
$("#"+config_id).remove();
} else {
$("#message").html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function () {
$("#message").html('<div class="alert alert-info">An error occurred.</div>');
}
});
});// End delete Telegram config
$( 'select[name="global-config-select"]').change(function(event) {
event.preventDefault();
var $this = $(this);

View File

@ -0,0 +1,40 @@
/**
* transport-telegram.inc.php
*
* LibreNMS Telegram alerting transport
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Neil Lathwood
* @author Neil Lathwood <neil@lathwood.co.uk>
*/
foreach ($opts as $chat_id => $data) {
$curl = curl_init();
set_curl_proxy($curl);
$text = urlencode($obj['msg']);
curl_setopt($curl, CURLOPT_URL, ("https://api.telegram.org/{$data['token']}/sendMessage?chat_id={$data['chat_id']}&text=$text") );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if( $code != 200 ) {
var_dump("API '$host' returned Error"); //FIXME: propper debuging
var_dump("Params: ".$api); //FIXME: propper debuging
var_dump("Return: ".$ret); //FIXME: propper debuging
return 'HTTP Status code '.$code;
}
}
return true;