Laurent Telegram Terminal uses JSON parser for Router OS https://github.com/Winand/mikrotik-json-parser
Laurent 5G has over 100 firmware api commands, but not all of them are needed to be executed from the chatbot. Those api commands that we allow to execute must be entered in the arrayCom array. You can edit it however you like. LTT user settings must also be set before operation.
Here's Laurent Telegram Terminal (LTT) codе (the code is far from ideal, but it works and is being improved):
Code: Select all
# LTT by Sertik 28/11/2022
# ----------------------------------------------------------------------------------------------
# http://<IP_address/cmd.cgi?psw=<Laurent`s password>&cmd=<KeCommands>
# http://<IP_address>/<Name json file>[?psw=<Laurent`s_pasword>]
#----------------------------------------------------------------------------------------------
#LTT settings:
:local Emoji "%F0%9F%A7%9A"
:local Esys "$Emoji$[/system identity get name]%0A"
:local TToken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:local TChatId "YYYYYYYYY"
:local Ladr 192.168.0.1
:local Sport 80
:local PSW "Laurent"
# allowed $KE commands:
:local arrayCom [:toarray {"REL"="set on/off/switch rele";
"RDR"="rele status";
"INF"="information Laurent-5G board";
"GST"="modem status";
"SMS"="send SMS";
"PSW"="read module password";
"SEC"="security policy";
"RID"="read status OUT line";
"SENSOR"="get sensors status Laurent";
"SETTINGS"="get sensors status Laurent";
"WR"="set on/off/switch OUT line"}]
# sub-function of replacing a space with a comma in a line
:local SpaceComChar do={:local string; :set $string $1;
:local StrTele ""; :local code "";
:for i from=0 to=([:len $string]-1) do={:local keys [:pick $string $i (1+$i)];; if ($keys=" ") do={:set $code ","} else={:set $code $keys}; :set $StrTele ("$StrTele"."$code")}
:return $StrTele;}
#main body script
/system script run JParseFunctions
:global Toffset
:if ([:typeof $Toffset] != "num") do={:set Toffset 0}
do {
:global JSONIn [/tool fetch url="https://api.telegram.org/bot$TToken/getUpdates\?chat_id=$TChatId&offset=$Toffset" as-value output=user]; :set JSONIn ($JSONIn->"data")
} on-error={:set $JSONIn []}
:global fJParse
:global JParseOut [$fJParse]
:local Results ($JParseOut->"result")
# -----------------
:if ([:len $Results]>0) do={
:local TXT
:foreach k,v in=$Results do={
:if (any ($v->"message"->"text")) do={
:local cmd ($v->"message"->"text")
:local cmdR
:if ([:len [:find $cmd " "]]=0) do={:set cmdR $cmd} else={:set cmdR [:pick $cmd 0 [:find $cmd " "]]}
:local prefix "/"
:local cmdrun 0
# executing special commands "list" (output commands to chat)
:if ($cmdrun=0) do={
:if (($cmd="list") or ($cmd=$prefix."list")) do={
:local cmdlist; :foreach t,n in=$arrayCom do={:set $cmdlist ("$cmdlist"."/$t "."- "."$n"."\n")}
/tool fetch url="https://api.telegram.org/bot$TToken/sendmessage\?chat_id=$TChatId" \
http-method=post http-data="text=$Emoji $[/system identity get name] Laurent terminal commands:%0A$cmdlist" keep-result=no; set cmdrun 1
}
}
# executing special commands for read JSON data
:if ($cmdrun=0) do={
:local fetS
:if (($cmdR="SENSOR") or ($cmdR=$prefix."SENSOR")) do={:set fetS "sensor"}
:if (($cmdR="SETTINGS") or ($cmdR=$prefix."SETTINGS")) do={:set fetS "set"}
:if ([:len $fetS]!=0) do={
:local StrFetchLaurent; :local Lanswer;
:set StrFetchLaurent ("http://"."$Ladr".":"."$Sport"."/json_"."$fetS".".cgi\?psw=$PSW")
:do {
:set Lanswer [/tool fetch url=$StrFetchLaurent as-value output=user]
} on-error={:set TXT "ERROR call to Laurent 5G module"; log error $TXT; :set $JSONIn []}
:global JSONLoads;
:if ($cmd!=$cmdR) do={
:local WLanswer [$JSONLoads ($Lanswer->"data")]
:local cmdX [:pick $cmd ([:find $cmd " "]+1) [:len $cmd]]; :set $cmdR $cmdX; :set Lanswer [:tostr ($WLanswer->$cmdX)];} else={:set Lanswer ($Lanswer->"data")}
/tool fetch url="https://api.telegram.org/bot$TToken/sendmessage\?chat_id=$TChatId" \
http-method=post http-data="text=$Emoji $[/system identity get name] Laurent status [$cmdR]:%0A $Lanswer" keep-result=no; set cmdrun 1
}
}
# executing commands from a list arrayCom
:if ($cmdrun=0) do={
:foreach key,val in=$arrayCom do={
:if (($cmdR=$key) or ($cmdR=$prefix.$key)) do={
:set cmd [$SpaceComChar $cmd];
if ($cmdR=$prefix.$key) do={:set cmd [:pick $cmd ([:find $cmd $prefix]+1) [:len $cmd]]}
:local StrFetchLaurent; :local Lanswer;
:set StrFetchLaurent ("http://"."$Ladr".":"."$Sport"."/cmd.cgi\?psw=$PSW&cmd=$cmd")
:do {
:set Lanswer [/tool fetch url=$StrFetchLaurent as-value output=user]
} on-error={:set TXT "ERROR call to Laurent 5G module"; log error $TXT;}
:set Lanswer ($Lanswer->"data")
:set cmdrun 1;
:set TXT ("Executing Laurent command in progress <- $cmd -> %0A Module answer $Lanswer")
/tool fetch url="https://api.telegram.org/bot$TToken/sendmessage\?chat_id=$TChatId" \
http-method=post http-data="text=$Esys $TXT" keep-result=no;
}
}
}
:set $Toffset ($v->"update_id" + 1)}
}
} else={
:set $Toffset 0;
}