Skip to content

Instantly share code, notes, and snippets.

@l0ser140
Last active July 19, 2024 19:46
Show Gist options
  • Save l0ser140/cde4916b038bb14a5388bca00ce496be to your computer and use it in GitHub Desktop.
Save l0ser140/cde4916b038bb14a5388bca00ce496be to your computer and use it in GitHub Desktop.
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=("https://api.telegram.org/".$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=("https://api.telegram.org/".$botID."/sendmessage\?chat_id=".$chatId."&text=Wrong command.") keep-result=no ;
};
} else={
:tool fetch url=("https://api.telegram.org/".$botID."/sendmessage\?chat_id=".$chatId."&text=I can't talk with you.") keep-result=no ;
};
:set updateId ($updateId +1) ;
:tool fetch url=("https://api.telegram.org/".$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=("https://api.telegram.org/".$botID."/sendmessage\?chat_id=".$myChatID."&text=".$scriptName." Executed") keep-result=no ;
@burabaika
Copy link

burabaika commented Jul 19, 2024

Не работает.

Создал два шедулера, вставил туда Ваш код.

Заменил значения бота и чата на свои.
Обновление бота не работает.
Снимок

Если вставить скрипт обновления в терминал, показывает такую ошибку, подчеркивает букву C красным
:set content ($content ->"data");
syntax error (line 1 column 6)

RB750Gr3
Прошивка 7.15.2

@l0ser140
Copy link
Author

l0ser140 commented Jul 19, 2024

Код работает на RoS 6.
На 7 не тестировался и скорее всего требует правок.

@burabaika
Copy link

блин....и как быть?

@dspanc
Copy link

dspanc commented Jul 19, 2024

блин....и как быть?

все прекрасно работает на последних прошивках

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment