Is it possible to send alert notifications to a Telegram group or contact?
Q: I need to post a message to a Telegram room every time alert is executed. How can I do that?
A: Follow the below tutorial on how to post IPHost Network Monitor alert notifications to Telegram chat. We will create a bot for this purpose, but will only use the bot’s token (won’t implement any response handling). Screenshots below were taken using Android client for Telegram.
1. Create a bot
Contact BotFather, start a chat with it, and issue a command /newbot. Give new bot a name and a username of your choice:
Write down the access token.
2. Create a new group chat or talk to the bot directly
Either create a new group and invite your bot to it, or just locate it by name (this is IPHost-demo above; use the one of your own choice) and start a chat. You need to type first “/start” command to activate bot, then any message (such as “Hi”) to this chat:
Now the bot has access to chat information and you can determine the chat ID by opening the following URL in browser:
where BOT_TOKEN is the token string obtained at step 1.
The URL, if crafted correctly, will return a JSON object; locate chat ID in it:
Write down the chat ID (marked with red box in sample response above).
3. Create Execute program action
Now open IPHost GUI client and create new “Execute program” action (click “Alerting > Alerts”, select “Simple Actions” tab):
For this action, assign the following parameters using the template variables:
Set fields as:
- Mode: set to Run program
- Path: $IPHostInstallDir\curl.exe (curl.exe bundled with IPHost Network Monitor)
- Arguments: a very long line that looks exactly like this:
-s --max-time 10 -d "chat_id=your_chat_id&disable_web_page_preview=1&parse_mode=Markdown" --data-urlencode "text=[$MonitorName]($MonitorReportUrl) on [$HostName]($HostReportUrl) - $EventShortDescription $Duration" -k https://api.telegram.org/botyour_token/sendMessage
your_chat_id: chat ID obtained at step 2
your_token: token obtained at step 1
- Credentials for authentication are not necessary, leave them empty.
This action invokes curl executable and forms a correct Telegram API request that will be passed to it. Variables are expanded to their values w.r.t. the monitor state change that caused the alert.
Now add newly created Simple Action to an existing alert, for example to “E-mail to Administrator + Popup” alert used by the Default Alerting Rule. Click “Alerts”, select “E-mail to Administrator + Popup” (or any other alert of your choice), and click “Edit”. Use “Add” drop-down in a newly opened alert editor to add “Send to Telegram” simple action:
Click “OK” required number of times to save the changes.
4. Test Telegram notifications
Now that “Send to Telegram” action has been added to alerts, try putting a monitor to Down state and back (by altering its “State conditions” and polling the monitor), or use “Alerting > Testing” tab to simulate alert execution.
If Telegram integration has been done correctly, you will see something like below in your Telegram chat:
In case the notification testing (see step 4. above) doesn’t result in message appearing in the corresponding Telegram chat, try doing the following:
a. Open, in your browser, the below URL:
having first replaced both “your_token” and “your_chat_id” placeholders with appropriate strings obtained at earlier steps.
If you have set up everything correctly
- the “Test message” string should be posted to corresponding Telegram chat
- the page displayed in browser (JSON response) should start with “ok: true” record
b. Open elevated cmd.exe (right-click, “Run as Administrator”), navigate to IPHost installation directory, and call the cURL command manually:
C: cd "C:\program Files (x86)\IPHost Network Monitor" curl.exe --max-time 10 -d "chat_id=your_chat_id&disable_web_page_preview=1&parse_mode=Markdown" --data-urlencode "text=Test message" https://api.telegram.org/botyour_token/sendMessage
again, having first replaced both “your_token” and “your_chat_id” placeholders with appropriate strings. Note that curl.exe arguments should all be printed on the same line (do not insert any linebreaks).
Unless you get no clues why posting notifications to Telegram chat fails, send us the test results (what is printed and displayed when you perform checks a. and b. above), along with error message(s) IPHost prints when you try to send corresponding test alert.