MikroTik Scripts: Sending a message to Telegram (function)

If you plan to use a lot of scripts on your MikroTik device and send their results to Telegram. It is not rational to write the procedure for sending a message in each script, the best way would be to place the “send message to Telegram” function in a separate script. And referring to this script as needed.

Now you will have to change the Telegram notification parameters in just one script.

? 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.

Article in other languages:
?? – MikroTik Scripts: Enviar un mensaje a Telegram (función)
?? – MikroTik Скрипты: Отправка сообщения в Telegram (функция)
?? – Scripts MikroTik: Envoi d’un message à Telegram (fonction)
?? – MikroTik-Skripte: Senden einer Nachricht an Telegram (funktion)
?? – MikroTik-scripts: een bericht naar Telegram sturen (functie)

Script “Sending a message to Telegram”

Since the script will be executed from another script, setting the script execution permissions is not required.

The script uses html markup, which markup options Telegram uses can be read – here.

The script accepts the MessageText variable, which contains the message text. MessageText cannot be empty.

If the script is executed without errors, information about sending a message and text are recorded in the MikroTik log.

[System] -> [Scripts] -> [+] -> [Name: MyTGBotSendMessage] -> [Don't Require Permissions]

Script code:

:local BotToken "987654321:AAFJIVTAWodBwzGX2CLne6-PK4RFNSy-8OY";
:local ChatID "987654321";
:local ParseMode "html";
:local DisableWebPagePreview True;
:local SendText $MessageText;

:local tgUrl "https://api.telegram.org/bot$BotToken/sendMessage?chat_id=$ChatID&text=$SendText&parse_mode=$ParseMode&disable_web_page_preview=$DisableWebPagePreview";

/tool fetch http-method=get url=$tgUrl keep-result=no;

:log info "Send Telegram Message: $MessageText";
Script "Sending a message to Telegram"

Calling the “Sending a message to Telegram” script from another script

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

To send a message, you must pass the MessageText parameter to the MyTGBotSendMessage script.

Script code:

:local DeviceName [/system identity get name];

:local MessageText "\F0\9F\9F\A2 <b>$DeviceName:</b> <i>italic</i>  <code>inline fixed-width code</code> plain text <a href=\"https://mhelp.pro/\">More Scripts</a>";

:local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]];

$SendTelegramMessage MessageText=$MessageText;

If, when calling the script, you receive a syntax error or a failure: closing connection: <400 Bad Request> error, the problem may be in the use of MikroTik service characters in the message text, for verification try sending a plain text without special characters or markup characters, in English.

Calling the "Sending a message to Telegram" script from another script

? This article discussed the creation of the Send message to Telegram script from Mikrotik. I hope now the use of sending messages to Telegram from MikroTik devices has become clearer. 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).

3 thoughts on “MikroTik Scripts: Sending a message to Telegram (function)”

  1. feb/01/2022-|-09:32:21-|-1bat23-|-1000-|-172.16.1.189-|-0C:98:38:A6:5E:6F-|-31d-|-3mb-|-vc-799-01.09.22-WATI

    there is one on many on system > script > name
    no comment and no source on that name script

    how can i get text “WATI” with script hotspot login with user hotspot 1bat23

    Reply

Leave a Comment