aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2023-10-13 22:06:41 +0200
committerGravatar Christian Hesse <mail@eworm.de>2023-10-17 14:05:03 +0200
commit80c0e47649b0125bc5058dbdd92910e4e4f05f82 (patch)
tree4999738526e28ee57e805400db2b885e106769d6
parent65d05a757b60fda68547b0f4df9ab397fcfd71c6 (diff)
parentbcc10c82855fb37f2672d28cf79125f921962c68 (diff)
Merge branch 'telegram' into next
-rw-r--r--doc/telegram-chat.d/01-chat-specific.avifbin31869 -> 38468 bytes
-rw-r--r--doc/telegram-chat.d/02-chat-all.avifbin48099 -> 54713 bytes
-rw-r--r--doc/telegram-chat.d/03-reply.avifbin0 -> 50126 bytes
-rw-r--r--doc/telegram-chat.md17
-rw-r--r--global-config.rsc2
-rw-r--r--global-functions.rsc54
-rw-r--r--mod/notification-telegram.rsc37
-rw-r--r--news-and-changes.rsc2
-rw-r--r--telegram-chat.rsc120
9 files changed, 146 insertions, 86 deletions
diff --git a/doc/telegram-chat.d/01-chat-specific.avif b/doc/telegram-chat.d/01-chat-specific.avif
index 387dc3a..ab75f78 100644
--- a/doc/telegram-chat.d/01-chat-specific.avif
+++ b/doc/telegram-chat.d/01-chat-specific.avif
Binary files differ
diff --git a/doc/telegram-chat.d/02-chat-all.avif b/doc/telegram-chat.d/02-chat-all.avif
index 32fc181..ed1a389 100644
--- a/doc/telegram-chat.d/02-chat-all.avif
+++ b/doc/telegram-chat.d/02-chat-all.avif
Binary files differ
diff --git a/doc/telegram-chat.d/03-reply.avif b/doc/telegram-chat.d/03-reply.avif
new file mode 100644
index 0000000..515853e
--- /dev/null
+++ b/doc/telegram-chat.d/03-reply.avif
Binary files differ
diff --git a/doc/telegram-chat.md b/doc/telegram-chat.md
index 391042d..397920a 100644
--- a/doc/telegram-chat.md
+++ b/doc/telegram-chat.md
@@ -46,6 +46,8 @@ parameters:
Usage and invocation
--------------------
+### Activating device(s)
+
This script is capable of chatting with multiple devices. By default a
device is passive and not acting on messages. To activate it send a message
containing `! identity` (exclamation mark, optional space and system's
@@ -63,6 +65,21 @@ act on your commands.
Send a single exclamation mark or non-existent identity to make all
devices passive again.
+### Reply to message
+
+Let's assume you received a message from a device before, and want to send
+a command to that device. No need to activate it, you can just reply to
+that message.
+
+![reply to message](telegram-chat.d/03-reply.avif)
+
+Associated messages are cleared on device reboot.
+
+### Ask for devices
+
+Send a message with a single question mark (`?`) to query for devices
+currenty online. The answer can be used for command via reply then.
+
Known limitations
-----------------
diff --git a/global-config.rsc b/global-config.rsc
index 172c4cd..0ea18e5 100644
--- a/global-config.rsc
+++ b/global-config.rsc
@@ -41,8 +41,6 @@
#};
:global TelegramChatGroups "(all)";
#:global TelegramChatGroups "(all|home|office)";
-# This is whether or not to send Telegram messages with fixed-width font.
-:global TelegramFixedWidthFont true;
# You can send Matrix notifications. Configure these settings and
# install the module:
diff --git a/global-functions.rsc b/global-functions.rsc
index 8c96c72..8d06cb1 100644
--- a/global-functions.rsc
+++ b/global-functions.rsc
@@ -12,7 +12,7 @@
:local 0 "global-functions";
# expected configuration version
-:global ExpectedConfigVersion 105;
+:global ExpectedConfigVersion 107;
# global variables not to be changed by user
:global GlobalFunctionsReady false;
@@ -48,6 +48,7 @@
:global MkDir;
:global NotificationFunctions;
:global ParseDate;
+:global ParseJson;
:global ParseKeyValueStore;
:global PrettyPrint;
:global RandomDelay;
@@ -694,6 +695,57 @@
"day"=[ :tonum [ :pick $Date 8 10 ] ] });
}
+# parse JSON into array
+# Warning: This is not a complete parser!
+:set ParseJson do={
+ :local Input [ :tostr $1 ];
+
+ :local Return ({});
+ :local Skip 0;
+
+ :if ([ :pick $Input 0 ] = "{") do={
+ :set Input [ :pick $Input 1 ([ :len $Input ] - 1) ];
+ }
+ :set Input [ :toarray $Input ];
+
+ :for I from=0 to=[ :len $Input ] do={
+ :if ($Skip > 0 || $Input->$I = "\n" || $Input->$I = "\r\n") do={
+ :if ($Skip > 0) do={
+ :set $Skip ($Skip - 1);
+ }
+ } else={
+ :local Done false;
+ :local Key ($Input->$I);
+ :local Val1 ($Input->($I + 1));
+ :local Val2 ($Input->($I + 2));
+ :if ($Val1 = ":") do={
+ :set ($Return->$Key) $Val2;
+ :set Skip 2;
+ :set Done true;
+ }
+ :if ($Done = false && $Val1 = ":[") do={
+ :local Tmp "";
+ :local End;
+ :set Skip 1;
+ :do {
+ :set Skip ($Skip + 1);
+ :local ValX ($Input->($I + $Skip));
+ :set End [ :pick $ValX ([ :len $ValX ] - 1) ];
+ :set Tmp ($Tmp . "},{" . $ValX);
+ } while=($End != "]");
+ :set ($Return->$Key) ("{" . [ :pick $Tmp 0 ([ :len $Tmp ] - 1) ] . "}");
+ :set Done true;
+ }
+ :if ($Done = false) do={
+ :set ($Return->$Key) [ :pick $Val1 1 [ :len $Val1 ] ];
+ :set Skip 1;
+ }
+ }
+ }
+
+ :return $Return;
+}
+
# parse key value store
:set ParseKeyValueStore do={
:local Source $1;
diff --git a/mod/notification-telegram.rsc b/mod/notification-telegram.rsc
index ea47b1a..b5729a4 100644
--- a/mod/notification-telegram.rsc
+++ b/mod/notification-telegram.rsc
@@ -15,9 +15,11 @@
# flush telegram queue
:set FlushTelegramQueue do={
:global TelegramQueue;
+ :global TelegramMessageIDs;
:global IsFullyConnected;
:global LogPrintExit2;
+ :global ParseJson;
:if ([ $IsFullyConnected ] = false) do={
$LogPrintExit2 debug $0 ("System is not fully connected, not flushing.") false;
@@ -34,14 +36,13 @@
:foreach Id,Message in=$TelegramQueue do={
:if ([ :typeof $Message ] = "array" ) do={
:do {
- /tool/fetch check-certificate=yes-without-crl output=none http-method=post \
+ :local Data ([ /tool/fetch check-certificate=yes-without-crl output=user http-method=post \
("https://api.telegram.org/bot" . ($Message->"tokenid") . "/sendMessage") \
- http-data=("chat_id=" . ($Message->"chatid") . \
- "&disable_notification=" . ($Message->"silent") . \
- "&reply_to_message_id=" . ($Message->"replyto") . \
- "&disable_web_page_preview=true&parse_mode=" . ($Message->"parsemode") . \
- "&text=" . ($Message->"text")) as-value;
+ http-data=("chat_id=" . ($Message->"chatid") . "&disable_notification=" . ($Message->"silent") . \
+ "&reply_to_message_id=" . ($Message->"replyto") . "&disable_web_page_preview=true" . \
+ "&parse_mode=MarkdownV2&text=" . ($Message->"text")) as-value ]->"data");
:set ($TelegramQueue->$Id);
+ :set ($TelegramMessageIDs->([ $ParseJson ([ $ParseJson $Data ]->"result") ]->"message_id")) 1;
} on-error={
$LogPrintExit2 debug $0 ("Sending queued Telegram message failed.") false;
:set AllDone false;
@@ -63,7 +64,7 @@
:global IdentityExtra;
:global TelegramChatId;
:global TelegramChatIdOverride;
- :global TelegramFixedWidthFont;
+ :global TelegramMessageIDs;
:global TelegramQueue;
:global TelegramTokenId;
:global TelegramTokenIdOverride;
@@ -73,19 +74,14 @@
:global EitherOr;
:global IfThenElse;
:global LogPrintExit2;
+ :global ParseJson;
:global SymbolForNotification;
:global UrlEncode;
:local EscapeMD do={
- :global TelegramFixedWidthFont;
-
:global CharacterReplace;
:global IfThenElse;
- :if ($TelegramFixedWidthFont != true) do={
- :return ($1 . [ $IfThenElse ($2 = "body") ("\n") "" ]);
- }
-
:local Return $1;
:local Chars {
"body"={ "\\"; "`" };
@@ -111,6 +107,10 @@
:return false;
}
+ :if ([ :typeof $TelegramMessageIDs ] = "nothing") do={
+ :set TelegramMessageIDs ({});
+ }
+
:local Truncated false;
:local Text ("*__" . [ $EscapeMD ("[" . $IdentityExtra . $Identity . "] " . \
($Notification->"subject")) "plain" ] . "__*\n\n");
@@ -133,17 +133,17 @@
(($LenSum - [ :len $Text ]) * 100 / $LenSum) . "%!") "plain" ]);
}
:set Text [ $UrlEncode $Text ];
- :local ParseMode [ $IfThenElse ($TelegramFixedWidthFont = true) "MarkdownV2" "" ];
:do {
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
$LogPrintExit2 warning $0 ("Downloading required certificate failed.") true;
}
- /tool/fetch check-certificate=yes-without-crl output=none http-method=post \
+ :local Data ([ /tool/fetch check-certificate=yes-without-crl output=user http-method=post \
("https://api.telegram.org/bot" . $TokenId . "/sendMessage") \
http-data=("chat_id=" . $ChatId . "&disable_notification=" . ($Notification->"silent") . \
- "&reply_to_message_id=" . ($Notification->"replyto") . \
- "&disable_web_page_preview=true&parse_mode=" . $ParseMode . "&text=" . $Text) as-value;
+ "&reply_to_message_id=" . ($Notification->"replyto") . "&disable_web_page_preview=true" . \
+ "&parse_mode=MarkdownV2&text=" . $Text) as-value ]->"data");
+ :set ($TelegramMessageIDs->([ $ParseJson ([ $ParseJson $Data ]->"result") ]->"message_id")) 1;
} on-error={
$LogPrintExit2 info $0 ("Failed sending telegram notification! Queuing...") false;
@@ -154,8 +154,7 @@
[ $EscapeMD ("This message was queued since " . [ /system/clock/get date ] . \
" " . [ /system/clock/get time ] . " and may be obsolete.") "plain" ]) ]);
:set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; tokenid=$TokenId;
- parsemode=$ParseMode; text=$Text; silent=($Notification->"silent");
- replyto=($Notification->"replyto") };
+ text=$Text; silent=($Notification->"silent"); replyto=($Notification->"replyto") };
:if ([ :len [ /system/scheduler/find where name="\$FlushTelegramQueue" ] ] = 0) do={
/system/scheduler/add name="\$FlushTelegramQueue" interval=1m start-time=startup \
on-event=(":global FlushTelegramQueue; \$FlushTelegramQueue;");
diff --git a/news-and-changes.rsc b/news-and-changes.rsc
index b027fb6..d20ace0 100644
--- a/news-and-changes.rsc
+++ b/news-and-changes.rsc
@@ -19,6 +19,8 @@
103="Dropped hard-coded name and timeout from 'hotspot-to-wpa-cleanup', instead a comment is required for dhcp server now.";
104="All relevant scripts were ported to new wifiwave2 and are available for AX devices now!";
105="Extended 'check-routeros-update' to support automatic update from specific neighbor(s).";
+ 106="Modified 'telegram-chat' to make it act on message replies, without activation. Also made it answer a single question mark with a short notice.";
+ 107="Dropped support for non-fixed width font in Telegram notifications.";
};
# Migration steps to be applied on script updates
diff --git a/telegram-chat.rsc b/telegram-chat.rsc
index 62a6ccc..c17394a 100644
--- a/telegram-chat.rsc
+++ b/telegram-chat.rsc
@@ -17,6 +17,7 @@
:global TelegramChatIdsTrusted;
:global TelegramChatOffset;
:global TelegramChatRunTime;
+:global TelegramMessageIDs;
:global TelegramTokenId;
:global CertificateAvailable;
@@ -26,6 +27,7 @@
:global IfThenElse;
:global LogPrintExit2;
:global MkDir;
+:global ParseJson;
:global ScriptLock;
:global SendTelegram2;
:global SymbolForNotification;
@@ -45,101 +47,91 @@ $WaitFullyConnected;
$LogPrintExit2 warning $0 ("Downloading required certificate failed.") true;
}
-:local JsonGetKey do={
- :local Array [ :toarray $1 ];
- :local Key [ :tostr $2 ];
-
- :for I from=0 to=([ :len $Array ] - 1) do={
- :if (($Array->$I) = $Key) do={
- :if ($Array->($I + 1) = ":") do={
- :return ($Array->($I + 2));
- }
- :return [ :pick ($Array->($I + 1)) 1 [ :len ($Array->($I + 1)) ] ];
- }
- }
-
- :return false;
-}
-
:local Data;
: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");
- :set Data [ :pick $Data ([ :find $Data "[" ] + 1) ([ :len $Data ] - 2) ];
} on-error={
$LogPrintExit2 debug $0 ("Failed getting updates from Telegram.") true;
}
:local UpdateID 0;
:local Uptime [ /system/resource/get uptime ];
-:foreach Update in=[ :toarray $Data ] do={
- :set UpdateID [ $JsonGetKey $Update "update_id" ];
- :if (($TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={
+:foreach UpdateArray in=[ :toarray ([ $ParseJson $Data ]->"result") ] do={
+ :local Update [ $ParseJson $UpdateArray ];
+ :set UpdateID ($Update->"update_id");
+ :local Message [ $ParseJson ($Update->"message") ];
+ :local IsReply [ :len ($Message->"reply_to_message") ];
+ :local IsMyReply ($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id"));
+ :if (($IsMyReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={
:local Trusted false;
- :local Message [ $JsonGetKey $Update "message" ];
- :local MessageId [ $JsonGetKey $Message "message_id" ];
- :local From [ $JsonGetKey $Message "from" ];
- :local FromID [ $JsonGetKey $From "id" ];
- :local FromUserName [ $JsonGetKey $From "username" ];
- :local ChatID [ $JsonGetKey [ $JsonGetKey $Message "chat" ] "id" ];
- :local Text [ $JsonGetKey $Message "text" ];
+ :local Chat [ $ParseJson ($Message->"chat") ];
+ :local From [ $ParseJson ($Message->"from") ];
+
:foreach IdsTrusted in=($TelegramChatId, $TelegramChatIdsTrusted) do={
- :if ($FromID = $IdsTrusted || $FromUserName = $IdsTrusted) do={
+ :if ($From->"id" = $IdsTrusted || $From->"username" = $IdsTrusted) do={
:set Trusted true;
}
}
:if ($Trusted = true) do={
- :if ([ :pick $Text 0 1 ] = "!") do={
- :if ($Text ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={
+ :local Done false;
+ :if ($Message->"text" = "?") do={
+ $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=true; replyto=($Message->"message_id"); \
+ subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
+ message=("Online, awaiting your commands!") });
+ :set Done true;
+ }
+ :if ($Done = false && [ :pick ($Message->"text") 0 1 ] = "!") do={
+ :if ($Message->"text" ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={
:set TelegramChatActive true;
} else={
:set TelegramChatActive false;
}
$LogPrintExit2 info $0 ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \
" from update " . $UpdateID . "!") false;
- } else={
- :if ($TelegramChatActive = true && $Text != false && [ :len $Text ] > 0) do={
- :if ([ $ValidateSyntax $Text ] = true) do={
- :local State "";
- :local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]);
- $MkDir "tmpfs/telegram-chat";
- $LogPrintExit2 info $0 ("Running command from update " . $UpdateID . ": " . $Text) false;
- :execute script=(":do {\n" . $Text . "\n} on-error={ /file/add name=\"" . $File . ".failed\" };" . \
- "/file/add name=\"" . $File . ".done\"") file=$File;
- :if ([ $WaitForFile ($File . ".done") [ $EitherOr $TelegramChatRunTime 20s ] ] = false) do={
- :set State "The command did not finish, still running in background.\n\n";
- }
- :if ([ :len [ /file/find where name=($File . ".failed") ] ] > 0) do={
- :set State "The command failed with an error!\n\n";
- }
- :local Content [ /file/get ($File . ".txt") contents ];
- $SendTelegram2 ({ origin=$0; chatid=$ChatID; silent=false; replyto=$MessageId; \
- subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
- message=("Command:\n" . $Text . "\n\n" . $State . [ $IfThenElse ([ :len $Content ] > 0) \
- ("Output:\n" . $Content) [ $IfThenElse ([ /file/get ($File . ".txt") 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=$ChatID; silent=false; replyto=$MessageId; \
- subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
- message=("Command:\n" . $Text . "\n\nThe command failed syntax validation!") });
+ :set Done true;
+ }
+ :if ($Done = false && ($IsMyReply = 1 || ($IsReply = 0 && $TelegramChatActive = true)) && [ :len ($Message->"text") ] > 0) do={
+ :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\" };" . \
+ "/file/add name=\"" . $File . ".done\"") file=$File;
+ :if ([ $WaitForFile ($File . ".done") [ $EitherOr $TelegramChatRunTime 20s ] ] = false) do={
+ :set State "The command did not finish, still running in background.\n\n";
+ }
+ :if ([ :len [ /file/find where name=($File . ".failed") ] ] > 0) do={
+ :set State "The command failed with an error!\n\n";
}
+ :local Content [ /file/get ($File . ".txt") contents ];
+ $SendTelegram2 ({ origin=$0; 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 . ".txt") 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"); \
+ subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
+ message=("Command:\n" . $Message->"text" . "\n\nThe command failed syntax validation!") });
}
}
} else={
- :local Message ("Received a message from untrusted contact " . \
- [ $IfThenElse ($FromUserName = false) "without username" ("'" . $FromUserName . "'") ] . \
- " (ID " . $FromID . ") in update " . $UpdateID . "!");
- :if ($Text ~ ("^! *" . [ $EscapeForRegEx $Identity ] . "\$")) do={
- $LogPrintExit2 warning $0 $Message false;
- $SendTelegram2 ({ origin=$0; chatid=$ChatID; silent=false; replyto=$MessageId; \
+ :local MessageText ("Received a message from untrusted contact " . \
+ [ $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"); \
subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \
message=("You are not trusted.") });
} else={
- $LogPrintExit2 info $0 $Message false;
+ $LogPrintExit2 info $0 $MessageText false;
}
}
} else={