From 4c51b2fe52d8972dd8e84586a1eb700d56c00013 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 10:29:28 +0200 Subject: telegram-chat: do not cover existing variable --- telegram-chat.rsc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 62a6ccc..075161e 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -130,16 +130,16 @@ $WaitFullyConnected; } } } else={ - :local Message ("Received a message from untrusted contact " . \ + :local MessageText ("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; + $LogPrintExit2 warning $0 $MessageText false; $SendTelegram2 ({ origin=$0; chatid=$ChatID; silent=false; replyto=$MessageId; \ subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \ message=("You are not trusted.") }); } else={ - $LogPrintExit2 info $0 $Message false; + $LogPrintExit2 info $0 $MessageText false; } } } else={ -- cgit v1.2.3-70-g09d2 From 8e9734347e365f9a801096664c27bf0f76d0bc4e Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 10:58:19 +0200 Subject: telegram-chat: parse (one level of) JSON into array --- telegram-chat.rsc | 74 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 075161e..73fe6fe 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -45,20 +45,30 @@ $WaitFullyConnected; $LogPrintExit2 warning $0 ("Downloading required certificate failed.") true; } -:local JsonGetKey do={ - :local Array [ :toarray $1 ]; - :local Key [ :tostr $2 ]; +:local ParseJson do={ + :local Input [ :toarray $1 ]; - :for I from=0 to=([ :len $Array ] - 1) do={ - :if (($Array->$I) = $Key) do={ - :if ($Array->($I + 1) = ":") do={ - :return ($Array->($I + 2)); + :local Return ({}); + :local Skip 0; + + :for I from=0 to=([ :len $Input ] - 1) do={ + :if ($Skip > 0 || $Input->$I = "\n" || $Input->$I = "\r\n") do={ + :if ($Skip > 0) do={ + :set $Skip ($Skip - 1); + } + } else={ + :local Key ($Input->$I); + :if ($Input->($I + 1) = ":") do={ + :set ($Return->$Key) ($Input->($I + 2)); + :set Skip 2; + } else={ + :set ($Return->$Key) [ :pick ($Input->($I + 1)) 1 [ :len ($Input->($I + 1)) ] ]; + :set Skip 1; } - :return [ :pick ($Array->($I + 1)) 1 [ :len ($Array->($I + 1)) ] ]; } } - :return false; + :return $Return; } :local Data; @@ -73,26 +83,24 @@ $WaitFullyConnected; :local UpdateID 0; :local Uptime [ /system/resource/get uptime ]; -:foreach Update in=[ :toarray $Data ] do={ - :set UpdateID [ $JsonGetKey $Update "update_id" ]; +:foreach UpdateArray in=[ :toarray $Data ] do={ + :local Update [ $ParseJson $UpdateArray ]; + :set UpdateID ($Update->"update_id"); :if (($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 Message [ $ParseJson ($Update->"message") ]; + :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={ + :if ([ :pick ($Message->"text") 0 1 ] = "!") do={ + :if ($Message->"text" ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={ :set TelegramChatActive true; } else={ :set TelegramChatActive false; @@ -100,13 +108,13 @@ $WaitFullyConnected; $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={ + :if ($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 . ": " . $Text) false; - :execute script=(":do {\n" . $Text . "\n} on-error={ /file/add name=\"" . $File . ".failed\" };" . \ + $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"; @@ -115,27 +123,27 @@ $WaitFullyConnected; :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; \ + $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \ subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \ - message=("Command:\n" . $Text . "\n\n" . $State . [ $IfThenElse ([ :len $Content ] > 0) \ + 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=$ChatID; silent=false; replyto=$MessageId; \ + $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \ subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \ - message=("Command:\n" . $Text . "\n\nThe command failed syntax validation!") }); + message=("Command:\n" . $Message->"text" . "\n\nThe command failed syntax validation!") }); } } } } else={ :local MessageText ("Received a message from untrusted contact " . \ - [ $IfThenElse ($FromUserName = false) "without username" ("'" . $FromUserName . "'") ] . \ - " (ID " . $FromID . ") in update " . $UpdateID . "!"); - :if ($Text ~ ("^! *" . [ $EscapeForRegEx $Identity ] . "\$")) do={ + [ $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=$ChatID; silent=false; replyto=$MessageId; \ + $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \ subject=([ $SymbolForNotification "speech-balloon" ] . "Telegram Chat"); \ message=("You are not trusted.") }); } else={ -- cgit v1.2.3-70-g09d2 From 080b3cbf9d8ff6cde9aeb08650ead5c3eeb9e7b7 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 12:08:17 +0200 Subject: global-functions: make $ParseJson global --- global-functions.rsc | 29 +++++++++++++++++++++++++++++ telegram-chat.rsc | 27 +-------------------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/global-functions.rsc b/global-functions.rsc index 8c96c72..80f1f78 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -48,6 +48,7 @@ :global MkDir; :global NotificationFunctions; :global ParseDate; +:global ParseJson; :global ParseKeyValueStore; :global PrettyPrint; :global RandomDelay; @@ -694,6 +695,34 @@ "day"=[ :tonum [ :pick $Date 8 10 ] ] }); } +# parse JSON into array +# Warning: This is not a complete parser! +:set ParseJson do={ + :local Input [ :toarray $1 ]; + + :local Return ({}); + :local Skip 0; + + :for I from=0 to=([ :len $Input ] - 1) do={ + :if ($Skip > 0 || $Input->$I = "\n" || $Input->$I = "\r\n") do={ + :if ($Skip > 0) do={ + :set $Skip ($Skip - 1); + } + } else={ + :local Key ($Input->$I); + :if ($Input->($I + 1) = ":") do={ + :set ($Return->$Key) ($Input->($I + 2)); + :set Skip 2; + } else={ + :set ($Return->$Key) [ :pick ($Input->($I + 1)) 1 [ :len ($Input->($I + 1)) ] ]; + :set Skip 1; + } + } + } + + :return $Return; +} + # parse key value store :set ParseKeyValueStore do={ :local Source $1; diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 73fe6fe..6083fee 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -26,6 +26,7 @@ :global IfThenElse; :global LogPrintExit2; :global MkDir; +:global ParseJson; :global ScriptLock; :global SendTelegram2; :global SymbolForNotification; @@ -45,32 +46,6 @@ $WaitFullyConnected; $LogPrintExit2 warning $0 ("Downloading required certificate failed.") true; } -:local ParseJson do={ - :local Input [ :toarray $1 ]; - - :local Return ({}); - :local Skip 0; - - :for I from=0 to=([ :len $Input ] - 1) do={ - :if ($Skip > 0 || $Input->$I = "\n" || $Input->$I = "\r\n") do={ - :if ($Skip > 0) do={ - :set $Skip ($Skip - 1); - } - } else={ - :local Key ($Input->$I); - :if ($Input->($I + 1) = ":") do={ - :set ($Return->$Key) ($Input->($I + 2)); - :set Skip 2; - } else={ - :set ($Return->$Key) [ :pick ($Input->($I + 1)) 1 [ :len ($Input->($I + 1)) ] ]; - :set Skip 1; - } - } - } - - :return $Return; -} - :local Data; :do { :set Data ([ /tool/fetch check-certificate=yes-without-crl output=user \ -- cgit v1.2.3-70-g09d2 From bb899b1fb0b20d481fafc7d67193ab003c33a652 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 12:49:10 +0200 Subject: global-functions: $ParseJson: drop superfluous substraction --- global-functions.rsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 80f1f78..12d510b 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -703,7 +703,7 @@ :local Return ({}); :local Skip 0; - :for I from=0 to=([ :len $Input ] - 1) do={ + :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); -- cgit v1.2.3-70-g09d2 From 7df4f9e78c9213dfb1bb9e423e1d55d2bc01dd6d Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 12:54:59 +0200 Subject: global-functions: $ParseJson: handle outher curly brackets --- global-functions.rsc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 12d510b..df374b8 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -698,11 +698,16 @@ # parse JSON into array # Warning: This is not a complete parser! :set ParseJson do={ - :local Input [ :toarray $1 ]; + :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={ -- cgit v1.2.3-70-g09d2 From 8ecde47a7836de8ec0e42ab2d95e80aff43151a8 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 13:07:21 +0200 Subject: mod/notification-telegram: remember ids of sent messages --- mod/notification-telegram.rsc | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/mod/notification-telegram.rsc b/mod/notification-telegram.rsc index ea47b1a..641c6f0 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=" . ($Message->"parsemode") . "&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; @@ -64,6 +65,7 @@ :global TelegramChatId; :global TelegramChatIdOverride; :global TelegramFixedWidthFont; + :global TelegramMessageIDs; :global TelegramQueue; :global TelegramTokenId; :global TelegramTokenIdOverride; @@ -73,6 +75,7 @@ :global EitherOr; :global IfThenElse; :global LogPrintExit2; + :global ParseJson; :global SymbolForNotification; :global UrlEncode; @@ -111,6 +114,10 @@ :return false; } + :if ([ :typeof $TelegramMessageIDs ] = "nothing") do={ + :set TelegramMessageIDs ({}); + } + :local Truncated false; :local Text ("*__" . [ $EscapeMD ("[" . $IdentityExtra . $Identity . "] " . \ ($Notification->"subject")) "plain" ] . "__*\n\n"); @@ -139,11 +146,12 @@ :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=" . $ParseMode . "&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; -- cgit v1.2.3-70-g09d2 From fd1263324dbbb0e80bc99e30d317eca9fd7b264b Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 11 Oct 2023 11:48:49 +0200 Subject: doc/telegram-chat: update screenshots --- doc/telegram-chat.d/01-chat-specific.avif | Bin 31869 -> 38468 bytes doc/telegram-chat.d/02-chat-all.avif | Bin 48099 -> 54713 bytes 2 files changed, 0 insertions(+), 0 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 Binary files a/doc/telegram-chat.d/01-chat-specific.avif and b/doc/telegram-chat.d/01-chat-specific.avif 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 Binary files a/doc/telegram-chat.d/02-chat-all.avif and b/doc/telegram-chat.d/02-chat-all.avif differ -- cgit v1.2.3-70-g09d2 From 399d952ac21057c5ab88120bef4a400b06caea3b Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 10 Oct 2023 13:16:58 +0200 Subject: telegram-chat: act on reply from self --- doc/telegram-chat.d/03-reply.avif | Bin 0 -> 50126 bytes doc/telegram-chat.md | 15 +++++++++++++++ global-functions.rsc | 2 +- news-and-changes.rsc | 1 + telegram-chat.rsc | 4 +++- 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 doc/telegram-chat.d/03-reply.avif 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 Binary files /dev/null and b/doc/telegram-chat.d/03-reply.avif differ diff --git a/doc/telegram-chat.md b/doc/telegram-chat.md index 391042d..3cbc710 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,19 @@ 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. + +> ⚠️ **Warning**: If another device is activated both will act, the one from +> reply and the active one! + Known limitations ----------------- diff --git a/global-functions.rsc b/global-functions.rsc index df374b8..dbedfbc 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 106; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/news-and-changes.rsc b/news-and-changes.rsc index b027fb6..bbbaad6 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -19,6 +19,7 @@ 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."; }; # Migration steps to be applied on script updates diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 6083fee..29130db 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; @@ -83,7 +84,8 @@ $WaitFullyConnected; $LogPrintExit2 info $0 ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \ " from update " . $UpdateID . "!") false; } else={ - :if ($TelegramChatActive = true && [ :len ($Message->"text") ] > 0) do={ + :if (($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id") = 1 || \ + $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ :if ([ $ValidateSyntax ($Message->"text") ] = true) do={ :local State ""; :local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]); -- cgit v1.2.3-70-g09d2 From adca33cc5bcc68911bc811d4b40f387e6b1afd97 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 11 Oct 2023 10:24:04 +0200 Subject: telegram-chat: act on reply without delay --- telegram-chat.rsc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 29130db..f71cbde 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -62,9 +62,10 @@ $WaitFullyConnected; :foreach UpdateArray in=[ :toarray $Data ] do={ :local Update [ $ParseJson $UpdateArray ]; :set UpdateID ($Update->"update_id"); - :if (($TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={ + :local Message [ $ParseJson ($Update->"message") ]; + :local IsReply ($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id")); + :if (($IsReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={ :local Trusted false; - :local Message [ $ParseJson ($Update->"message") ]; :local Chat [ $ParseJson ($Message->"chat") ]; :local From [ $ParseJson ($Message->"from") ]; @@ -84,8 +85,7 @@ $WaitFullyConnected; $LogPrintExit2 info $0 ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \ " from update " . $UpdateID . "!") false; } else={ - :if (($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id") = 1 || \ - $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ + :if (($IsReply = 1 || $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ :if ([ $ValidateSyntax ($Message->"text") ] = true) do={ :local State ""; :local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]); -- cgit v1.2.3-70-g09d2 From 555461c6121465aab748429d6a3226b1a6ca42b4 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 16 Oct 2023 12:50:50 +0200 Subject: telegram-chat: do not nest conditions --- telegram-chat.rsc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index f71cbde..9afac69 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -76,6 +76,7 @@ $WaitFullyConnected; } :if ($Trusted = true) do={ + :local Done false; :if ([ :pick ($Message->"text") 0 1 ] = "!") do={ :if ($Message->"text" ~ ("^! *(" . [ $EscapeForRegEx $Identity ] . "|@" . $TelegramChatGroups . ")\$")) do={ :set TelegramChatActive true; @@ -84,8 +85,9 @@ $WaitFullyConnected; } $LogPrintExit2 info $0 ("Now " . [ $IfThenElse $TelegramChatActive "active" "passive" ] . \ " from update " . $UpdateID . "!") false; - } else={ - :if (($IsReply = 1 || $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ + :set Done true; + } + :if ($Done = false && ($IsReply = 1 || $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ :if ([ $ValidateSyntax ($Message->"text") ] = true) do={ :local State ""; :local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]); @@ -113,7 +115,6 @@ $WaitFullyConnected; message=("Command:\n" . $Message->"text" . "\n\nThe command failed syntax validation!") }); } } - } } else={ :local MessageText ("Received a message from untrusted contact " . \ [ $IfThenElse ([ :len ($From->"username") ] = 0) "without username" ("'" . $From->"username" . "'") ] . \ -- cgit v1.2.3-70-g09d2 From 15873e2fdb20ad74e8ba9f0cf3dd7331ad143809 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 16 Oct 2023 15:04:22 +0200 Subject: telegram-chat: restore indention --- telegram-chat.rsc | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 9afac69..e063e80 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -88,33 +88,33 @@ $WaitFullyConnected; :set Done true; } :if ($Done = false && ($IsReply = 1 || $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=false; 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!") }); + :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=false; 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 MessageText ("Received a message from untrusted contact " . \ [ $IfThenElse ([ :len ($From->"username") ] = 0) "without username" ("'" . $From->"username" . "'") ] . \ -- cgit v1.2.3-70-g09d2 From 1b62545d8c5b186e9059c9640761a12ad336ec7b Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 13 Oct 2023 21:54:50 +0200 Subject: telegram-chat: answer question mark with short notice --- doc/telegram-chat.md | 5 +++++ news-and-changes.rsc | 2 +- telegram-chat.rsc | 8 +++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/telegram-chat.md b/doc/telegram-chat.md index 3cbc710..79a9eb1 100644 --- a/doc/telegram-chat.md +++ b/doc/telegram-chat.md @@ -78,6 +78,11 @@ Associated messages are cleared on device reboot. > ⚠️ **Warning**: If another device is activated both will act, the one from > reply and the active one! +### 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/news-and-changes.rsc b/news-and-changes.rsc index bbbaad6..49b1671 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -19,7 +19,7 @@ 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."; + 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."; }; # Migration steps to be applied on script updates diff --git a/telegram-chat.rsc b/telegram-chat.rsc index e063e80..16edbca 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -77,7 +77,13 @@ $WaitFullyConnected; :if ($Trusted = true) do={ :local Done false; - :if ([ :pick ($Message->"text") 0 1 ] = "!") do={ + :if ($Message->"text" = "?") do={ + $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; 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={ -- cgit v1.2.3-70-g09d2 From bc4839f61130695f504ccd3745dd099540db5206 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 16 Oct 2023 13:05:21 +0200 Subject: telegram-chat: rename variable --- telegram-chat.rsc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 16edbca..1a89544 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -63,8 +63,8 @@ $WaitFullyConnected; :local Update [ $ParseJson $UpdateArray ]; :set UpdateID ($Update->"update_id"); :local Message [ $ParseJson ($Update->"message") ]; - :local IsReply ($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id")); - :if (($IsReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={ + :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 Chat [ $ParseJson ($Message->"chat") ]; :local From [ $ParseJson ($Message->"from") ]; @@ -93,7 +93,7 @@ $WaitFullyConnected; " from update " . $UpdateID . "!") false; :set Done true; } - :if ($Done = false && ($IsReply = 1 || $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ + :if ($Done = false && ($IsMyReply = 1 || $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ :if ([ $ValidateSyntax ($Message->"text") ] = true) do={ :local State ""; :local File ("tmpfs/telegram-chat/" . [ $GetRandom20CharAlNum 6 ]); -- cgit v1.2.3-70-g09d2 From c9233773b3a1dd967d1dd5924c5f00c516d55d41 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 16 Oct 2023 13:07:29 +0200 Subject: telegram-chat: do not act on foreign reply... ... even if active! --- doc/telegram-chat.md | 3 --- telegram-chat.rsc | 3 ++- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/doc/telegram-chat.md b/doc/telegram-chat.md index 79a9eb1..397920a 100644 --- a/doc/telegram-chat.md +++ b/doc/telegram-chat.md @@ -75,9 +75,6 @@ that message. Associated messages are cleared on device reboot. -> ⚠️ **Warning**: If another device is activated both will act, the one from -> reply and the active one! - ### Ask for devices Send a message with a single question mark (`?`) to query for devices diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 1a89544..2bd8ccd 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -63,6 +63,7 @@ $WaitFullyConnected; :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; @@ -93,7 +94,7 @@ $WaitFullyConnected; " from update " . $UpdateID . "!") false; :set Done true; } - :if ($Done = false && ($IsMyReply = 1 || $TelegramChatActive = true) && [ :len ($Message->"text") ] > 0) do={ + :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 ]); -- cgit v1.2.3-70-g09d2 From fed7f2da46fc02b12d23c0f05111452b8e9935e6 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 13 Oct 2023 16:42:24 +0200 Subject: mod/notification-telegram: drop support for non-fixed width font --- global-config.rsc | 2 -- global-functions.rsc | 2 +- mod/notification-telegram.rsc | 15 +++------------ news-and-changes.rsc | 1 + 4 files changed, 5 insertions(+), 15 deletions(-) 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 dbedfbc..4617a3a 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -12,7 +12,7 @@ :local 0 "global-functions"; # expected configuration version -:global ExpectedConfigVersion 106; +:global ExpectedConfigVersion 107; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/mod/notification-telegram.rsc b/mod/notification-telegram.rsc index 641c6f0..b5729a4 100644 --- a/mod/notification-telegram.rsc +++ b/mod/notification-telegram.rsc @@ -40,7 +40,7 @@ ("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 ]->"data"); + "&parse_mode=MarkdownV2&text=" . ($Message->"text")) as-value ]->"data"); :set ($TelegramQueue->$Id); :set ($TelegramMessageIDs->([ $ParseJson ([ $ParseJson $Data ]->"result") ]->"message_id")) 1; } on-error={ @@ -64,7 +64,6 @@ :global IdentityExtra; :global TelegramChatId; :global TelegramChatIdOverride; - :global TelegramFixedWidthFont; :global TelegramMessageIDs; :global TelegramQueue; :global TelegramTokenId; @@ -80,15 +79,9 @@ :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"={ "\\"; "`" }; @@ -140,7 +133,6 @@ (($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={ @@ -150,7 +142,7 @@ ("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 ]->"data"); + "&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; @@ -162,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 49b1671..d20ace0 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -20,6 +20,7 @@ 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 -- cgit v1.2.3-70-g09d2 From 140ec1a3a26af52572e755c126323097e56a4faa Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 16 Oct 2023 23:38:06 +0200 Subject: global-functions: $ParseJson: use temporary variables --- global-functions.rsc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/global-functions.rsc b/global-functions.rsc index 4617a3a..d510ff9 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -715,11 +715,13 @@ } } else={ :local Key ($Input->$I); - :if ($Input->($I + 1) = ":") do={ - :set ($Return->$Key) ($Input->($I + 2)); + :local Val1 ($Input->($I + 1)); + :local Val2 ($Input->($I + 2)); + :if ($Val1 = ":") do={ + :set ($Return->$Key) $Val2; :set Skip 2; } else={ - :set ($Return->$Key) [ :pick ($Input->($I + 1)) 1 [ :len ($Input->($I + 1)) ] ]; + :set ($Return->$Key) [ :pick $Val1 1 [ :len $Val1 ] ]; :set Skip 1; } } -- cgit v1.2.3-70-g09d2 From 3b9df48721c6cb6c3018581749cdbcb6442774a5 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 16 Oct 2023 23:39:27 +0200 Subject: global-functions: $ParseJson: prepare elif-workaround --- global-functions.rsc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index d510ff9..8dc8c02 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -714,13 +714,16 @@ :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; - } else={ + :set Done true; + } + :if ($Done = false) do={ :set ($Return->$Key) [ :pick $Val1 1 [ :len $Val1 ] ]; :set Skip 1; } -- cgit v1.2.3-70-g09d2 From c0aab0feadd1ede2cc2d900811f0678f8901de6a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 13 Oct 2023 22:41:28 +0200 Subject: global-functions: $ParseJson: properly handle array --- global-functions.rsc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/global-functions.rsc b/global-functions.rsc index 8dc8c02..8d06cb1 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -723,6 +723,19 @@ :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; -- cgit v1.2.3-70-g09d2 From 73194b92cf2c4c10b7d45945b15f26c965d03dca Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 13 Oct 2023 22:43:19 +0200 Subject: telegram-chat: use $ParseJson for all JSON --- telegram-chat.rsc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 2bd8ccd..59e56b7 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -52,14 +52,13 @@ $WaitFullyConnected; :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 UpdateArray in=[ :toarray $Data ] do={ +:foreach UpdateArray in=[ :toarray ([ $ParseJson $Data ]->"result") ] do={ :local Update [ $ParseJson $UpdateArray ]; :set UpdateID ($Update->"update_id"); :local Message [ $ParseJson ($Update->"message") ]; -- cgit v1.2.3-70-g09d2 From bcc10c82855fb37f2672d28cf79125f921962c68 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 17 Oct 2023 14:01:10 +0200 Subject: telegram-chat: make messages silent... ... at least those not indicating an error. --- telegram-chat.rsc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram-chat.rsc b/telegram-chat.rsc index 59e56b7..c17394a 100644 --- a/telegram-chat.rsc +++ b/telegram-chat.rsc @@ -78,7 +78,7 @@ $WaitFullyConnected; :if ($Trusted = true) do={ :local Done false; :if ($Message->"text" = "?") do={ - $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \ + $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; @@ -108,7 +108,7 @@ $WaitFullyConnected; :set State "The command failed with an error!\n\n"; } :local Content [ /file/get ($File . ".txt") contents ]; - $SendTelegram2 ({ origin=$0; chatid=($Chat->"id"); silent=false; replyto=($Message->"message_id"); \ + $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) \ -- cgit v1.2.3-70-g09d2