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