Telegram Media Type

Video Lecture

Create Telegram Bot Webhook Media Type Create Telegram Bot Webhook Media Type Telegram Media Type

Description

We create a webhook media type that pushes messages to Telegram.

Telegram is a messaging app that runs on PC and popular smart phones.

Before creating the media type, you will need to

  • Install Telegram,
  • Create a Bot,
  • Copy it's HTTP API Token,
  • Create a Group,
  • Add the Bot to it,
  • Send the Group at least 1 message.
  • Retrieve the Chat ID from the chat data using the getUpdates api call with the bots HTTP API Token.

To get the Chat ID from the chat data, visit this url

https://api.telegram.org/botXXX:YYY/getUpdates (replace the XXX: YYY with your HTTP API Token you just got from Telegram)

Search the returned JSON data for last chat message with type=group, and copy the chat id from it. This is your Chat ID to use for sending messages to the group.

Zabbix 5

Go back into Zabbix, go to Administration --> Media Types and edit the Telegram Media Type and add your new Bot API Token to the configuration.

Save and Test

Your Telegram Media Type can now be used like any other.

Zabbix 4.4

Go back into Zabbix, go to Administration --> Media Types and create a new Media Type.

Name it what you want,

Select the Type as Webhook

The parameters,

1
2
Subject : {ALERT.SUBJECT}
Message : {ALERT.MESSAGE}    

And Script,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
try {
    var params = JSON.parse(value);

    var chatid = "your chat id";
    var botid = "your bots http token id";

    var req = new CurlHttpRequest();
    req.AddHeader('Content-Type: application/x-www-form-urlencoded');
    var URL = encodeURI("https://api.telegram.org/bot" + botid + "/sendMessage?chat_id=" + chatid + "&text=" + params.Subject + "\n" + params.Message)
    Zabbix.Log(1, 'Telegram Webhook Url : ' + URL);
    var result = req.Get(URL, {});
    Zabbix.Log(1, 'Telegram Webhook Result : ' + result);
    return true
} catch (error) {
    Zabbix.Log(1, "Telegram Webhook Failed : " + error);
    throw error
}

Modify the chatid and botid parameters in the script above to be yours.

Save and Test

Your Telegram Media Type can now be used like any other.

Note

In this version I created for Zabbix 4.4, when adding the media type to your user, put anything in the sendto field, since in this script, the sendto is hard coded already as the chat id and is ignored from the information provided by the trigger.

Get Telegram Chat ID

You can also try this tool to get the Chat ID of your Telegram group. Ensure you have created a Group first, added your Bot to it, and sent at least 1 message to the group. The Chat ID will be extracted from the message data.




    

Optional

ParseMode

Leave empty, otherwise see the official telegram documentation for Formatting options

Troubleshooting

If the getUpdates url doesn't return a node containing a group with an id, then

  • remove your bot from the group**,
  • and then re add your bot to the group,
  • then send a message to the group again,
  • then try the getUpdates url again.

The chat id is likely to be a negative number, so make sure you copy the negative symbol as well when setting the chat id in the script.

Eg, If chat id = -123456789, occasionally quickly copying and pasting you may forget the - character and copy just 123456789

See the official Telegram getUpdates documentation at https://core.telegram.org/bots/api#getting-updates

If you get an error "no message for media type", then go into the Media type settings, and then its Message Templates tab and then add the Problem and Problem Recovery templates if they don't already exist, and then update.

Telegram getUpdates API

Supported macros