telegram-chat: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:01 +01:00
parent 6b1c6a7119
commit 6fd745fc0f

View file

@ -8,10 +8,12 @@
# use Telegram to chat with your Router and send commands
# https://git.eworm.de/cgit/routeros-scripts/about/doc/telegram-chat.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local Main do={
:local ScriptName [ :tostr $1 ];
:global Identity;
:global TelegramChatActive;
:global TelegramChatGroups;
@ -41,7 +43,7 @@
:global WaitForFile;
:global WaitFullyConnected;
$ScriptLock $0;
$ScriptLock $ScriptName;
$WaitFullyConnected;
@ -53,7 +55,7 @@ $WaitFullyConnected;
}
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
$LogPrintExit2 warning $0 ("Downloading required certificate failed.") true;
$LogPrintExit2 warning $ScriptName ("Downloading required certificate failed.") true;
}
$RandomDelay $TelegramRandomDelay;
@ -68,7 +70,7 @@ $RandomDelay $TelegramRandomDelay;
:set TelegramRandomDelay [ $MAX 0 ($TelegramRandomDelay - 1) ];
} on-error={
:if ($I < 4) do={
$LogPrintExit2 debug $0 ("Fetch failed, " . $I . ". try.") false;
$LogPrintExit2 debug $ScriptName ("Fetch failed, " . $I . ". try.") false;
:set TelegramRandomDelay [ $MIN 15 ($TelegramRandomDelay + 5) ];
:delay (($I * $I) . "s");
}
@ -77,7 +79,7 @@ $RandomDelay $TelegramRandomDelay;
}
:if ($Data = false) do={
$LogPrintExit2 warning $0 ("Failed getting updates from Telegram.") true;
$LogPrintExit2 warning $ScriptName ("Failed getting updates from Telegram.") true;
}
:local UpdateID 0;
@ -102,8 +104,8 @@ $RandomDelay $TelegramRandomDelay;
:if ($Trusted = true) do={
:local Done false;
:if ($Message->"text" = "?") do={
$LogPrintExit2 info $0 ("Sending notice for update " . $UpdateID . ".") false;
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
$LogPrintExit2 info $ScriptName ("Sending notice for update " . $UpdateID . ".") false;
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=("Online, awaiting your commands!") });
:set Done true;
@ -114,7 +116,7 @@ $RandomDelay $TelegramRandomDelay;
} else={
:set TelegramChatActive false;
}
$LogPrintExit2 info $0 ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \
$LogPrintExit2 info $ScriptName ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \
" from update " . $UpdateID . "!") false;
:set Done true;
}
@ -123,7 +125,7 @@ $RandomDelay $TelegramRandomDelay;
:local State "";
:local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]);
$MkDir "tmpfs/telegram-chat";
$LogPrintExit2 info $0 ("Running command from update " . $UpdateID . ": " . $Message->"text") false;
$LogPrintExit2 info $ScriptName ("Running command from update " . $UpdateID . ": " . $Message->"text") false;
:execute script=(":do {\n" . $Message->"text" . "\n} on-error={ /file/add name=\"" . $File . ".failed\" };" . \
"/file/add name=\"" . $File . ".done\"") file=($File . "\00");
:if ([ $WaitForFile ($File . ".done") [ $EitherOr $TelegramChatRunTime 20s ] ] = false) do={
@ -133,15 +135,15 @@ $RandomDelay $TelegramRandomDelay;
:set State "The command failed with an error!\n\n";
}
:local Content [ /file/get $File contents ];
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=("Command:\n" . $Message->"text" . "\n\n" . $State . [ $IfThenElse ([ :len $Content ] > 0) \
("Output:\n" . $Content) [ $IfThenElse ([ /file/get $File size ] > 0) \
("Output exceeds file read size.") ("No output.") ] ]) });
/file/remove "tmpfs/telegram-chat";
} else={
$LogPrintExit2 info $0 ("The command from update " . $UpdateID . " failed syntax validation!") false;
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
$LogPrintExit2 info $ScriptName ("The command from update " . $UpdateID . " failed syntax validation!") false;
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=("Command:\n" . $Message->"text" . "\n\nThe command failed syntax validation!") });
}
@ -151,17 +153,20 @@ $RandomDelay $TelegramRandomDelay;
[ $IfThenElse ([ :len ($From->"username") ] = 0) "without username" ("'" . $From->"username" . "'") ] . \
" (ID " . $From->"id" . ") in update " . $UpdateID . "!");
:if ($Message->"text" ~ ("^! *" . [ $EscapeForRegEx $Identity ] . "\$")) do={
$LogPrintExit2 warning $0 $MessageText false;
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
$LogPrintExit2 warning $ScriptName $MessageText false;
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=("You are not trusted.") });
} else={
$LogPrintExit2 info $0 $MessageText false;
$LogPrintExit2 info $ScriptName $MessageText false;
}
}
} else={
$LogPrintExit2 debug $0 ("Already handled update " . $UpdateID . ".") false;
$LogPrintExit2 debug $ScriptName ("Already handled update " . $UpdateID . ".") false;
}
}
:set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
[ $IfThenElse ($UpdateID >= $TelegramChatOffset->2) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
}
$Main [ :jobname ];