Mikrotik Scripts: sending power on notification to Telegram

MikroTik script sends a power on notification the device to Telegram. Allows you to learn about unexpected device reboots or equipment power problems, complementing the monitoring system.
When sending a message to Telegram, it will also write the event to the system log.

? For the script to work, you must have a Telegram bot Token and a chat ID or a personal ID. Creating a Telegram bot and getting the required IDs is not the topic of this article.

The Telegram message contains:

  1. device identifier;
  2. information message about device startup.

Article in other languages:
?? – Mikrotik Scripts: envío de notificación de encendido a Telegram
?? – MikroTik Скрипты: отправить уведомление о включении устройства в Telegram
?? – Scripts Mikrotik: envoi d’une notification pour allumer l’appareil dans Telegram
?? – MikroTik-Skripte: Senden Sie eine Benachrichtigung, um das Gerät im Telegramm einzuschalten
?? – MikroTik-scripts: melding over het inschakelen van het apparaat, bericht in Telegram

Change device Identity

Change the device ID to better understand which device is sending the request.

[System] -> [Identity]

or

/system identity set name="WhiteHouseMikroTik"

Create a script notification about device power on

The script creates the RebootStatus variable.
If the RebootStatus variable does not matter (which happens when the script is launched for the first time), a message is sent to Telegram.
Then the RebootStatus variable is set to True.

For the script to work, you must set the following permissions: read, write, policy, check.

[System] -> [Scripts] -> [+] -> [Name: CheckPowerOn] -> [Policy: read, write, policy, test]

Script code:

# Func: Telegram send message
:local TGSendMessage do={
    :local tgUrl "https://api.telegram.org/bot$Token/sendMessage?chat_id=$ChatID&text=$Text&parse_mode=html";
    /tool fetch http-method=get url=$tgUrl keep-result=no;
}

# Constants
:global RebootStatus;
:local TelegramBotToken "987654321:AAFJIVTAWodBwzGX2CLne6-PK4RFNSy-8OY";
:local TelegramChatID "987654321";
:local DeviceName [/system identity get name];

:local TelegramMessageText "\F0\9F\9F\A2 <b>$DeviceName:</b> start after Power Off.";

# Program
:if ($RebootStatus != true)  do={
    $TGSendMessage Token=$TelegramBotToken ChatID=$TelegramChatID Text=$TelegramMessageText;
    :set RebootStatus true;
    :log info "Script CheckPowerOn send Telegram message."
}
Mikrotik Scripts: sending power on notification to Telegram

Add script launch to schedule

For the script to work, you must set the following permissions: read, write, policy, test.

[System] -> [Schedule] -> [+] -> [Name: CheckPowerOn script] - > [Interval: 00:05:00] -> [Policy: read, write, policy, test]

Script code:

/system script run CheckPowerOn
Adding a script for checking device startup and sending a Telegram message to the MikroTik task scheduler

Now every 5 minutes the device will check the reboot variable and send a notification to Telegram if the device has been rebooted.


? Mikrotik script for sending a notification about turning on a device in Telegram was discussed in this article. I hope this helps to increase control over your MikroTik device. However, if you run into any problems while setting up, feel free to write in the comments. I will try to help.

✅ The script is checked: hAP ac lite [RouterBOARD 952Ui-5ac2nD], RouterOS 6.47.8 (stable).

4 thoughts on “Mikrotik Scripts: sending power on notification to Telegram”

  1. Hi Yun,

    Do you build custom scripts for companies? I am looking to contract someone to write advanced telegram scripts as well as some other scripts for my project.
    Thank you for your time.

    Reply
  2. the script is not working ,i see some extra block in the screenshot which is missing in the code.Can you help ?
    # Func: TGet device Name
    :local FunncDeviceName do={
    :local DeviceName [/system identity print as-value];
    :set DeviceName ($DeviceName ->”name”);
    ;return [$DeviceName];

    Reply

Leave a Comment