Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Mikrotik Telegram bot
#Schedule run every ~30sec.
:global botID;
:global myChatID;
:local parse do={
:local startLoc ([:find $content $variable -1] + [:len $variable] + 2);
:local commaLoc ([:find $content "," $startLoc] - 1 + 1);
:local braceLoc ([:find $content "}" $startLoc] - 1 + 1);
:local endLoc $commaLoc;
:local startSymbol [:pick $content $startLoc]
:if ($braceLoc != 0 and ($commaLoc = 0 or $braceLoc < $commaLoc)) do={
:set endLoc $braceLoc;
:if ($startSymbol = "{") do={
:set endLoc ($braceLoc + 1);
:if ($quotas = true) do={
:set startLoc ($startLoc + 2);
:set endLoc ($endLoc - 1);
:if ($endLoc < $startLoc) do={
:set endLoc ($startLoc + 1);
:local value [:pick $content $startLoc $endLoc]
:log debug message="Telegram: parsed $variable : $value";
:return $value;
:local content [/tool fetch url=("".$botID."/getUpdates?limit=1&allowed_updates=message") as-value output=user];
:set content ($content ->"data");
:if ([:len $content] > 23 ) do={
:local updateId [$parse content=$content variable="update_id"]
:local chat [$parse content=$content variable="chat"]
:local chatId [$parse content=$chat variable="id"]
:if ($chatId = $myChatID) do={
:local message [$parse content=$content variable="text" quotas=true] ;
:log warn message="Command $message received" ;
:if ([/system script find name=$message] != "") do={
:system script run $message ;
} else={
:tool fetch url=("".$botID."/sendmessage\?chat_id=".$chatId."&text=Wrong command.") keep-result=no ;
} else={
:tool fetch url=("".$botID."/sendmessage\?chat_id=".$chatId."&text=I can't talk with you.") keep-result=no ;
:set updateId ($updateId +1) ;
:tool fetch url=("".$botID."/getUpdates\?offset=$updateId") keep-result=no ;
#Schedule run at startup.
#Telegram API keys for use in another scripts.
:global botID "botXXXXXX:XXXXXXX" ;
:global myChatID "XXXXXXXX" ;
#Example script.
:local scriptName "wolmypc";
:global botID;
:global myChatID;
#Start usefull script part
/tool wol mac=XX:XX:XX:XX:XX:XX interface=ifname ;
#End usefull script part
:log debug message="$scriptName Executed" ;
/tool fetch url=("".$botID."/sendmessage\?chat_id=".$chatId."&text=".$scriptName." Executed") keep-result=no ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment