aboutsummaryrefslogtreecommitdiffstats
path: root/telegram-chat.rsc
diff options
context:
space:
mode:
Diffstat (limited to 'telegram-chat.rsc')
-rw-r--r--telegram-chat.rsc40
1 files changed, 28 insertions, 12 deletions
diff --git a/telegram-chat.rsc b/telegram-chat.rsc
index f2750f5..5db4860 100644
--- a/telegram-chat.rsc
+++ b/telegram-chat.rsc
@@ -1,16 +1,18 @@
#!rsc by RouterOS
# RouterOS script: telegram-chat
-# Copyright (c) 2023-2024 Christian Hesse <mail@eworm.de>
-# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+# Copyright (c) 2023-2025 Christian Hesse <mail@eworm.de>
+# https://rsc.eworm.de/COPYING.md
#
# requires RouterOS, version=7.15
+# requires device-mode, fetch
#
# use Telegram to chat with your Router and send commands
-# https://git.eworm.de/cgit/routeros-scripts/about/doc/telegram-chat.md
+# https://rsc.eworm.de/doc/telegram-chat.md
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
+:local ExitOK false;
:do {
:local ScriptName [ :jobname ];
@@ -35,6 +37,7 @@
:global MIN;
:global MkDir;
:global RandomDelay;
+ :global RmDir;
:global ScriptLock;
:global SendTelegram2;
:global SymbolForNotification;
@@ -43,6 +46,7 @@
:global WaitFullyConnected;
:if ([ $ScriptLock $ScriptName ] = false) do={
+ :set ExitOK true;
:error false;
}
@@ -57,6 +61,7 @@
:if ([ $CertificateAvailable "Go Daddy Root Certificate Authority - G2" ] = false) do={
$LogPrint warning $ScriptName ("Downloading required certificate failed.");
+ :set ExitOK true;
:error false;
}
@@ -82,6 +87,7 @@
:if ($Data = false) do={
$LogPrint warning $ScriptName ("Failed getting updates.");
+ :set ExitOK true;
:error false;
}
@@ -91,13 +97,14 @@
:foreach Update in=($JSON->"result") do={
:set UpdateID ($Update->"update_id");
:local Message ($Update->"message");
- :local IsReply [ :len ($Message->"reply_to_message") ];
+ :local IsReply ([ :typeof ($Message->"reply_to_message") ] = "string");
:local IsMyReply ($TelegramMessageIDs->[ :tostr ($Message->"reply_to_message"->"message_id") ]);
:if (($IsMyReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={
:local Trusted false;
:local Chat ($Message->"chat");
:local From ($Message->"from");
:local Command ($Message->"text");
+ :local ThreadId [ $IfThenElse ($Message->"is_topic_message") ($Message->"message_thread_id") "" ];
:foreach IdsTrusted in=($TelegramChatId, $TelegramChatIdsTrusted) do={
:if ($From->"id" = $IdsTrusted || $From->"username" = $IdsTrusted) do={
@@ -109,9 +116,11 @@
:local Done false;
:if ($Command = "?") do={
$LogPrint info $ScriptName ("Sending notice for update " . $UpdateID . ".");
- $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
+ $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; \
+ replyto=($Message->"message_id"); threadid=$ThreadId; \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
- message=("Online" . [ $IfThenElse $TelegramChatActive " (and active!)" ] . ", awaiting your commands!") });
+ message=([ $IfThenElse ([ :len ($From->"first_name") ] > 0) ("Hello " . ($From->"first_name") . "!\n\n") ] . \
+ "Online" . [ $IfThenElse $TelegramChatActive " (and active!)" ] . ", awaiting your commands!") });
:set Done true;
}
:if ($Done = false && [ :pick $Command 0 1 ] = "!") do={
@@ -124,12 +133,14 @@
" from update " . $UpdateID . "!");
:set Done true;
}
- :if ($Done = false && ($IsMyReply = 1 || ($IsReply = 0 && $TelegramChatActive = true)) && [ :len $Command ] > 0) do={
+ :if ($Done = false && ($IsMyReply = 1 || ($IsReply = false && \
+ $TelegramChatActive = true)) && [ :len $Command ] > 0) do={
:if ([ $ValidateSyntax $Command ] = true) do={
:local State "";
:local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]);
:if ([ $MkDir "tmpfs/telegram-chat" ] = false) do={
$LogPrint error $ScriptName ("Failed creating directory!");
+ :set ExitOK true;
:error false;
}
$LogPrint info $ScriptName ("Running command from update " . $UpdateID . ": " . $Command);
@@ -142,16 +153,18 @@
:set State ([ $SymbolForNotification "cross-mark" ] . "The command failed with an error!\n\n");
}
:local Content ([ /file/read chunk-size=32768 file=$File as-value ]->"data");
- $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
+ $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=true; \
+ replyto=($Message->"message_id"); threadid=$ThreadId; \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=([ $SymbolForNotification "gear" ] . "Command:\n" . $Command . "\n\n" . \
$State . [ $IfThenElse ([ :len $Content ] > 0) \
([ $SymbolForNotification "memo" ] . "Output:\n" . $Content) \
([ $SymbolForNotification "memo" ] . "No output.") ]) });
- /file/remove "tmpfs/telegram-chat";
+ $RmDir "tmpfs/telegram-chat";
} else={
$LogPrint info $ScriptName ("The command from update " . $UpdateID . " failed syntax validation!");
- $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
+ $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; \
+ replyto=($Message->"message_id"); threadid=$ThreadId; \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=([ $SymbolForNotification "gear" ] . "Command:\n" . $Command . "\n\n" . \
[ $SymbolForNotification "cross-mark" ] . "The command failed syntax validation!") });
@@ -163,7 +176,8 @@
" (ID " . $From->"id" . ") in update " . $UpdateID . "!");
:if ($Command ~ ("^! *" . [ $EscapeForRegEx $Identity ] . "\$")) do={
$LogPrint warning $ScriptName $MessageText;
- $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \
+ $SendTelegram2 ({ origin=$ScriptName; chatid=($Chat->"id"); silent=false; \
+ replyto=($Message->"message_id"); threadid=$ThreadId; \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=("You are not trusted.") });
} else={
@@ -176,4 +190,6 @@
}
:set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
[ $IfThenElse ($UpdateID >= $TelegramChatOffset->2) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
-} on-error={ }
+} on-error={
+ :global ExitError; $ExitError $ExitOK [ :jobname ];
+}