global-functions: introduce $SendEMail2, $SendNotification2 & $SendTelegram2

These accept just one array as argument. Adding new features is possible
without breaking the API.

These calls are the same for now:

$SendNotification "Subject..." "Message...";
$SendNotification2 ({ subject="Subject..."; message="Message..." });

But the latter will bring more features in future.
This commit is contained in:
Christian Hesse 2021-04-27 17:46:03 +02:00
parent d4c9d1c577
commit c7a2eecd3c

View file

@ -43,8 +43,11 @@
:global ScriptInstallUpdate; :global ScriptInstallUpdate;
:global ScriptLock; :global ScriptLock;
:global SendEMail; :global SendEMail;
:global SendEMail2;
:global SendNotification; :global SendNotification;
:global SendNotification2;
:global SendTelegram; :global SendTelegram;
:global SendTelegram2;
:global SymbolByUnicodeName; :global SymbolByUnicodeName;
:global SymbolForNotification; :global SymbolForNotification;
:global TimeIsSync; :global TimeIsSync;
@ -867,11 +870,16 @@
} }
} }
# send notification via e-mail # send notification via e-mail - expects at lease two string arguments
:set SendEMail do={ :set SendEMail do={
:local Subject [ :tostr $1 ]; :global SendEMail2;
:local Message [ :tostr $2 ];
:local Link [ :tostr $3 ]; $SendEMail2 ({ subject=$1; message=$2; link=$3 });
}
# send notification via e-mail - expects one array argument
:set SendEMail2 do={
:local Notification $1;
:global Identity; :global Identity;
:global EmailGeneralTo; :global EmailGeneralTo;
@ -892,9 +900,9 @@
:local Signature [ / system note get note ]; :local Signature [ / system note get note ];
:set ($EmailQueue->[ :len $EmailQueue ]) { :set ($EmailQueue->[ :len $EmailQueue ]) {
to=$EmailGeneralTo; cc=$EmailGeneralCc; to=$EmailGeneralTo; cc=$EmailGeneralCc;
subject=[ $QuotedPrintable ("[" . $Identity . "] " . $Subject) ]; subject=[ $QuotedPrintable ("[" . $Identity . "] " . ($Notification->"subject")) ];
body=($Message . \ body=(($Notification->"message") . \
[ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \ [ $IfThenElse ([ :len ($Notification->"link") ] > 0) ("\n\n" . ($Notification->"link")) "" ] . \
[ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]) }; [ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]) };
:if ([ :len [ / system scheduler find where name="FlushEmailQueue" ] ] = 0) do={ :if ([ :len [ / system scheduler find where name="FlushEmailQueue" ] ] = 0) do={
/ system scheduler add name=FlushEmailQueue interval=1s start-time=startup \ / system scheduler add name=FlushEmailQueue interval=1s start-time=startup \
@ -902,27 +910,34 @@
} }
} }
# send notification via e-mail and telegram # send notification via e-mail and telegram - expects at lease two string arguments
# Note that attachment is ignored for telegram, silent is ignored for e-mail!
:set SendNotification do={ :set SendNotification do={
:local Subject [ :tostr $1 ]; :global SendNotification2;
:local Message [ :tostr $2 ];
:local Link [ :tostr $3 ];
:local Silent [ :tostr $4 ];
:global SendEMail; $SendNotification2 ({ subject=$1; message=$2; link=$3; silent=$4 });
:global SendTelegram;
$SendEMail $Subject $Message $Link;
$SendTelegram $Subject $Message $Link $Silent;
} }
# send notification via telegram # send notification via e-mail and telegram - expects one array argument
:set SendNotification2 do={
:local Notification $1;
:global SendEMail2;
:global SendTelegram2;
$SendEMail2 $Notification;
$SendTelegram2 $Notification;
}
# send notification via telegram - expects at lease two string arguments
:set SendTelegram do={ :set SendTelegram do={
:local Subject [ :tostr $1 ]; :global SendTelegram2;
:local Message [ :tostr $2 ];
:local Link [ :tostr $3 ]; $SendTelegram2 ({ subject=$1; message=$2; link=$3; silent=$4 });
:local Silent [ :tostr $4 ]; }
# send notification via telegram - expects one array argument
:set SendTelegram2 do={
:local Notification $1;
:global Identity; :global Identity;
:global TelegramChatId; :global TelegramChatId;
@ -975,8 +990,8 @@
} }
:local Truncated false; :local Truncated false;
:local LenLink [ :len $Link ]; :local LenLink [ :len ($Notification->"link") ];
:local Text ("[" . $Identity . "] " . $Subject . "\n\n" . $Message); :local Text ("[" . $Identity . "] " . ($Notification->"subject") . "\n\n" . ($Notification->"message"));
:local LenText [ :len $Text ]; :local LenText [ :len $Text ];
:if ($LenText > (3968 - $LenLink)) do={ :if ($LenText > (3968 - $LenLink)) do={
:set Text [ $EscapeMD ([ :pick $Text 0 (3840 - $LenLink) ] . "...") "body" ]; :set Text [ $EscapeMD ([ :pick $Text 0 (3840 - $LenLink) ] . "...") "body" ];
@ -985,7 +1000,7 @@
:set Text [ $EscapeMD $Text "body" ]; :set Text [ $EscapeMD $Text "body" ];
} }
:if ($LenLink > 0) do={ :if ($LenLink > 0) do={
:set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . [ $EscapeMD $Link "hint" ]); :set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . [ $EscapeMD ($Notification->"link") "hint" ]);
} }
:if ($Truncated = true) do={ :if ($Truncated = true) do={
:set Text ($Text . "\n" . [ $SymbolForNotification "scissors" ] . \ :set Text ($Text . "\n" . [ $SymbolForNotification "scissors" ] . \
@ -1001,7 +1016,7 @@
} }
/ tool fetch check-certificate=yes-without-crl output=none http-method=post \ / tool fetch check-certificate=yes-without-crl output=none http-method=post \
("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \
http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \ http-data=("chat_id=" . $ChatId . "&disable_notification=" . ($Notification->"silent") . \
"&disable_web_page_preview=true&parse_mode=" . $ParseMode . "&text=" . $Text) as-value; "&disable_web_page_preview=true&parse_mode=" . $ParseMode . "&text=" . $Text) as-value;
} on-error={ } on-error={
$LogPrintExit2 info $0 ("Failed sending telegram notification! Queuing...") false; $LogPrintExit2 info $0 ("Failed sending telegram notification! Queuing...") false;
@ -1013,7 +1028,7 @@
[ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \ [ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \
" " . [ / system clock get time ] . " and may be obsolete.") "hint" ]) ]); " " . [ / system clock get time ] . " and may be obsolete.") "hint" ]) ]);
:set ($TelegramQueue->[ :len $TelegramQueue ]) { :set ($TelegramQueue->[ :len $TelegramQueue ]) {
chatid=$ChatId; parsemode=$ParseMode; text=$Text; silent=$Silent }; chatid=$ChatId; parsemode=$ParseMode; text=$Text; silent=($Notification->"silent") };
:if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={ :if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={
/ system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \ / system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \
on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;"; on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;";