Last active
January 16, 2024 22:03
-
-
Save l0ser140/cde4916b038bb14a5388bca00ce496be to your computer and use it in GitHub Desktop.
Mikrotik Telegram bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Schedule run at startup. | |
#Telegram API keys for use in another scripts. | |
:global botID "botXXXXXX:XXXXXXX" ; | |
:global myChatID "XXXXXXXX" ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ; |
В новой версии RouterOS 7.13, добавили:
*) log - added "fetch" topic;
Теперь при каждом срабатывании скрипта, идет запись в лог, у меня скрипт срабатывает раз в 2 минуты - получается не кислый такой спам в логах:
Download from https://api.telegram.org/bot####:###/getUpdates?limit=1&allowed_updates=message to RAM FINISHED
Вот обсуждение:
https://forummikrotik.ru/viewtopic.php?t=12659&start=120
Думаю исправить это можно так:
/system logging
set 0 topics=info,!debug
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:global myChatID "XXXXXXXX" ;
неверный ChatID