]> Git [git.vados.ru] :: Repos - Mikrotiks/ros.vados.ru.git/commitdiff
Autocommit in repo: ros.vados.ru, generated by owner: root, in 00:45:02 01.05.2026.
authorvados-dev <192440777+vados-dev@users.noreply.github.com>
Thu, 30 Apr 2026 21:45:03 +0000 (00:45 +0300)
committervados-dev <192440777+vados-dev@users.noreply.github.com>
Thu, 30 Apr 2026 21:45:03 +0000 (00:45 +0300)
html/AM-GlobalFunc.rsc
html/mod/AM-TG-Bot.rsc
html/mod/AM-TG-Bot.rsc.diff [new file with mode: 0644]
html/mod/telegram-chat.rsc [new file with mode: 0644]

index 7d800247424d5422b2a4401b73c57a8a4ba5fb45..c4e9ad3de07b850c3bd4c139bdd1168405a1aac6 100644 (file)
@@ -17,9 +17,9 @@
 :global GlobalFuncReady false;
 
 # Git commit id & info, expected configuration version
-:global CommitId "main/320/6a1c0c0f";
+:global CommitId "main/321/3f698a51";
 :global CommitTAG "unknown";
-:global CommitInfo "Autocommit in repo: ros.vados.ru, generated by owner: root, in 00:40:03 01.05.2026.";
+:global CommitInfo "Autocommit in repo: ros.vados.ru, generated by owner: root, in 00:45:02 01.05.2026.";
 
 # The variables are received right here.
 :global GlobalConfVersion 138;
