aboutsummaryrefslogtreecommitdiffstats
path: root/telegram-chat.rsc
diff options
context:
space:
mode:
Diffstat (limited to 'telegram-chat.rsc')
-rw-r--r--telegram-chat.rsc120
1 files changed, 56 insertions, 64 deletions
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={