diff options
author | Christian Hesse <mail@eworm.de> | 2020-09-14 22:56:52 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2020-09-14 22:56:52 +0200 |
commit | 1b68e42bc6116e16cbf284de1086c6d1a18f07f2 (patch) | |
tree | 8db67a6e0043b642f82ace2bc255a3f385b4908d /global-functions | |
parent | 0c111a2ebe4ffbd070e34c47c836f42cd75c32f8 (diff) |
global-functions: queue Telegram notifications on failure and re-send
This introduces function $FlushTelegramQueue, which flushes queue of
Telegram notifications. Notifications are appended to the queue if
sending failed.
Diffstat (limited to 'global-functions')
-rw-r--r-- | global-functions | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/global-functions b/global-functions index 26c180b..1cacde6 100644 --- a/global-functions +++ b/global-functions @@ -24,6 +24,7 @@ :global DeviceInfo; :global DNSIsResolving; :global DownloadPackage; +:global FlushTelegramQueue; :global GetMacVendor; :global GetRandom20CharHex; :global GetRandomNumber; @@ -280,6 +281,36 @@ :return false; } +# flush telegram queue +:set FlushTelegramQueue do={ + :global TelegramQueue; + :global TelegramTokenId; + + :global LogPrintExit; + + :local AllDone true; + :local QueueLen [ :len $TelegramQueue ]; + :foreach Id,Message in=$TelegramQueue do={ + :if ([ :typeof $Message ] = "array" ) do={ + :do { + / tool fetch check-certificate=yes-without-crl output=none http-method=post \ + ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ + http-data=("chat_id=" . ($Message->"chatid") . \ + "&disable_notification=" . ($Message->"silent") . \ + "&text=" . ($Message->"text")); + :set ($TelegramQueue->$Id); + } on-error={ + $LogPrintExit debug ("Sending queued Telegram message failed.") false; + :set AllDone false; + } + } + } + + :if ($AllDone = true && $QueueLen = [ :len $TelegramQueue ]) do={ + / system scheduler remove FlushTelegramQueue; + } +} + # get MAC vendor :set GetMacVendor do={ :local Mac [ :tostr $1 ]; @@ -701,9 +732,10 @@ :local Silent [ :tostr $3 ]; :global Identity; - :global TelegramTokenId; :global TelegramChatId; :global TelegramChatIdOverride; + :global TelegramQueue; + :global TelegramTokenId; :global CertificateAvailable; :global LogPrintExit; @@ -718,6 +750,7 @@ :return false; } + :local Text [ $UrlEncode ("[" . $Identity . "] " . $Subject . "\n\n" . $Message) ]; :do { :if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={ $LogPrintExit warning ("Downloading required certificate failed.") true; @@ -725,9 +758,18 @@ / tool fetch check-certificate=yes-without-crl output=none http-method=post \ ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \ - "&text=" . [ $UrlEncode ("[" . $Identity . "] " . $Subject . "\n\n" . $Message) ]); + "&text=" . $Text); } on-error={ - $LogPrintExit warning ("Failed sending telegram notification!") false; + $LogPrintExit warning ("Failed sending telegram notification! Queuing...") false; + + :if ([ :typeof $TelegramQueue ] = "nothing") do={ + :set TelegramQueue [ :toarray "" ]; + } + :set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; text=$Text; silent=$Silent }; + :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;"; + } } } |