index b85aea60b6296d6143c460b539a50783db6f4629..807b2f61d53eda0c00f510d92cb217ddc2887f5f 100644 (file)
   }
   :set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
     [ $IfThenElse ($UpdateID >= $TelegramChatOffset->2) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
-} do={:global ExitError; $ExitError $ExitOK [ :jobname ] $Err}
+} do={
+  :global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
+}
diff --git a/html/mod/AM-TG-Bot.rsc.diff b/html/mod/AM-TG-Bot.rsc.diff
new file mode 100644 (file)
index 0000000..99af563
--- /dev/null
@@ -0,0 +1,58 @@
+1,5c1,4
+< #!rsc by Vados
+< # RouterOS script: mod/AM-TG-Bot
+< # Script comment: Use Telegram to chat with your Router and send commands
+< # Copyright (c) 2007-2026 Vados <vados@vados.ru>
+< #
+---
+> #!rsc by RouterOS
+> # RouterOS script: telegram-chat
+> # Copyright (c) 2023-2026 Christian Hesse <mail@eworm.de>
+> # https://rsc.eworm.de/COPYING.md
+8a8,11
+> #
+> # use Telegram to chat with your Router and send commands
+> # https://rsc.eworm.de/doc/telegram-chat.md
+> 
+11,12c14,15
+<   :global GlobalConfReady; :global GlobalFuncReady;
+<   :retry { :if ($GlobalConfReady != true || $GlobalFuncReady != true) \
+---
+>   :global GlobalConfigReady; :global GlobalFunctionsReady;
+>   :retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
+44a48
+>   :global WaitFullyConnected;
+51,53c55
+<   :if ([ :len [ /system/scheduler/find where name=$ScriptName ] ] = 0) do={
+<     /system/scheduler/add start-time=startup interval=30s name=$ScriptName on-event="/system/script/run $ScriptName;" comment="Telegram Chat Bot";
+<   }
+---
+>   $WaitFullyConnected;
+68a71
+> 
+73c76
+<         :set Data ([ /tool/fetch check-certificate=yes-without-crl \
+---
+>         :set Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
+75c78
+<           $TelegramChatOffset->0 . "&allowed_updates=%5B%22message%22%5D") output=user as-value ]->"data");
+---
+>           $TelegramChatOffset->0 . "&allowed_updates=%5B%22message%22%5D") as-value ]->"data");
+85a89
+> 
+107a112
+> 
+114a120
+> 
+183c189,191
+<         } else={$LogPrint info $ScriptName $MessageText}
+---
+>         } else={
+>           $LogPrint info $ScriptName $MessageText;
+>         }
+185c193,195
+<     } else={$LogPrint debug $ScriptName ("Already handled update " . $UpdateID . ".")}
+---
+>     } else={
+>       $LogPrint debug $ScriptName ("Already handled update " . $UpdateID . ".");
+>     }
diff --git a/html/mod/telegram-chat.rsc b/html/mod/telegram-chat.rsc
new file mode 100644 (file)
index 0000000..c0fa5c1
--- /dev/null
@@ -0,0 +1,201 @@
+#!rsc by RouterOS
+# RouterOS script: telegram-chat
+# Copyright (c) 2023-2026 Christian Hesse <mail@eworm.de>
+# https://rsc.eworm.de/COPYING.md
+#
+# requires RouterOS, version=7.19
+# requires device-mode, fetch
+#
+# use Telegram to chat with your Router and send commands
+# https://rsc.eworm.de/doc/telegram-chat.md
+
+:local ExitOK false;
+:onerror Err {
+  :global GlobalConfigReady; :global GlobalFunctionsReady;
+  :retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
+      do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
+  :local ScriptName [ :jobname ];
+
+  :global Identity;
+  :global TelegramChatActive;
+  :global TelegramChatGroups;
+  :global TelegramChatId;
+  :global TelegramChatIdsTrusted;
+  :global TelegramChatOffset;
+  :global TelegramChatRunTime;
+  :global TelegramMessageIDs;
+  :global TelegramRandomDelay;
+  :global TelegramTokenId;
+
+  :global CertificateAvailable;
+  :global EitherOr;
+  :global EscapeForRegEx;
+  :global FileExists;
+  :global GetRandom20CharAlNum;
+  :global IfThenElse;
+  :global LogPrint;
+  :global LogPrintVerbose;
+  :global MAX;
+  :global MIN;
+  :global MkDir;
+  :global RandomDelay;
+  :global RmDir;
+  :global ScriptLock;
+  :global SendTelegram2;
+  :global SymbolForNotification;
+  :global ValidateSyntax;
+  :global WaitForFile;
+  :global WaitFullyConnected;
+
+  :if ([ $ScriptLock $ScriptName ] = false) do={
+    :set ExitOK true;
+    :error false;
+  }
+
+  $WaitFullyConnected;
+
+  :if ([ :typeof $TelegramChatOffset ] != "array") do={
+    :set TelegramChatOffset { 0; 0; 0 };
+  }
+  :if ([ :typeof $TelegramRandomDelay ] != "num") do={
+    :set TelegramRandomDelay 0;
+  }
+
+  :if ([ $CertificateAvailable "Go Daddy Root Certificate Authority - G2" "fetch" ] = false) do={
+    $LogPrint warning $ScriptName ("Downloading required certificate failed.");
+    :set ExitOK true;
+    :error false;
+  }
+
+  $RandomDelay $TelegramRandomDelay;
+
+  :local Data false;
+  :for I from=1 to=4 do={
+    :if ($Data = false) do={
+      :onerror Err {
+        :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) ];
+      } do={
+        :if ($I < 4) do={
+          $LogPrint debug $ScriptName ("Fetch failed, " . $I . ". try: " . $Err);
+          :set TelegramRandomDelay [ $MIN 15 ($TelegramRandomDelay + 5) ];
+          :delay (($I * $I) . "s");
+        }
+      }
+    }
+  }
+
+  :if ($Data = false) do={
+    $LogPrint warning $ScriptName ("Failed getting updates.");
+    :set ExitOK true;
+    :error false;
+  }
+
+  :local JSON [ :deserialize from=json value=$Data ];
+  :local UpdateID 0;
+  :local Uptime [ /system/resource/get uptime ];
+  :foreach Update in=($JSON->"result") do={
+    :set UpdateID ($Update->"update_id");
+    $LogPrintVerbose debug $ScriptName ("Update " . $UpdateID . ": " . [ :serialize to=json $Update ]);
+
+    :local Message ($Update->"message");
+    :local IsAnyReply ([ :typeof ($Message->"reply_to_message") ] = "array");
+    :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 || \
+             $Chat->"id" = $IdsTrusted) do={
+          :set Trusted true;
+        }
+      }
+
+      :if ($Trusted = true) do={
+        :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"); threadid=$ThreadId; \
+            subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
+            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={
+          :if ($Command ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={
+            :set TelegramChatActive true;
+          } else={
+            :set TelegramChatActive false;
+          }
+          $LogPrint info $ScriptName ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \
+            " from update " . $UpdateID . "!");
+          :set Done true;
+        }
+        :if ($Done = false && ($IsMyReply = 1 || ($IsAnyReply = 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);
+            :execute script=(":do {\n" . $Command . "\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={
+              :set State ([ $SymbolForNotification "warning-sign" ] . "The command did not finish, still running in background.\n\n");
+            }
+            :if ([ $FileExists ($File . ".failed") ] = true) do={
+              :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"); 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.") ]) });
+            $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"); threadid=$ThreadId; \
+              subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
+              message=([ $SymbolForNotification "gear" ] . "Command:\n" . $Command . "\n\n" . \
+                [ $SymbolForNotification "cross-mark" ] . "The command failed syntax validation!") });
+          }
+        }
+      } else={
+        :local MessageText ("Received a message from untrusted contact " . \
+          [ $IfThenElse ([ :len ($From->"username") ] = 0) "without username" ("'" . $From->"username" . "'") ] . \
+          " (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"); threadid=$ThreadId; \
+            subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
+            message=("You are not trusted.") });
+        } else={
+          $LogPrint info $ScriptName $MessageText;
+        }
+      }
+    } else={
+      $LogPrint debug $ScriptName ("Already handled update " . $UpdateID . ".");
+    }
+  }
+  :set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
+    [ $IfThenElse ($UpdateID >= $TelegramChatOffset->2) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
+} do={
+  :global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
+}