MikroTik: Send Message to Telegram

Notifications are an important part of monitoring of a MikroTik router’s health, performance and different events, including potential security issues.

Telegram is a popular messaging app that offers a variety of features, including notifications.

It can be used to receive notifications from various sources, including MikroTik.

This post provides a step-by-step guide on how to configure MikroTik to send messages to Telegram and integrate it into MikroTik scripts.

Cool Tip: How to send messages form a Linux command-line through a Telegram API! Create your personal notification bot! Read more →

Send Message to Telegram from MikroTik

A quick message can be sent from MikroTik to Telegram, as follows:

[admin@MikroTik] > /tool fetch keep-result=no url="https://api.telegram.org/bot<tgBotToken>/sendMessage\?chat_id=<tgChatID>&text=<message>"
- example -
[admin@MikroTik] > /tool fetch keep-result=no url="https://api.telegram.org/bot0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi/sendMessage\?chat_id=012345678&text=Hey%20Telegram!%20Hello%20from%20MikroTik!"

Sending a message from MikroTik:

Receiving a message in Telegram:

MikroTik Telegram Script

If you plan to send messages to Telegram from MikroTik scripts, I would recommend to create a special function for this, let’s say $tgSendMessage and store it as a :global environment variable from where it can be called by the other scripts.

Create a script named telegram-config-global and open it in an editor:

[admin@MikroTik] > /system script add name=telegram-config-global
[admin@MikroTik] > /system script edit telegram-config-global source

Write a source code of the script:

#!rsc by ShellHacks
# RouterOS script: telegram-config-global
# Copyright (c) 2011-2024 www.ShellHacks.com <mail@shellhacks.com>
#
# Telegram Bot API global settings
# Documentation: https://www.shellhacks.com/mikrotik-send-message-to-telegram
# Source: https://github.com/shellhacks/MikroTik/blob/main/src/telegram-config-global.rsc
#
# Tested on RouterOS, version=7.12 (stable)
# Version: 1.0.0

# BEGIN SETUP
:global tgBotToken "0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi"
:global tgChatID "012345678"
# END SETUP

# Send message to Telegram
# Usage: $tgSendMessage $tgChatID ""
:global tgSendMessage do={
  :global tgBotToken
  :local url ("https://api.telegram.org/bot$tgBotToken" . "/sendMessage\?chat_id=" . $1 . "&text=" . $2 . "&parse_mode=html")
  /tool fetch keep-result=no url="$url"
}

Press Ctrl + O to save the script and close the editor.

The latest version of this script can be found on GitHub.

Once the script is created you can manually test it as follows:

[admin@MikroTik] > /system script run telegram-config-global
[admin@MikroTik] > :environment print
- sample output -
tgBotToken="0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi"
tgChatID="012345678"
tgSendMessage={[]; {(evl [/{}] (evl [/global{name=$tgBotToken}]) (evl [/local{name=$url; ...
[admin@MikroTik] > $tgSendMessage $tgChatID "Yo!"
- sample output -
      status: finished
  downloaded: 0KiBC-z pause]
       total: 0KiB
    duration: 1s

Finally, it is required to configure this script to run on MikroTik startup using a scheduler.

This is required to keep these variables persistent, since by default they are not saved after reboot:

[admin@MikroTik] > /system scheduler add name=telegram-config-global \
                                         on-event=telegram-config-global \
                                         start-time=startup
[admin@MikroTik] > /system scheduler print
- sample output -
Columns: NAME, START-TIME, INTERVAL, ON-EVENT, RUN-COUNT
# NAME                            START-TIME  INTERVAL  ON-EVENT                RUN-COUNT
0 telegram-config-global startup              0s        telegram-config-global  0

To verify if the script is executed on startup and performs its intended function, reboot the MikroTik device and check the global environment variables after:

[admin@MikroTik] > /system reboot
...
[admin@MikroTik] > :environment print
- sample output -
tgBotToken="0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi"
tgChatID="012345678"
tgSendMessage={[]; {(evl [/{}] (evl [/global{name=$tgBotToken}]) (evl [/local{name=$url; ...

Cool Tip: How to send a message to a Telegram channel using Python! Read more →

Was it useful? Share this post with the world!

Leave a Reply