2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: telegram-chat
|
|
|
|
# Copyright (c) 2023 Christian Hesse <mail@eworm.de>
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
|
|
|
# use Telegram to chat with your Router and send commands
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/telegram-chat.md
|
|
|
|
|
|
|
|
:local 0 "telegram-chat";
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
|
|
|
:global Identity;
|
|
|
|
:global TelegramChatActive;
|
|
|
|
:global TelegramChatGroups;
|
|
|
|
:global TelegramChatId;
|
|
|
|
:global TelegramChatIdsTrusted;
|
|
|
|
:global TelegramChatOffset;
|
|
|
|
:global TelegramChatRunTime;
|
2023-10-10 11:16:58 +00:00
|
|
|
:global TelegramMessageIDs;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global TelegramTokenId;
|
|
|
|
|
|
|
|
:global CertificateAvailable;
|
|
|
|
:global EitherOr;
|
|
|
|
:global EscapeForRegEx;
|
|
|
|
:global GetRandom20CharAlNum;
|
|
|
|
:global IfThenElse;
|
|
|
|
:global LogPrintExit2;
|
|
|
|
:global MkDir;
|
2023-10-10 10:08:17 +00:00
|
|
|
:global ParseJson;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global ScriptLock;
|
|
|
|
:global SendTelegram2;
|
|
|
|
:global SymbolForNotification;
|
|
|
|
:global ValidateSyntax;
|
|
|
|
:global WaitForFile;
|
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
|
|
|
$ScriptLock $0;
|
|
|
|
|
|
|
|
$WaitFullyConnected;
|
|
|
|
|
|
|
|
:if ([ :typeof $TelegramChatOffset ] != "array") do={
|
|
|
|
:set TelegramChatOffset { 0; 0; 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
|
|
|
|
$LogPrintExit2 warning $0 ("Downloading required certificate failed.") true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:22:51 +00:00
|
|
|
:local Data false;
|
2023-10-19 19:13:04 +00:00
|
|
|
:for I from=1 to=4 do={
|
2023-10-17 19:22:51 +00:00
|
|
|
:if ($Data = false) do={
|
|
|
|
:do {
|
|
|
|
:set Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
|
|
|
("https://api.telegram.org/bot" . $TelegramTokenId . "/getUpdates?offset=" . \
|
|
|
|
$TelegramChatOffset->0 . "&allowed_updates=%5B%22message%22%5D") as-value ]->"data");
|
|
|
|
} on-error={
|
2023-10-19 19:13:04 +00:00
|
|
|
:if ($I < 4) do={
|
|
|
|
$LogPrintExit2 debug $0 ("Fetch failed, " . $I . ". try.") false;
|
|
|
|
:delay (($I * $I) "s");
|
|
|
|
}
|
2023-10-17 19:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($Data = false) do={
|
|
|
|
$LogPrintExit2 warning $0 ("Failed getting updates from Telegram.") true;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:local UpdateID 0;
|
|
|
|
:local Uptime [ /system/resource/get uptime ];
|
2023-10-18 16:26:22 +00:00
|
|
|
:foreach UpdateArray in=([ $ParseJson $Data ]->"result") do={
|
2023-10-10 08:58:19 +00:00
|
|
|
:local Update [ $ParseJson $UpdateArray ];
|
|
|
|
:set UpdateID ($Update->"update_id");
|
2023-10-11 08:24:04 +00:00
|
|
|
:local Message [ $ParseJson ($Update->"message") ];
|
2023-10-16 11:07:29 +00:00
|
|
|
:local IsReply [ :len ($Message->"reply_to_message") ];
|
2023-10-16 11:05:21 +00:00
|
|
|
:local IsMyReply ($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id"));
|
|
|
|
:if (($IsMyReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={
|
2023-03-07 09:57:42 +00:00
|
|
|
:local Trusted false;
|
2023-10-10 08:58:19 +00:00
|
|
|
:local Chat [ $ParseJson ($Message->"chat") ];
|
|
|
|
:local From [ $ParseJson ($Message->"from") ];
|
|
|
|
|
2023-03-07 09:57:42 +00:00
|
|
|
:foreach IdsTrusted in=($TelegramChatId, $TelegramChatIdsTrusted) do={
|
2023-10-10 08:58:19 +00:00
|
|
|
:if ($From->"id" = $IdsTrusted || $From->"username" = $IdsTrusted) do={
|
2023-03-07 09:57:42 +00:00
|
|
|
:set Trusted true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($Trusted = true) do={
|
2023-10-16 10:50:50 +00:00
|
|
|
:local Done false;
|
2023-10-13 19:54:50 +00:00
|
|
|
:if ($Message->"text" = "?") do={
|
2023-10-19 07:19:34 +00:00
|
|
|
$LogPrintExit2 info $0 ("Sending notice for update " . $UpdateID . ".") false;
|
2023-10-17 12:01:10 +00:00
|
|
|
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
|
2023-10-13 19:54:50 +00:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
|
|
|
message=("Online, awaiting your commands!") });
|
|
|
|
:set Done true;
|
|
|
|
}
|
|
|
|
:if ($Done = false && [ :pick ($Message->"text") 0 1 ] = "!") do={
|
2023-10-10 08:58:19 +00:00
|
|
|
:if ($Message->"text" ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={
|
2023-03-07 09:57:42 +00:00
|
|
|
:set TelegramChatActive true;
|
|
|
|
} else={
|
|
|
|
:set TelegramChatActive false;
|
|
|
|
}
|
|
|
|
$LogPrintExit2 info $0 ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \
|
|
|
|
" from update " . $UpdateID . "!") false;
|
2023-10-16 10:50:50 +00:00
|
|
|
:set Done true;
|
|
|
|
}
|
2023-10-16 11:07:29 +00:00
|
|
|
:if ($Done = false && ($IsMyReply = 1 || ($IsReply = 0 && $TelegramChatActive = true)) && [ :len ($Message->"text") ] > 0) do={
|
2023-10-16 13:04:22 +00:00
|
|
|
:if ([ $ValidateSyntax ($Message->"text") ] = true) do={
|
|
|
|
:local State "";
|
|
|
|
:local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]);
|
|
|
|
$MkDir "tmpfs/telegram-chat";
|
|
|
|
$LogPrintExit2 info $0 ("Running command from update " . $UpdateID . ": " . $Message->"text") false;
|
|
|
|
:execute script=(":do {\n" . $Message->"text" . "\n} on-error={ /file/add name=\"" . $File . ".failed\" };" . \
|
2023-11-22 20:20:44 +00:00
|
|
|
"/file/add name=\"" . $File . ".done\"") file=($File . "\00");
|
2023-10-16 13:04:22 +00:00
|
|
|
:if ([ $WaitForFile ($File . ".done") [ $EitherOr $TelegramChatRunTime 20s ] ] = false) do={
|
|
|
|
:set State "The command did not finish, still running in background.\n\n";
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2023-10-16 13:04:22 +00:00
|
|
|
:if ([ :len [ /file/find where name=($File . ".failed") ] ] > 0) do={
|
|
|
|
:set State "The command failed with an error!\n\n";
|
|
|
|
}
|
2023-11-22 20:20:44 +00:00
|
|
|
:local Content [ /file/get $File contents ];
|
2023-10-17 12:01:10 +00:00
|
|
|
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
|
2023-10-16 13:04:22 +00:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
|
|
|
message=("Command:\n" . $Message->"text" . "\n\n" . $State . [ $IfThenElse ([ :len $Content ] > 0) \
|
2023-11-22 20:20:44 +00:00
|
|
|
("Output:\n" . $Content) [ $IfThenElse ([ /file/get $File size ] > 0) \
|
2023-10-16 13:04:22 +00:00
|
|
|
("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"); \
|
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
|
|
|
message=("Command:\n" . $Message->"text" . "\n\nThe command failed syntax validation!") });
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2023-10-16 13:04:22 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
} else={
|
2023-10-10 08:29:28 +00:00
|
|
|
:local MessageText ("Received a message from untrusted contact " . \
|
2023-10-10 08:58:19 +00:00
|
|
|
[ $IfThenElse ([ :len ($From->"username") ] = 0) "without username" ("'" . $From->"username" . "'") ] . \
|
|
|
|
" (ID " . $From->"id" . ") in update " . $UpdateID . "!");
|
|
|
|
:if ($Message->"text" ~ ("^! *" . [ $EscapeForRegEx $Identity ] . "\$")) do={
|
2023-10-10 08:29:28 +00:00
|
|
|
$LogPrintExit2 warning $0 $MessageText false;
|
2023-10-10 08:58:19 +00:00
|
|
|
$SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
|
2023-03-07 09:57:42 +00:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
|
|
|
message=("You are not trusted.") });
|
|
|
|
} else={
|
2023-10-10 08:29:28 +00:00
|
|
|
$LogPrintExit2 info $0 $MessageText false;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else={
|
|
|
|
$LogPrintExit2 debug $0 ("Already handled update " . $UpdateID . ".") false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
|
|
|
|
[ $IfThenElse ($UpdateID >= $TelegramChatOffset->2) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
|