Надо тогда уж:запрос в телеге
/home system identity
/home log warning [/system identity get name]
Надо тогда уж:запрос в телеге
/home system identity
Этот скрипт выплюнет месадж в лог с именем. А можно выполнить произвольную команду ?
Вы и выполняете произвольную команду. Или Вы хотите приказать роутеру блины испечь ? Роутер может исполнять команды только из набора скриптового языка РоутерОС или пользовательские, заранее написанные скрипты и функции./home log warning [/system identity get name]
Этот скрипт выплюнет месадж в лог с именем. А можно выполнить произвольную команду ?
Sertik писал(а): ↑26 окт 2022, 12:50Вы и выполняете произвольную команду. Или Вы хотите приказать роутеру блины испечь ? Роутер может исполнять команды только из набора скриптового языка РоутерОС или пользовательские, заранее написанные скрипты и функции./home log warning [/system identity get name]
Этот скрипт выплюнет месадж в лог с именем. А можно выполнить произвольную команду ?
маршруты в чат Телеграмм можно было бы получить выполнив команду РоутерОс:Спасибо, я ознакомился с "Руководство SATELLITE_v1.9". Мне наверное не актуально от туда 99 процентов.
Мне интересно было получить маршруты например и адреса на интерфейсах, посмотреть в нетвоч и скрипты
Грубо - результат выполнения print в терминале.
Код: Выделить всё
# заполняем ключевой массив наглядно командами и подписями к ним
:local arCom [:toarray {"arp"="список arp";
"address"="список ip addresses";
"backup"="резервное копирование конфигурации роутера";
"lease"="список DHCP liase";
"report"="отчет статуса роутера";
"status"="параметры системы";
"vpnuser"="настроенные VPN-пользователи с паролями";
"vpn"="серверы и клиенты VPN роутера"}]
# далее вызываем функцию:
:global tlgrmcmd
:log warning [$tlgrmcmd $arCom identity]
Код: Выделить всё
# TLGRM - combined notifications script & launch of commands (scripts & functions) via Telegram
# Script uses ideas by Sertik, Virtue, Pepelxl, Dimonw, -13-, Jotne, Alice Tails, Chupaka, drPioneer, Brook
# https://forummikrotik.ru/viewtopic.php?p=81945#p81945
# tested on ROS 6.49.6
# updated 2022/10/04 added support for function prefix Ros format: $funcName or [$funcName]
# updated 2022/10/11 added support smile-commands
# updated 2022/10/17 added ignore registre identity
# updated 2022/10/25 added global functions tlgrmcmd and tlgrm for set command list in chatbot and set flags
# added flag allowTlgrm (true/false) for allow or bun TLGRM work
:global allowTlgrm; # Flag allow run TLGRM (true - allow, false - ban)
:global scriptTlgrm; # Flag of the running script: false=>in progress, true=>idle
:if ([:typeof $allowTlgrm]!="bool") do={:set allowTlgrm true}
:if ($allowTlgrm) do={
:global Emoji; # router Emoji in chat
:global botID
:global myChatID
:global broadCast; # reception mode
:global launchScr; # Permission to execute scripts
:global launchFnc; # Permission to perform functions
:global launchCmd; # Permission to execute commands
:do {
# Function of searching comments for MAC-address
# https://forummikrotik.ru/viewtopic.php?p=73994#p73994
:local FindMacAddr do={
:if ($1~"[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]") do={
:foreach idx in=[/ip dhcp-server lease find disabled=no] do={
:local mac [/ip dhcp-server lease get $idx mac-address];
:if ($1~"$mac") do={:return ("$1 [$[/ip dhcp-server lease get $idx address]/$[/ip dhcp-server lease get $idx comment]].")};
}
:foreach idx in=[/interface bridge host find] do={
:local mac [/interface bridge host get $idx mac-address];
:if ($1~"$mac") do={:return ("$1 [$[/interface bridge host get $idx on-interface]].")};
}
}
:return ($1);
}
# Function of converting CP1251 to UTF8
# https://forummikrotik.ru/viewtopic.php?p=81457#p81457
:local CP1251toUTF8 do={
:local cp1251 [:toarray {
"\20";"\01";"\02";"\03";"\04";"\05";"\06";"\07";"\08";"\09";"\0A";"\0B";"\0C";"\0D";"\0E";"\0F";\
"\10";"\11";"\12";"\13";"\14";"\15";"\16";"\17";"\18";"\19";"\1A";"\1B";"\1C";"\1D";"\1E";"\1F";\
"\21";"\22";"\23";"\24";"\25";"\26";"\27";"\28";"\29";"\2A";"\2B";"\2C";"\2D";"\2E";"\2F";"\3A";\
"\3B";"\3C";"\3D";"\3E";"\3F";"\40";"\5B";"\5C";"\5D";"\5E";"\5F";"\60";"\7B";"\7C";"\7D";"\7E";\
"\C0";"\C1";"\C2";"\C3";"\C4";"\C5";"\C6";"\C7";"\C8";"\C9";"\CA";"\CB";"\CC";"\CD";"\CE";"\CF";\
"\D0";"\D1";"\D2";"\D3";"\D4";"\D5";"\D6";"\D7";"\D8";"\D9";"\DA";"\DB";"\DC";"\DD";"\DE";"\DF";\
"\E0";"\E1";"\E2";"\E3";"\E4";"\E5";"\E6";"\E7";"\E8";"\E9";"\EA";"\EB";"\EC";"\ED";"\EE";"\EF";\
"\F0";"\F1";"\F2";"\F3";"\F4";"\F5";"\F6";"\F7";"\F8";"\F9";"\FA";"\FB";"\FC";"\FD";"\FE";"\FF";\
"\A8";"\B8";"\B9"}];
:local utf8 [:toarray {
"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"000A";"0020";"0020";"000D";"0020";"0020";\
"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";\
"0021";"0022";"0023";"0024";"0025";"0026";"0027";"0028";"0029";"002A";"002B";"002C";"002D";"002E";"002F";"003A";\
"003B";"003C";"003D";"003E";"003F";"0040";"005B";"005C";"005D";"005E";"005F";"0060";"007B";"007C";"007D";"007E";\
"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";"2116"}];
:local convStr "";
:local code "";
:for i from=0 to=([:len $1]-1) do={
:local symb [:pick $1 $i ($i+1)];
:local idx [:find $cp1251 $symb];
:local key ($utf8->$idx);
:if ([:len $key]!=0) do={
:set $code ("%$[:pick ($key) 0 2]%$[:pick ($key) 2 4]");
:if ([pick $code 0 3]="%00") do={:set $code ([:pick $code 3 6])};
} else={:set code ($symb)};
:set $convStr ($convStr.$code);
}
:return ($convStr);
}
# convert string to lowstring
:local fsLowStr do={
:local fsLowerChar do={
:local "fs_lower" "0123456789abcdefghijklmnopqrstuvwxyz";
:local "fs_upper" "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
:local pos [:find $"fs_upper" $1]
:if ($pos > -1) do={:return [:pick $"fs_lower" $pos];}
:return $1}
:local result ""; :local in $1
:for i from=0 to=([:len $in] - 1) do={
:set result ($result . [$fsLowerChar [:pick $in $i]])}
:return $result;
}
# Telegram messenger response parsing function
# https://habr.com/ru/post/482802/
:local MsgParser do={
:local variaMod ("\"".$2."\"");
:if ([:len [:find $1 $variaMod -1]]=0) do={:return ("'unknown'")};
:local startLoc ([:find $1 $variaMod -1]+[:len $variaMod]+1);
:local commaLoc ([:find $1 "," $startLoc]);
:local brakeLoc ([:find $1 "}" $startLoc]);
:local endLoc $commaLoc;
:local startSymbol [:pick $1 $startLoc];
:if ($brakeLoc!=0 and ($commaLoc=0 or $brakeLoc<$commaLoc)) do={:set endLoc $brakeLoc};
:if ($startSymbol="{") do={:set endLoc ($brakeLoc+1)};
:if ($3=true) do={:set startLoc ($startLoc+1); :set endLoc ($endLoc-1)};
:if ($endLoc<$startLoc) do={:set endLoc ($startLoc+1)};
:return ([:pick $1 $startLoc $endLoc]);
}
# Time translation function to UNIX-time
# https://forum.mikrotik.com/viewtopic.php?t=75555#p790745
# Usage: $EpochTime [time input]
# Get current time: put [$EpochTime]
# Read log time in one of three format: "hh:mm:ss", "mmm/dd hh:mm:ss" or "mmm/dd/yyyy hh:mm:ss"
:local EpochTime do={
:local ds [/system clock get date];
:local ts [/system clock get time];
:if ([:len $1]>19) do={:set ds "$[:pick $1 0 11]"; :set ts [:pick $1 12 20]};
:if ([:len $1]>8 && [:len $1]<20) do={:set ds "$[:pick $1 0 6]/$[:pick $ds 7 11]"; :set ts [:pick $1 7 15]};
:local yesterday false;
:if ([:len $1]=8) do={
:if ([:totime $1]>ts) do={:set yesterday (true)};
:set ts $1;
}
:local months;
:if ((([:pick $ds 9 11]-1)/4)!=(([:pick $ds 9 11])/4)) do={
:set months {"an"=0;"eb"=31;"ar"=60;"pr"=91;"ay"=121;"un"=152;"ul"=182;"ug"=213;"ep"=244;"ct"=274;"ov"=305;"ec"=335};
} else={
:set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"ec"=334};
}
:set ds (([:pick $ds 9 11]*365)+(([:pick $ds 9 11]-1)/4)+($months->[:pick $ds 1 3])+[:pick $ds 4 6]);
:set ts (([:pick $ts 0 2]*3600)+([:pick $ts 3 5]*60)+[:pick $ts 6 8]);
:if (yesterday) do={:set ds ($ds-1)};
:return ($ds*86400+$ts+946684800-[/system clock get gmt-offset]);
}
# Time conversion function from UNIX-time
# https://forummikrotik.ru/viewtopic.php?t=11636
# usage: [$UnixTimeToFormat "timeStamp" "type"]
# type: "unspecified" - month/dd/yyyy <only> (Mikrotik sheduller format)
# 1 - yyyy/mm/dd hh:mm:ss
# 2 - dd:mm:yyyy hh:mm:ss
# 3 - dd month yyy hh mm ss
# 4 - yyyy month dd hh mm ss
# 5 - month/dd/yyyy-hh:mm:ss (Mikrotik sheduller format)
:local UnixTimeToFormat do={
:local decodedLine "";
:local timeStamp $1;
:local timeS ($timeStamp%86400);
:local timeH ($timeS/3600);
:local timeM ($timeS%3600 /60);
:set $timeS ($timeS-$timeH*3600-$timeM*60);
:local dateD ($timeStamp/86400);
:local dateM 2;
:local dateY 1970;
:local leap false;
:while (($dateD/365)>0) do={
:set $dateD ($dateD-365);
:set $dateY ($dateY+1);
:set $dateM ($dateM+1);
:if ($dateM=4) do={
:set $dateM 0;
:if (($dateY%400=0) or ($dateY%100!=0)) do={:set $leap true; :set $dateD ($dateD-1)};
} else={:set $leap false};
}
:local months [:toarray (0,31,28,31,30,31,30,31,31,30,31,30,31)];
:if (leap) do={:set $dateD ($dateD+1); :set ($months->2) 29};
:do {
:for i from=1 to=12 do={
:if (($months->$i)>$dateD) do={
:set $dateM $i;
:set $dateD ($dateD+1);
break;
} else={:set $dateD ($dateD-($months->$i))};
}
} on-error={};
:local tmod;
:if ([:len $2]!=0) do={:set $tmod $2} else={:set $tmod (:nothing)};
:local sl "/";
:local mstr {"jan";"feb";"mar";"apr";"may";"jun";"jul";"aug";"sep";"oct";"nov";"dec"};
:local strY [:tostr $dateY];
:local strN;
:local strD;
:local strH;
:local strM;
:local strS;
:if ($dateM>9) do={:set $strN [:tostr $dateM]} else={:set $strN ("0".[:tostr $dateM])};
:if ($dateD>9) do={:set $strD [:tostr $dateD]} else={:set $strD ("0".[:tostr $dateD])};
:if ($timeH>9) do={:set $strH [:tostr $timeH]} else={:set $strH ("0".[:tostr $timeH])};
:if ($timeM>9) do={:set $strM [:tostr $timeM]} else={:set $strM ("0".[:tostr $timeM])};
:if ($timeS>9) do={:set $strS [:tostr $timeS]} else={:set $strS ("0".[:tostr $timeS])};
:do {
:if ([:len $tmod]=0) do={:local mt ($mstr->($dateM-1)); :set $decodedLine ("$mt/"."$strD/"."$strY"); break};
:if ($tmod=1) do={:set $decodedLine "$strY$sl$strN$sl$strD $strH:$strM:$strS"; break};
:if ($tmod=2) do={:set $decodedLine "$strD$sl$strN$sl$strY $strH:$strM:$strS"; break};
:if ($tmod=3) do={:set $decodedLine ("$strD ".($mstr->($dateM-1))." $strY $strH:$strM:$strS"); break};
:if ($tmod=4) do={:set $decodedLine ("$strY ".($mstr->($dateM-1))." $strD $strH:$strM:$strS"); break};
:if ($tmod=5) do={:local m ($mstr->($dateM-1)); :set $decodedLine ("$m/"."$strD/"."$strY"."-$strH:$strM:$strS"); break};
} on-error={};
:return ($decodedLine);
}
# Main body of the script
:global timeAct;
:global timeLog;
# updated 2022/10/17 added ignore registre identity
:local nameID [$fsLowStr [/system identity get name]];
:local timeOf [/system clock get gmt-offset];
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Start of TLGRM-script on '$nameID' router.");
:if ([:len $scriptTlgrm]=0) do={:set scriptTlgrm true};
:if ($scriptTlgrm) do={
:set scriptTlgrm false;
:if ([:len $timeAct]>0) do={:put ("$[$UnixTimeToFormat ($timeAct+$timeOf) 1] - Time when the last command was launched.")};
:if ([:len $timeLog]>0) do={:put ("$[$UnixTimeToFormat ($timeLog+$timeOf) 1] - Time when the log entries were last sent.")};
# Part of the script body to launch via Telegram
# https://forummikrotik.ru/viewtopic.php?p=78085
:local timeStmp [$EpochTime];
:local urlString "https://api.telegram.org/$botID/getUpdates\?offset=-1&limit=1&allowed_updates=message";
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - *** Stage of launch scripts, function & commands via Telegram:");
:if ([:len $timeAct]=0) do={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Time of the last launch of the command was not found.");
:set timeAct $timeStmp;
} else={
:local httpResp [/tool fetch url=$urlString as-value output=user];
:local content ($httpResp->"data");
:if ([:len $content]>30) do={
:local msgTxt [$MsgParser $content "text" true];
:set msgTxt ([:pick $msgTxt ([:find $msgTxt "/" -1]+1) [:len $msgTxt]]);
:if ($msgTxt ~"@") do={:set $msgTxt [:pick $msgTxt 0 [:find $msgTxt "@"]]}
:local newStr "";
:local change "";
:for i from=0 to=([:len $msgTxt]-1) do={
:local symb [:pick $msgTxt $i ($i+1)];
:if ($symb="_") do={:set change (" ")} else={:set change ($symb)};
:set $newStr ($newStr.$change);
}
:set msgTxt $newStr;
:local msgAddr "";
:if ($broadCast) do={:set $msgAddr $nameID} else={
:set msgAddr ([:pick $msgTxt 0 [:find $msgTxt " " -1]]);
# updated 2022/10/17 added ignore registre identity
:set msgAddr [$fsLowStr $msgAddr]
:if ([:len [:find $msgTxt " "]]=0) do={:set msgAddr ("$msgTxt ")};
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Recipient of the Telegram message: '$msgAddr'");
:set msgTxt ([:pick $msgTxt ([:find $msgTxt $msgAddr -1]+[:len $msgAddr]+1) [:len $msgTxt]]);
}
# added 04/10/2022 skipping the function prefix "$" или [$ .....]
:if ([:pick $msgTxt 0 1]="\$") do={:set $msgTxt [:pick $msgTxt 1 [:len $msgTxt]]}
:if (([:pick $msgTxt 0 2]="[\$") and ([:pick $msgTxt ([:len $msgTxt]-1) [:len $msgTxt]]="]")) do={:set $msgTxt [:pick $msgTxt 2 ([:len $msgTxt]-1)]}
:if ($msgAddr=$nameID or $msgAddr="forall") do={
:local chatID [$MsgParser [$MsgParser $content "chat"] "id"];
:local userNm [$MsgParser $content "username"];
:set timeStmp [$MsgParser $content "date"];
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Sender of the Telegram message: $userNm");
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Command to execute: '$msgTxt'");
:local restline [];
:if ([:len [:find $msgTxt " "]]!=0) do={
:set restline [:pick $msgTxt ([:find $msgTxt " "]+1) [:len $msgTxt]];
:set msgTxt [:pick $msgTxt 0 [:find $msgTxt " "]];
}
:if ($chatID=$myChatID && $timeAct<$timeStmp) do={
:set timeAct $timeStmp;
:if ([/system script environment find name=$msgTxt]!="" && $launchFnc=true) do={
:if (([/system script environment get [/system script environment find name=$msgTxt] value]="(code)") \
or ([:len [:find [/system script environment get [/system script environment find name=$msgTxt] value] "(eval"]]>0)) do={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Telegram user $userNm launches function '$msgTxt'.");
:log warning ("Telegram user $userNm launches function '$msgTxt'.");
# [:parse ":global $msgTxt; [\$$msgTxt $restline]"];
:execute script="[:parse [\$$msgTxt $restline]]";
} else={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - '$msgTxt' is a global variable and not a function - no execute.");
:log warning ("'$msgTxt' is a global variable and not a function - no execute.");
}
}
# added 07/10/2022 allow to perform emodji !
:if ([:pick $msgTxt 0 1]="\5C") do={:set $msgTxt [:pick $msgTxt 1 [:len $msgTxt]]
:if ([:find $msgTxt "\5C"]!=0) do={:local a [:pick $msgTxt 0 [:find $msgTxt "\5C"]]; :local b [:pick $msgTxt ([:find $msgTxt "\5C"]+1) [:len $msgTxt]]; :set $msgTxt ($a.$b)}}
:if ([/system script find name=$msgTxt]!="" && $launchScr=true) do={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Telegram user $userNm activates script '$msgTxt'.");
:log warning ("Telegram user $userNm activates script '$msgTxt'.");
# [[:parse "[:parse [/system script get $msgTxt source]] $restline"]];
:execute script="[[:parse \"[:parse [/system script get $msgTxt source]] $restline\"]]";
}
:if ([/system script find name=$msgTxt]="" && [/system script environment find name=$msgTxt]="" && $launchCmd=true) do={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Telegram user $userNm is trying to execute command '$msgTxt'.");
:log warning ("Telegram user $userNm is trying to execute command '$msgTxt'.");
# :do {[:parse "/$msgTxt $restline"]} on-error={};
:do {:execute script="[:parse \"/$msgTxt $restline\"]"} on-error={};
}
} else={:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Wrong time to launch.")};
} else={:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - No command found for this device.")};
} else={:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Completion of response from Telegram.")};
}
# Part of the script body for notifications in Telegram
# https://www.reddit.com/r/mikrotik/comments/onusoj/sending_log_alerts_to_telegram/
:local outMsg "";
:local logGet [:toarray [/log find ($topics~"warning" or $topics~"error" or $topics~"critical" or $topics~"caps" or $topics~"wireless" or $message~"logged in")]];
:local logCnt [:len $logGet];
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - *** Stage of sending notifications to Telegram:");
:if ([:len $timeLog]=0) do={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Time of the last log entry was not found.");
:set outMsg (">$[/system clock get time] Telegram notification started.");
}
:if ($logCnt>0) do={
:local lastTime [$EpochTime [/log get [:pick $logGet ($logCnt-1)] time]];
:local index 0;
:local tempTim "";
:local tempMsg "";
:local tempTpc "";
:local unixTim "";
:do {
:set index ($index+1);
:set tempTim [/log get [:pick $logGet ($logCnt-$index)] time];
:set tempTpc [/log get [:pick $logGet ($logCnt-$index)] topics];
:set tempMsg [/log get [:pick $logGet ($logCnt-$index)] message];
:set tempMsg (">$tempTim $tempMsg");
:local findMacMsg ([$FindMacAddr $tempMsg]);
:set unixTim [$EpochTime $tempTim];
:if (($unixTim>$timeLog) && (!(($tempTpc~"caps" or $tempTpc~"wireless" or $tempTpc~"dhcp") && ($tempMsg!=$findMacMsg)))) do={
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Found log entry: $findMacMsg");
:set outMsg ($findMacMsg."\n".$outMsg);
}
} while=(($unixTim>$timeLog) && ($index<$logCnt));
:if (([:len $timeLog]<1) or (([:len $timeLog]>0) && ($timeLog!=$lastTime) && ([:len $outMsg]>8))) do={
:set timeLog $lastTime;
:if ([:len $outMsg]>4096) do={:set outMsg ([:pick $outMsg 0 4096])};
:set outMsg [$CP1251toUTF8 $outMsg];
:set outMsg ("$Emoji "."$[/system identity get name]".":"."%0A"."$outMsg");
:local urlString ("https://api.telegram.org/$botID/sendmessage\?chat_id=$myChatID&text=$outMsg");
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Generated string for Telegram:\r\n".$urlString);
/tool fetch url=$urlString as-value output=user;
} else={:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - There are no log entries to send.")};
} else={:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Necessary log entries were not found.")};
:set scriptTlgrm true;
} else={:put "$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - Script already being executed..."};
:put ("$[$UnixTimeToFormat ([$EpochTime]+$timeOf) 1] - End of TLGRM-script on '$nameID' router.");
} on-error={
:set scriptTlgrm true;
:put ("Script error: something didn't work when sending a request to Telegram.");
:put ("*** First, check the correctness of the values of the variables botID & myChatID. ***");
}
# add 25/10/2022
# function set commands list key array in $1 to Telegram chatbot
:global tlgrmcmd; :if (!any $tlgrmcmd) do={:global tlgrmcmd do={
:if ([:typeof $0]="lookup") do={
#--function teSetMyCommands by Brook 2022 --
:local teSetMyCommands do={
:local TbotID $fBotID
:local tgUrl []; :local content []
:local commandsList [:toarray $fCommands]
:local cmdItems []
:local command []
:local end []
:foreach i in=$commandsList do={
:local command [:pick $i 0 [find $i ";"]]
:local description [:pick $i ([find $i ";"] + 1) [:len $i]]
:local startCommand "\7B\22command\22:\22$command\22"
:local commandDescription ",\22description\22:\22$description\22\7D,"
:set command "$startCommand$commandDescription$endCommand"
:set $cmdItems ($cmdItems . $command)
}
:set cmdItems [:pick $cmdItems 0 ([:len $cmdItems] - 1)]
:local start "\5B"; :local end "\5D";
:set commandsList "$start$cmdItems$end"
:set tgUrl "https://api.telegram.org/$TbotID/setMyCommands\?commands=$commandsList"
do {
:set content [:tool fetch ascii=yes url=$tgUrl as-value output=user]
:if ($content->"status" = "finished") do={ :return true }
} on-error={ :return false }
}
# --function convert string to lowstring by Osama, modified Sertik--
:local fsLowStr do={
:local fsLowerChar do={
:local "fs_lower" "0123456789abcdefghijklmnopqrstuvwxyz";
:local "fs_upper" "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
:local pos [:find $"fs_upper" $1]
:if ($pos > -1) do={:return [:pick $"fs_lower" $pos];}
:return $1}
:local result ""; :local in $1
:for i from=0 to=([:len $in] - 1) do={
:set result ($result . [$fsLowerChar [:pick $in $i]])}
:return $result;
}
# -- Function of converting CP1251 to UTF8 by DrPioneer --
# https://forummikrotik.ru/viewtopic.php?p=81457#p81457
:local CP1251toUTF8 do={
:local cp1251 [:toarray {
"\20";"\01";"\02";"\03";"\04";"\05";"\06";"\07";"\08";"\09";"\0A";"\0B";"\0C";"\0D";"\0E";"\0F";\
"\10";"\11";"\12";"\13";"\14";"\15";"\16";"\17";"\18";"\19";"\1A";"\1B";"\1C";"\1D";"\1E";"\1F";\
"\21";"\22";"\23";"\24";"\25";"\26";"\27";"\28";"\29";"\2A";"\2B";"\2C";"\2D";"\2E";"\2F";"\3A";\
"\3B";"\3C";"\3D";"\3E";"\3F";"\40";"\5B";"\5C";"\5D";"\5E";"\5F";"\60";"\7B";"\7C";"\7D";"\7E";\
"\C0";"\C1";"\C2";"\C3";"\C4";"\C5";"\C6";"\C7";"\C8";"\C9";"\CA";"\CB";"\CC";"\CD";"\CE";"\CF";\
"\D0";"\D1";"\D2";"\D3";"\D4";"\D5";"\D6";"\D7";"\D8";"\D9";"\DA";"\DB";"\DC";"\DD";"\DE";"\DF";\
"\E0";"\E1";"\E2";"\E3";"\E4";"\E5";"\E6";"\E7";"\E8";"\E9";"\EA";"\EB";"\EC";"\ED";"\EE";"\EF";\
"\F0";"\F1";"\F2";"\F3";"\F4";"\F5";"\F6";"\F7";"\F8";"\F9";"\FA";"\FB";"\FC";"\FD";"\FE";"\FF";\
"\A8";"\B8";"\B9"}];
:local utf8 [:toarray {
"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"000A";"0020";"0020";"000D";"0020";"0020";\
"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";"0020";\
"0021";"0022";"0023";"0024";"0025";"0026";"0027";"0028";"0029";"002A";"002B";"002C";"002D";"002E";"002F";"003A";\
"003B";"003C";"003D";"003E";"003F";"0040";"005B";"005C";"005D";"005E";"005F";"0060";"007B";"007C";"007D";"007E";\
"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";"2116"}];
:local convStr "";
:local code "";
:for i from=0 to=([:len $1]-1) do={
:local symb [:pick $1 $i ($i+1)];
:local idx [:find $cp1251 $symb];
:local key ($utf8->$idx);
:if ([:len $key]!=0) do={
:set $code ("%$[:pick ($key) 0 2]%$[:pick ($key) 2 4]");
:if ([pick $code 0 3]="%00") do={:set $code ([:pick $code 3 6])};
} else={:set code ($symb)};
:set $convStr ($convStr.$code);
}
:return ($convStr);
}
:global botID
:global GroupChat
:global broadCast
:local Identity "";
:if ($2="identity") do={:set Identity [$fsLowStr [/system identity get name]]; :set Identity ("$Identity"."_")}
:if ($2="forall") do={:set Identity ("$2"."_")}
:if ([:len $2]=0) do={:set Identity ""}
:local count 0
:local TXTmessage ""
:foreach k,v in=$1 do={
:set $v [$CP1251toUTF8 $v]
:set TXTmessage ("$TXTmessage"."$Identity"."$k;$v".",")
}
:set TXTmessage [:pick $TXTmessage 0 ([:len $TXTmessage]-1)]
:if ([$teSetMyCommands fBotID=$botID fCommands=$TXTmessage]) do={:return OK} else={:return ERR}
}
}
}
:global tlgrm; :if (!any $tlgrm) do={:global tlgrm do={
:if (any $0) do={
:if ($1="help") do={:global Emoji; :global FuncTelegramSender; [$FuncTelegramSender ("$Emoji $[/system identity get name]:%0A [/$[:pick $0 1 [:len $0]]] service function tlgrm parameters:%0A $0 allow, bun, on, off, Scr, Fnc, Cmd, GroupChat, broadCast")]; :return []}
:if (($1="allow") && ([:len $2]=0)) do={:global allowTlgrm; :set allowTlgrm true; :return $1}
:if (($1="bun") && ([:len $2]=0)) do={:global allowTlgrm; :set allowTlgrm false; :return $1}
:if (($1="on") && ([:len $2]=0)) do={:global tlgrm; [$tlgrm launchScr true]; [$tlgrm launchFnc true]; [$tlgrm launchCmd true]; :return $1}
:if (($1="off") && ([:len $2]=0)) do={:global tlgrm; [$tlgrm launchScr false]; [$tlgrm launchFnc false]; [$tlgrm launchCmd false]; :return $1}
:if ([:len $1]=0) do={:global tlgrm; [$tlgrm help]
:return []}
:if (($1="GroupChat") && ([:typeof $2]="str") && ([:pick $2 0 1]="@")) do={:global GroupChat; :set GroupChat $2; :return $2}
:if (($1="GroupChat") && ([:len $2]=0)) do={:global GroupChat; :set GroupChat []; :return $2}
:local fSet
:if (($2="true") or ($2="false")) do={
:if ($2="true") do={:set fSet true} else={:set fSet false}
:if ($1="broadCast") do={:global broadCast; :set broadCast $fSet;}
:return $2}
:if ($1="Scr") do={:global launchScr; :set launchScr $fSet; :return $2}
:if ($1="Fnc") do={:global launchFnc; :set launchFnc $fSet; :return $2}
:if ($1="Cmd") do={:global launchCmd; :set launchCmd $fSet; :return $2}
} else={:return "ERROR function $0 parameter $1 $2"}
:return "ERROR function $0 parameter <$1>"
}
}
}
}