Nagios notifications via Telegram

This post shows you how to use Telegram for Nagios notifications. First create a Telegram Bot by talking to the BotFather. The Telegram Bot will be the sender of the Nagios alerts.


You’ll receive an API token that also includes the UserID of the Bot:

  • Token: 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw
  • UserID: 200194008

Download the script that will send the alerts via Telegram:

wget -O /usr/local/bin/
chmod 755 /usr/local/bin/

This is the configuration you need in Nagios (of course replace the token with your own):

# commands to send host/service notifications
define command {
  command_name     notify-host-by-telegram
  command_line     /usr/local/bin/ --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$"
define command {
  command_name     notify-service-by-telegram
  command_line     /usr/local/bin/ --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --servicestate "$SERVICESTATE$" --hostname "$HOSTNAME$" --servicedesc "$SERVICEDESC$" --output "$SERVICEOUTPUT$"

# 2 example contact definitions
define contact {
  contact_name                    John Doe
  pager                           12345678
  service_notification_commands   notify-service-by-telegram
  host_notification_commands      notify-host-by-telegram
define contact {
  contact_name                    Telegram Group Chat
  pager                           -23456789
  service_notification_commands   notify-service-by-telegram
  host_notification_commands      notify-host-by-telegram

The Telegram Nagios plugin is able to send alerts to a single contact or to a group chat. As you can see Telegram GroupIDs are negative numbers.

How to get your UserID or GroupID?

Download and install this Telegram CLI: The CLI makes it easier to discover your UserID and GroupIDs.

$ telegram-cli
> get_self
User John Doe @johndoe (#12345678):
        phone: XXXXXXXXXXX
        offline (was online [2016/03/15 11:57:46])

There is your UserID (#12345678). First start a conversation with the Bot you just created to be able to receive messages (Nagios alerts) from the Bot and to be able to invite it to a Telegram group chat.

To receive Nagios alerts in a Telegram group chat, create a group chat and invite the Bot. You need at least 2 other users in the group.

$ telegram-cli
> create_group_chat "Nagios Alerts" user#200194008 user#12345678 user#33333333
[21:28]  Nagios Alerts John Doe created chat Nagios Alerts. 3 users

> chat_info Nagios_Alerts
Chat Nagios Alerts updated photo admin members
Chat Nagios Alerts (id 23456789) members:
                Nagios Bot invited by John Doe at [2016/03/08 21:28:59]
                John Doe invited by John Doe at [2016/03/08 21:28:59] admin

There is the GroupID (id 23456789) of the Nagios Alerts group chat which needs to be configured in the Nagios configuration as a negative number (-23456789).

Let’s send some test messages! --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "-23456789" --servicestate "OK" --hostname "hostname.domain.tld" --servicedesc "load" --output "OK - load average: 0.02 0.01 0.01" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "-23456789" --servicestate "WARNING" --hostname "hostname.domain.tld" --servicedesc "load" --output "WARNING - load average: 3.48 4.19 2.74" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type service --contact "-23456789" --servicestate "CRITICAL" --hostname "hostname.domain.tld" --servicedesc "load" --output "CRITICAL - load average: 233.29 154.35 15.05" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "UNREACHABLE" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "Network Unreachable (hostname.domain.tld)" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "DOWN" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "PING CRITICAL - Packet loss = 100%" --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "-23456789" --hoststate "UP" --hostname "hostname.domain.tld" --hostaddress "2001:DB8::1" --output "PING OK - Packet loss = 0%, RTA = 3.74 ms

And here is the result sunglasses


31 thoughts on “Nagios notifications via Telegram

  1. John

    Hi , if this codes still working at nagios xi? or anyone kindly help to intergrate nagios xi to telegram.
    Thanks and regards..

  2. Robert

    Thank you for this tutorial!

    I have problem in this part:

    define command {
    command_name notify-host-by-telegram
    command_line /usr/local/bin/ --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$"

    I made the changes as my bot but always returns me this error:

    Traceback (most recent call last):
    File "/usr/local/bin/", line 74, in
    File "/usr/local/bin/", line 66, in main
    user_id = int(
    ValueError: invalid literal for int() with base 10: '$'

    Running the tests at the end of your tutorial works perfectly. If I modify the $ CONTACTPAGER $ for the group id, I get in the telegram the hostname of my server …

    linux01 $ ($): $

    What might be happening?

    Thank you in advance!

  3. pommi Post author

    @John: I’m sorry. I don’t know about Nagios XI.
    @Robert: Did you set the “pager” for the contact correctly in your Nagios config and did you reload Nagios afterwards? Can you show more lines of what your are doing when you say “I get in the telegram the hostname of my server” (I do not completely understand what is happening here)?

  4. Robert

    Thank you for attention @pommi

    I set the pager in contact and did restart the nagios service.

    What am I doing to test is this:

    I’m getting the code that is in command.cfg in nagios and testing manually.

    /usr/local/bin/ --token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw --object_type host --contact "$CONTACTPAGER$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostname "$HOSTNAME$" --hostaddress "$HOSTADDRESS$" --output "$HOSTOUTPUT$"

    From what I understand, for some reason “$CONTACTPAGER$” is not being recognized as a variable and is giving the following error:

    Traceback (most recent call last):
    File "/usr/local/bin/", line 74, in
    File "/usr/local/bin/", line 66, in main
    user_id = int(
    ValueError: invalid literal for int() with base 10: '$'

    If I change the “$CONTACTPAGER$” to the id of the group, I can to get the message in the telegram but not in the expected way:

    linux01 $ ($): $

    linux01 is the hostname of my server.

    I’m stuck on this question without knowing what to do 🙁

    Thank you in advance!

  5. pommi Post author

    Hi Robert, Thanks for your explanation. When testing from the shell you need to replace all the “$VALUES$”. The “$VALUE$” is there for nagios to replace it by actual content. You can see examples in my blog post, how to send test messages. The reason you get “linux01 $ ($): $”, is because your shell interprets all things starting with a $-sign as environment variables. For example “$HOSTADDRESS”, which is empty so you get “$” (“$HOSTADDRESS$” minus “$HOSTADDRESS”).

    P.S. You might want to “/revoke” the API key you just published in your comment. 😉 I replaced it with the example from the blog post.

  6. Stephan

    Trying to install this on OS X (10.11). It requires a TelegramBot:
    (/usr/local/bin)($) ./
    Traceback (most recent call last):
    File “./”, line 4, in
    from twx.botapi import TelegramBot

    I was able to install the telegram-cli. I am using python 2.7.10:
    (/usr/local/bin)($) python
    Python 2.7.10 (default, Oct 23 2015, 19:19:21)
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin

    Any way to make this working?

  7. pommi Post author requires twx.botapi, a Telegram Bot API client python library. You need to install that.

  8. Ale

    Hi Pommi,
    Thank you for you post.
    I installed all the pre requisites ans execute the scipt to test the send, but i did not give any error.
    How can i troubleshoot it to debug it ?

    / –token 200194008:AAEG6djWC9FENEZaVIo3y3vZm24P3GTMetw –object_type service –contact “+54xxxxx” –servicestate “OK” –hostname “hostname.domain.tld” –servicedesc “load” –output “OK – load average: 0.02 0.01 0.01”

  9. pommi Post author

    Hi Ale, I’ve removed your token/API key, because it shouldn’t be published online 😉

    What looks strange in your case is the – – contact option. It shouldn’t start with a +, it’s not a phone number. It is a Telegram GroupID or UserID. When using a UserID make sure you first start a conversation with your bot first. A bot cannot initiate a conversation to prevent spam on Telegram.

  10. Ale

    Hi Pommi,
    First Thank you to Fix my Error 🙂 I really forget to clear it when i post it. Thank you Again.
    For the other hand i will test to change the phone number and put your recommendation.
    Thank you a lot.

  11. Ale

    Hi Pommi,
    I am trying to send message to a group, but i have this error:
    Error(error_code=400, description=”API doesn’t answer”)

    The command i Use is: –token 444444444:AAXXXXXXXXXXXXXXXXX –object_type service –contact “-4444444” –servicestate “OK” –hostname “hostname.domain.tld” –servicedesc “load” –output “OK – load average: 0.02 0.01 0.01”

  12. Nicolas

    Hello. I’m running into an issue where when Nagios wants to send a notification I get an “ImportError: No module named twx.botapi” error. But when I send a test message I receive it with no problem. I cant make it work. What am I doing wrong?


  13. pommi Post author

    Hi Nicolas, that means that the twx.botapi python library is not available for the nagios user. Did you install the twx.botapi python library system wide?

  14. Laurent


    This is the error I get when running the script :
    # ./
    Traceback (most recent call last):
    File “./”, line 4, in
    from twx.botapi import TelegramBot
    File “/usr/lib/python2.7/site-packages/twx/botapi/”, line 1, in
    from . botapi import *
    File “/usr/lib/python2.7/site-packages/twx/botapi/”, line 9, in
    from requests import Request, Session
    File “/usr/lib/python2.7/site-packages/requests/”, line 61, in
    from .packages.urllib3.exceptions import DependencyWarning
    ImportError: cannot import name DependencyWarning

    Could you tell what is the problem or missing module ?




    Hi, I have the same problem

    Traceback (most recent call last):
    File “./”, line 4, in
    from twx.botapi import TelegramBot
    ImportError: No module named twx.botapi

    Requirement already satisfied: python-telegram-bot in /usr/lib/python2.6/site-packages
    Requirement already satisfied: future>=0.15.2 in /usr/lib/python2.6/site-packages (from python-telegram-bot)
    Requirement already satisfied: certifi in /usr/lib/python2.6/site-packages (from python-telegram-bot)
    Requirement already satisfied: importlib in /usr/lib/python2.6/site-packages (from future>=0.15.2->python-telegram-bot)
    Requirement already satisfied: argparse in /usr/lib/python2.6/site-packages (from future>=0.15.2->python-telegram-bot)

  16. Laurent

    Hi Jose,

    Yours is due to the missing module twx.botapi, please do :
    pip install twx.botapi

    After this, you will maybe have the same problem as I’m having.



  17. Steven

    Im triying to install it on a CentoOS7 server that i use to monitorize vm-machines, can anyone explain me how i install the twx.botapi here, i can’t find the way and i have all already configured.
    Thanks in advance

  18. LENG


    I try to followed the installation progress and seem everything is fine. But when i tried to send test message, I don’t receive it on my telegram group chat. Anyone suggestion please !!!!!

  19. FajarAB

    why the output that appears like this $ ($): $$. and why notification alert only to the service, not on host?

  20. Victor

    *From StackOverflow

    In order to get the group/user chat id, do as follows:

    1. Add the Telegram BOT to the group/chat.
    2. Send him a any message.
    3. Get the list of updates for your BOT:


    5. Look for the “chat” object:


    This is a sample of the response when you add your BOT into a group.
    Use the “id” of the “chat” object to send your messages.

  21. Craig

    Thanks for this. After a little bit of fiddling, and figuring out how to install twx.botapi, I have this working on CentOS 7.5.
    Could someone point me in the right direction on how to reformat the messages sent from a service as they don’t look as clean as the host messages…

    Host Messages:
    🔥 ss-fsl-durham ( (Host check timed out after 30.01 seconds)
    ✅ ss-fsl-durham ( PING OK – Packet loss = 60%, RTA = 94.25 ms

    Service Messages:
    🔥 ss-fsl-durham/Chrome: chrome.exe: Running

    How/where can I remove the ‘/’ that is just before ‘Chrome’?


    P.S. for those struggling with twx.botapi this is how I got it working on my CentOS install. You will have to adapt for your OS.

    [nagios@monitor ~]# cd
    [nagios@monitor ~]# yum install jansson-devel lua-devel readline-devel libconfig-devel libevent-devel openssl-devel python-pip
    [nagios@monitor ~]# pip install –upgrade pip
    [nagios@monitor ~]# git clone –recursive
    [nagios@monitor ~]# cd tg
    [nagios@monitor ~]# ./configure
    [nagios@monitor ~]# make
    [nagios@monitor ~]# pip install twx.botapi

  22. Viktor

    Hello to all,
    I proceed configuration of this tool this morning at RedHat 7.3 Enterprise Linux. I had one issue with the configuration so I would like to share them with you.
    1) Steps from Victor (nice name by the way 🙂 ) did not work I could not get any output from
    Fix: remove BOT from group chat and then add BOT again
    Rest of guide from Victor is working perfectly and there is no need to install
    I was searching for something useful to integrate IM into the process of monitoring our environment.
    @pommi Thank you very much for this article and for python script as well. You are amazing.

  23. Marcus Vinicius

    Hello Everyone. Please, i search examples files host.cfg and service.cfg. How to add notify-host-by-telegram and notify-service-by-telegram that files. i need add commad event_handler notify-host-by-telegram ??? Please help me.

Leave a Reply

Your email address will not be published. Required fields are marked *