2023-03-07 10:57:42 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: telegram-chat
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2023-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:12 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-03-07 10:57:42 +01:00
|
|
|
#
|
2024-05-24 20:37:42 +02:00
|
|
|
# requires RouterOS, version=7.15
|
2025-01-29 10:23:58 +01:00
|
|
|
# requires device-mode, fetch
|
2023-11-15 21:31:19 +01:00
|
|
|
#
|
2023-03-07 10:57:42 +01:00
|
|
|
# use Telegram to chat with your Router and send commands
|
2025-01-24 20:46:12 +01:00
|
|
|
# https://rsc.eworm.de/doc/telegram-chat.md
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-12-06 10:31:52 +01:00
|
|
|
:local ExitOK false;
|
2025-05-06 09:44:23 +02:00
|
|
|
:onerror Err {
|
2025-05-06 14:08:37 +02:00
|
|
|
:global GlobalConfigReady; :global GlobalFunctionsReady;
|
|
|
|
:retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
|
|
|
|
do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
|
2024-03-06 15:28:55 +01:00
|
|
|
:local ScriptName [ :jobname ];
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global Identity;
|
|
|
|
:global TelegramChatActive;
|
|
|
|
:global TelegramChatGroups;
|
|
|
|
:global TelegramChatId;
|
|
|
|
:global TelegramChatIdsTrusted;
|
|
|
|
:global TelegramChatOffset;
|
|
|
|
:global TelegramChatRunTime;
|
|
|
|
:global TelegramMessageIDs;
|
|
|
|
:global TelegramRandomDelay;
|
|
|
|
:global TelegramTokenId;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global CertificateAvailable;
|
|
|
|
:global EitherOr;
|
|
|
|
:global EscapeForRegEx;
|
2025-05-30 22:41:03 +02:00
|
|
|
:global FileExists;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global GetRandom20CharAlNum;
|
|
|
|
:global IfThenElse;
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2025-05-13 09:52:12 +02:00
|
|
|
:global LogPrintVerbose;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global MAX;
|
|
|
|
:global MIN;
|
|
|
|
:global MkDir;
|
|
|
|
:global RandomDelay;
|
2025-02-07 16:14:58 +01:00
|
|
|
:global RmDir;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ScriptLock;
|
|
|
|
:global SendTelegram2;
|
|
|
|
:global SymbolForNotification;
|
|
|
|
:global ValidateSyntax;
|
|
|
|
:global WaitForFile;
|
|
|
|
:global WaitFullyConnected;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-05 16:12:36 +01:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-06 15:28:55 +01:00
|
|
|
:error false;
|
2024-03-05 16:12:36 +01:00
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
$WaitFullyConnected;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ([ :typeof $TelegramChatOffset ] != "array") do={
|
|
|
|
:set TelegramChatOffset { 0; 0; 0 };
|
|
|
|
}
|
|
|
|
:if ([ :typeof $TelegramRandomDelay ] != "num") do={
|
|
|
|
:set TelegramRandomDelay 0;
|
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-06-20 10:47:42 +02:00
|
|
|
:if ([ $CertificateAvailable "Go Daddy Root Certificate Authority - G2" ] = false) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint warning $ScriptName ("Downloading required certificate failed.");
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-08 12:45:38 +01:00
|
|
|
:error false;
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2024-01-29 21:50:32 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
$RandomDelay $TelegramRandomDelay;
|
|
|
|
|
|
|
|
:local Data false;
|
|
|
|
:for I from=1 to=4 do={
|
|
|
|
:if ($Data = false) do={
|
2025-05-08 10:13:05 +02:00
|
|
|
:onerror Err {
|
2024-03-04 13:48:01 +01:00
|
|
|
: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");
|
|
|
|
:set TelegramRandomDelay [ $MAX 0 ($TelegramRandomDelay - 1) ];
|
2025-05-08 10:13:05 +02:00
|
|
|
} do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ($I < 4) do={
|
2025-05-08 10:13:05 +02:00
|
|
|
$LogPrint debug $ScriptName ("Fetch failed, " . $I . ". try: " . $Err);
|
2024-03-04 13:48:01 +01:00
|
|
|
:set TelegramRandomDelay [ $MIN 15 ($TelegramRandomDelay + 5) ];
|
|
|
|
:delay (($I * $I) . "s");
|
|
|
|
}
|
2023-10-19 21:13:04 +02:00
|
|
|
}
|
2023-10-17 21:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ($Data = false) do={
|
2024-04-26 20:50:08 +02:00
|
|
|
$LogPrint warning $ScriptName ("Failed getting updates.");
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-08 12:45:38 +01:00
|
|
|
:error false;
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-28 22:33:49 +01:00
|
|
|
:local JSON [ :deserialize from=json value=$Data ];
|
2024-03-04 13:48:01 +01:00
|
|
|
:local UpdateID 0;
|
|
|
|
:local Uptime [ /system/resource/get uptime ];
|
2024-03-28 22:33:49 +01:00
|
|
|
:foreach Update in=($JSON->"result") do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:set UpdateID ($Update->"update_id");
|
2025-05-13 09:52:12 +02:00
|
|
|
$LogPrintVerbose debug $ScriptName ("Update " . $UpdateID . ": " . [ :serialize to=json $Update ]);
|
|
|
|
|
2024-03-28 22:33:49 +01:00
|
|
|
:local Message ($Update->"message");
|
2025-05-23 17:38:22 +02:00
|
|
|
:local IsAnyReply ([ :typeof ($Message->"reply_to_message") ] = "array");
|
2024-03-28 22:33:49 +01:00
|
|
|
:local IsMyReply ($TelegramMessageIDs->[ :tostr ($Message->"reply_to_message"->"message_id") ]);
|
2024-03-04 13:48:01 +01:00
|
|
|
:if (($IsMyReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={
|
|
|
|
:local Trusted false;
|
2024-03-28 22:33:49 +01:00
|
|
|
:local Chat ($Message->"chat");
|
|
|
|
:local From ($Message->"from");
|
2024-05-24 20:37:42 +02:00
|
|
|
:local Command ($Message->"text");
|
2025-02-24 15:31:34 +01:00
|
|
|
:local ThreadId [ $IfThenElse ($Message->"is_topic_message") ($Message->"message_thread_id") "" ];
|
2023-10-10 10:58:19 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:foreach IdsTrusted in=($TelegramChatId, $TelegramChatIdsTrusted) do={
|
2025-05-12 16:53:55 +02:00
|
|
|
:if ($From->"id" = $IdsTrusted || \
|
|
|
|
$From->"username" = $IdsTrusted || \
|
|
|
|
$Chat->"id" = $IdsTrusted) do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:set Trusted true;
|
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ($Trusted = true) do={
|
|
|
|
:local Done false;
|
2024-05-24 20:00:03 +02:00
|
|
|
:if ($Command = "?") do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Sending notice for update " . $UpdateID . ".");
|
2025-02-24 15:31:34 +01:00
|
|
|
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; \
|
|
|
|
replyto=($Message->"message_id"); threadid=$ThreadId; \
|
2024-03-04 13:48:01 +01:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
2025-02-25 21:26:52 +01:00
|
|
|
message=([ $IfThenElse ([ :len ($From->"first_name") ] > 0) ("Hello " . ($From->"first_name") . "!\n\n") ] . \
|
|
|
|
"Online" . [ $IfThenElse $TelegramChatActive " (and active!)" ] . ", awaiting your commands!") });
|
2024-03-04 13:48:01 +01:00
|
|
|
:set Done true;
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
2024-05-25 19:55:13 +02:00
|
|
|
:if ($Done = false && [ :pick $Command 0 1 ] = "!") do={
|
2024-05-24 20:00:03 +02:00
|
|
|
:if ($Command ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:set TelegramChatActive true;
|
|
|
|
} else={
|
|
|
|
:set TelegramChatActive false;
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \
|
|
|
|
" from update " . $UpdateID . "!");
|
2024-03-04 13:48:01 +01:00
|
|
|
:set Done true;
|
|
|
|
}
|
2025-05-23 17:38:22 +02:00
|
|
|
:if ($Done = false && ($IsMyReply = 1 || ($IsAnyReply = false && \
|
2025-02-25 10:46:25 +01:00
|
|
|
$TelegramChatActive = true)) && [ :len $Command ] > 0) do={
|
2024-05-25 19:55:13 +02:00
|
|
|
:if ([ $ValidateSyntax $Command ] = true) do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:local State "";
|
|
|
|
:local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]);
|
2024-03-04 20:49:44 +01:00
|
|
|
:if ([ $MkDir "tmpfs/telegram-chat" ] = false) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint error $ScriptName ("Failed creating directory!");
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-08 12:45:38 +01:00
|
|
|
:error false;
|
2024-03-04 20:49:44 +01:00
|
|
|
}
|
2024-05-24 20:00:03 +02:00
|
|
|
$LogPrint info $ScriptName ("Running command from update " . $UpdateID . ": " . $Command);
|
|
|
|
:execute script=(":do {\n" . $Command . "\n} on-error={ /file/add name=\"" . $File . ".failed\" };" . \
|
2024-03-04 13:48:01 +01:00
|
|
|
"/file/add name=\"" . $File . ".done\"") file=($File . "\00");
|
|
|
|
:if ([ $WaitForFile ($File . ".done") [ $EitherOr $TelegramChatRunTime 20s ] ] = false) do={
|
2024-03-21 13:39:56 +01:00
|
|
|
:set State ([ $SymbolForNotification "warning-sign" ] . "The command did not finish, still running in background.\n\n");
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2025-05-30 22:41:03 +02:00
|
|
|
:if ([ $FileExists ($File . ".failed") ] = true) do={
|
2024-03-21 13:39:56 +01:00
|
|
|
:set State ([ $SymbolForNotification "cross-mark" ] . "The command failed with an error!\n\n");
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2024-03-28 21:29:27 +01:00
|
|
|
:local Content ([ /file/read chunk-size=32768 file=$File as-value ]->"data");
|
2025-02-24 15:31:34 +01:00
|
|
|
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; \
|
|
|
|
replyto=($Message->"message_id"); threadid=$ThreadId; \
|
2024-03-04 13:48:01 +01:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
2024-05-24 20:00:03 +02:00
|
|
|
message=([ $SymbolForNotification "gear" ] . "Command:\n" . $Command . "\n\n" . \
|
2024-03-21 13:39:56 +01:00
|
|
|
$State . [ $IfThenElse ([ :len $Content ] > 0) \
|
2024-03-28 21:29:27 +01:00
|
|
|
([ $SymbolForNotification "memo" ] . "Output:\n" . $Content) \
|
|
|
|
([ $SymbolForNotification "memo" ] . "No output.") ]) });
|
2025-02-07 16:14:58 +01:00
|
|
|
$RmDir "tmpfs/telegram-chat";
|
2024-03-04 13:48:01 +01:00
|
|
|
} else={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("The command from update " . $UpdateID . " failed syntax validation!");
|
2025-02-24 15:31:34 +01:00
|
|
|
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; \
|
|
|
|
replyto=($Message->"message_id"); threadid=$ThreadId; \
|
2024-03-04 13:48:01 +01:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
2024-05-24 20:00:03 +02:00
|
|
|
message=([ $SymbolForNotification "gear" ] . "Command:\n" . $Command . "\n\n" . \
|
2024-03-21 13:39:56 +01:00
|
|
|
[ $SymbolForNotification "cross-mark" ] . "The command failed syntax validation!") });
|
2023-10-16 15:04:22 +02:00
|
|
|
}
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
|
|
|
} else={
|
|
|
|
:local MessageText ("Received a message from untrusted contact " . \
|
|
|
|
[ $IfThenElse ([ :len ($From->"username") ] = 0) "without username" ("'" . $From->"username" . "'") ] . \
|
|
|
|
" (ID " . $From->"id" . ") in update " . $UpdateID . "!");
|
2024-05-24 20:00:03 +02:00
|
|
|
:if ($Command ~ ("^! *" . [ $EscapeForRegEx $Identity ] . "\$")) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint warning $ScriptName $MessageText;
|
2025-02-24 15:31:34 +01:00
|
|
|
$SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; \
|
|
|
|
replyto=($Message->"message_id"); threadid=$ThreadId; \
|
2023-10-16 15:04:22 +02:00
|
|
|
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
|
2024-03-04 13:48:01 +01:00
|
|
|
message=("You are not trusted.") });
|
2023-10-16 15:04:22 +02:00
|
|
|
} else={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName $MessageText;
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
2023-10-16 15:04:22 +02:00
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
} else={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint debug $ScriptName ("Already handled update " . $UpdateID . ".");
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-04 13:48:01 +01:00
|
|
|
:set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
|
|
|
|
[ $IfThenElse ($UpdateID >= $TelegramChatOffset->2) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
|
2024-12-06 10:31:52 +01:00
|
|
|
}
|