Function-converter for sending messages in Russian to Telegram from Mikrotik RouterOS

Messenger Telegram is extremely convenient for notifying about the operation of IT equipment, which is actively used by System Administrators. Popular WhatsApp and Viber support APIs, but they do not have built-in technology for creating their own bots.



RouterOS of the well-known company Mikrotik does not allow sending messages in Russian to Telegram using regular means. Russian characters can be adequately displayed in messages if sent in UTF-8 encoding. Well, the codes are known, why not write a transcoder?



To simplify the task, I did not use conversion algorithms, but created a key array containing, in the form of keys, the Russian characters themselves in string and uppercase layouts, and in the corresponding values ​​- the codes of these characters in UTF-8.



This array of data may be useful for other tasks:



Key array of characters of the Russian alphabet and values ​​of their codes in UTF8
#  table of the codes of Russian letters UTF8
:local rsimv [:toarray {""="D090"; ""="D091"; ""="D092"; ""="D093"; ""="D094"; ""="D095"; ""="D096"; ""="D097"; ""="D098"; ""="D099"; ""="D09A"; ""="D09B"; ""="D09C"; ""="D09D"; ""="D09E"; ""="D09F"; ""="D0A0"; ""="D0A1"; ""="D0A2"; ""="D0A3"; ""="D0A4"; ""="D0A5"; ""="D0A6"; ""="D0A7"; ""="D0A8"; ""="D0A9"; ""="D0AA"; ""="D0AB"; ""="D0AC"; ""="D0AD"; ""="D0AE"; ""="D0AF"; ""="D0B0"; ""="D0B1"; ""="D0B2"; ""="D0B3"; ""="D0B4"; ""="D0B5"; ""="D0B6"; ""="D0B7"; ""="D0B8"; ""="D0B9"; ""="D0BA"; ""="D0BB"; ""="D0BC"; ""="D0BD"; ""="D0BE"; ""="D0BF"; ""="D180"; ""="D181"; ""="D182"; ""="D183"; ""="D184"; ""="D185"; ""="D186"; ""="D187"; ""="D188"; ""="D189"; ""="D18A"; ""="D18B"; ""="D18C"; ""="D18D"; ""="D18E"; ""="D18F"; ""="D001"; ""="D191"}]






Now we write directly the string converter itself.

At the input, the $ string variable must contain a string containing Russian or mixed Russian-English text. The output is a collected string, ready to be sent to a Telegram. At the same time, the Latin Afavite characters and punctuation marks are not recoded, as they are normally displayed in the messenger chats. Instead of Russian characters, their counterparts in UTF-8 codes are inserted into the string, separated by "%" signs, used by Telegram to display encoded characters.



# encoding of the symbols and ssembly line
:local StrTele ""; :local code "";
:for i from=0 to=([:len $string]-1) do={:local keys [:pick $string $i (1+$i)]; :local key ($rsimv->$keys); if ([:len $key]!=0) do={:set $code ("%"."$[:pick ($rsimv->$keys) 0 2]"."%"."$[:pick ($rsimv->$keys) 2 4]");} else={:set $code $keys}; :set $StrTele ("$StrTele"."$code")}




Let's put all this in the FuncStrToTele function so that you can call it from any of your scripts in RouterOs Mikrotik. You can also use it in DUDe. Here is its full code (you can insert RouterOS Mikrotik into the repository of your scripts and execute the script to place the function in the / system script environment):



Mixed string converter function
# Function Converter of Russian characters for sending in Telegram

# by Sertik 09/09/2020

# usage [$FuncStrToTele « String .,!+»]



:global FuncStrToTele do={

:local string; :set $string $1;



# table of the codes of Russian letters UTF8

:local rsimv [:toarray {«»=«D090»; «»=«D091»; «»=«D092»; «»=«D093»; «»=«D094»; «»=«D095»; «»=«D096»; «»=«D097»; «»=«D098»; «»=«D099»; «»=«D09A»; «»=«D09B»; «»=«D09C»; «»=«D09D»; «»=«D09E»; «»=«D09F»; «»=«D0A0»; «»=«D0A1»; «»=«D0A2»; «»=«D0A3»; «»=«D0A4»; «»=«D0A5»; «»=«D0A6»; «»=«D0A7»; «»=«D0A8»; «»=«D0A9»; «»=«D0AA»; «»=«D0AB»; «»=«D0AC»; «»=«D0AD»; «»=«D0AE»; «»=«D0AF»; «»=«D0B0»; «»=«D0B1»; «»=«D0B2»; «»=«D0B3»; «»=«D0B4»; «»=«D0B5»; «»=«D0B6»; «»=«D0B7»; «»=«D0B8»; «»=«D0B9»; «»=«D0BA»; «»=«D0BB»; «»=«D0BC»; «»=«D0BD»; «»=«D0BE»; «»=«D0BF»; «»=«D180»; «»=«D181»; «»=«D182»; «»=«D183»; «»=«D184»; «»=«D185»; «»=«D186»; «»=«D187»; «»=«D188»; «»=«D189»; «»=«D18A»; «»=«D18B»; «»=«D18C»; «»=«D18D»; «»=«D18E»; «»=«D18F»; «»=«D001»; «»=«D191»; "№"=«0023»}]



# encoding of the symbols and ssembly line

:local StrTele ""; :local code "";

:for i from=0 to=([:len $string]-1) do={:local keys [:pick $string $i (1+$i)]; :local key ($rsimv->$keys); if ([:len $key]!=0) do={:set $code ("%"."$[:pick ($rsimv->$keys) 0 2]"."%"."$[:pick ($rsimv->$keys) 2 4]");} else={:set $code $keys}; :set $StrTele ("$StrTele"."$code")}



:return $StrTele;

}





As you can see from the comment, the only parameter of the function is the input string of characters, the function returns the generated string to be sent to the variable $ string



In our scripts, do not forget to declare a previously defined function to be called:



:global FuncStrToTele
[$FuncStrToTele ", user !"]




To send a message, you need to create your bot in Telegram, find out its API BOTToken and chat ID. Instructions for creating bots and getting IDs have been published several times. For example, you can look at the following links:





We send our test message to the bot chat:



:local botID "botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX" ;
:local myChatID "YYYYYY" ;
:global FuncStrToTele;
:local string [$FuncStrToTele "  Sertik !  - Russian alfabit  Telegramm"]
:tool fetch url=("https://api.telegram.org/$botID/sendmes ... xt=$string") keep-result=no


Check it works fine for me. Now you can easily send messages in Russian to Telegram! Comments and additions are welcome.



It is probably advisable to add sending a line feed character code, it is not difficult to do.



All Articles