2016-12-11 00:49:40 +00:00
Telegram Plugin
===============
Send alerts to Telegram Bot.

2020-11-20 21:39:32 +00:00
For help, join [](https://slack.alerta.dev)
2016-12-11 00:49:40 +00:00
Installation
------------
Clone the GitHub repo and run:
$ python setup.py install
Or, to install remotely from GitHub run:
$ pip install git+https://github.com/alerta/alerta-contrib.git#subdirectory=plugins/telegram
Note: If Alerta is installed in a python virtual environment then plugins
need to be installed into the same environment for Alerta to dynamically
discover them.
Configuration
-------------
Add `telegram` to the list of enabled `PLUGINS` in `alertad.conf` server
configuration file and set plugin-specific variables either in the
server configuration file or as environment variables.
```python
PLUGINS = ['telegram']
2021-10-18 21:40:49 +00:00
TELEGRAM_TOKEN = '' # Required, default="not set"
TELEGRAM_CHAT_ID = '' # Required, default="not set", specify multiple ID's separated by comma. Get yours in https://t.me/chatid_echo_bot
2019-06-27 20:23:08 +00:00
TELEGRAM_TEMPLATE = '' # default will use hardcoded one (can be a filename to template file)
2018-09-12 13:30:53 +00:00
TELEGRAM_PROXY = '' # default="not set", URL must start from http://, socks5 not supported
TELEGRAM_PROXY_USERNAME = '' # default="not set"
TELEGRAM_PROXY_PASSWORD = '' # default="not set"
2016-12-11 00:49:40 +00:00
```
Create a new Telegram Bot using a Telegram client.
See https://core.telegram.org/bots#creating-a-new-bot
2016-12-16 00:05:34 +00:00
To `ack` , `close` or `blackout` an alert from the Telegram client set
the webhook URL to your Alerta API Telegram endpoint (must be HTTPS):
```python
2021-10-18 21:40:49 +00:00
TELEGRAM_WEBHOOK_URL = 'https://alerta.example.com/api/webhooks/telegram?api-key=[APIKEY_FROM_ALERTA]' # You can get one in https://alerta.example.com/keys
2016-12-16 00:05:34 +00:00
BLACKOUT_DURATION = 86400 # default=3600 ie. 1 hour
```
2019-07-12 15:13:24 +00:00
To enable/disable sound on notifications([disable_notification](https://core.telegram.org/bots/api#sendmessage)) use `TELEGRAM_SOUND_NOTIFICATION_SEVERITY` :
```python
# Only severities 'security', 'critical' and 'major' have disable_notification=False
# all other severities have disable_notification=True
TELEGRAM_SOUND_NOTIFICATION_SEVERITY = ['security', 'critical', 'major']
```
If `TELEGRAM_SOUND_NOTIFICATION_SEVERITY` is unset then all telegram
notifications have [disable_notification ](https://core.telegram.org/bots/api#sendmessage )=False
2021-04-19 19:27:18 +00:00
To disable notifications on severity on your chose use `TELEGRAM_DISABLE_NOTIFICATION_SEVERITY` :
```python
# Severity `warning` is disabled notification in telegram
# all other severeties have notifications
TELEGRAM_DISABLE_NOTIFICATION_SEVERITY = ['warning']
```
2019-07-12 15:13:24 +00:00
Templating
2017-11-10 14:10:35 +00:00
----------
2019-06-27 20:23:08 +00:00
There can be defined template to send data to telegram it have to be defined in `TELEGRAM_TEMPLATE` . `TELEGRAM_TEMPLATE` can be a filename pointing to [Jinja2 ](http://jinja.pocoo.org/docs/2.10/ ) template file.
2017-11-10 14:10:35 +00:00
Template have to be writen in [Jinja2 ](http://jinja.pocoo.org/docs/2.10/ )
2019-07-12 15:13:24 +00:00
Alert data will be passed to it as context. So you can modify tg message as you wish. Example can be found in [Explorer ](http://explorer.alerta.io/#/send )
2017-11-10 14:10:35 +00:00
2016-12-11 00:49:40 +00:00
Troubleshooting
---------------
Restart Alerta API and confirm that the plugin has been loaded and enabled.
Set `DEBUG=True` in the `alertad.conf` configuration file and look for log
entries similar to below:
```
--------------------------------------------------------------------------------
DEBUG in __init__ [/Users/nsatterl/.virtualenvs/telegram/lib/python2.7/site-packages/alerta/plugins/__init__.py:46]:
Server plug-in 'telegram' found.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DEBUG in alerta_telegram [build/bdist.macosx-10.12-x86_64/egg/alerta_telegram.py:21]:
Telegram: {u'username': u'alertaio_bot', u'first_name': u'alerta-bot', u'id': 264434259}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
INFO in __init__ [/Users/nsatterl/.virtualenvs/telegram/lib/python2.7/site-packages/alerta/plugins/__init__.py:53]:
Server plug-in 'telegram' enabled.
--------------------------------------------------------------------------------
```
References
----------
* Telegram Bots Intro: https://core.telegram.org/bots
2016-12-14 22:22:52 +00:00
* Telepot Python Client: http://telepot.readthedocs.io/en/latest/
2017-11-10 14:10:35 +00:00
* Jinja2: http://jinja.pocoo.org/docs/2.10/
2016-12-11 00:49:40 +00:00
License
-------
Copyright (c) 2016 Nick Satterly. Available under the MIT License.