mod/notification-ntfy: support basic auth

Closes #59
This commit is contained in:
Christian Hesse 2024-04-09 23:46:32 +02:00
parent 5f76c245b0
commit ca2e5f2a01
5 changed files with 20 additions and 4 deletions

View file

@ -47,6 +47,11 @@ Then reload the configuration.
> [`global-config`](../../global-config.rsc) (the one without `-overlay`) to
> your local `global-config-overlay` and modify it to your specific needs.
Using a paid account or running a server on-premises allows to add additional
basic authentication. Configure `NtfyServerUser` and `NtfyServerPass` for this.
Even authentication via access token is possible, adding it as password with
a blank username.
Usage and invocation
--------------------

View file

@ -56,6 +56,8 @@
# install the module:
# $ScriptInstallUpdate mod/notification-ntfy
:global NtfyServer "ntfy.sh";
:global NtfyServerUser [];
:global NtfyServerPass [];
:global NtfyTopic "";
# It is possible to override e-mail, Telegram, Matrix and Ntfy setting

View file

@ -12,7 +12,7 @@
:local ScriptName [ :jobname ];
# expected configuration version
:global ExpectedConfigVersion 126;
:global ExpectedConfigVersion 127;
# global variables not to be changed by user
:global GlobalFunctionsReady false;

View file

@ -38,7 +38,8 @@
:if ([ :typeof $Message ] = "array" ) do={
:do {
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
($Message->"url") http-header-field=($Message->"headers") http-data=($Message->"text") as-value;
http-header-field=($Message->"headers") http-data=($Message->"text") \
($Message->"url") user=($Message->"user") password=($Message->"pass") as-value;
:set ($NtfyQueue->$Id);
} on-error={
$LogPrint debug $0 ("Sending queued Ntfy message failed.");
@ -62,6 +63,10 @@
:global NtfyQueue;
:global NtfyServer;
:global NtfyServerOverride;
:global NtfyServerPass;
:global NtfyServerPassOverride;
:global NtfyServerUser;
:global NtfyServerUserOverride;
:global NtfyTopic;
:global NtfyTopicOverride;
@ -73,6 +78,8 @@
:global UrlEncode;
:local Server [ $EitherOr ($NtfyServerOverride->($Notification->"origin")) $NtfyServer ];
:local User [ $EitherOr ($NtfyServerUserOverride->($Notification->"origin")) $NtfyServerUser ];
:local Pass [ $EitherOr ($NtfyServerPassOverride->($Notification->"origin")) $NtfyServerPass ];
:local Topic [ $EitherOr ($NtfyTopicOverride->($Notification->"origin")) $NtfyTopic ];
:if ([ :len $Topic ] = 0) do={
@ -95,7 +102,7 @@
}
}
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
$Url http-header-field=$Headers http-data=$Text as-value;
http-header-field=$Headers http-data=$Text $Url user=$User password=$Pass as-value;
} on-error={
$LogPrint info $0 ("Failed sending ntfy notification! Queuing...");
@ -105,7 +112,8 @@
:set Text ($Text . "\n" . [ $SymbolForNotification "alarm-clock" ] . \
"This message was queued since " . [ /system/clock/get date ] . " " . \
[ /system/clock/get time ] . " and may be obsolete.");
:set ($NtfyQueue->[ :len $NtfyQueue ]) { url=$Url; headers=$Headers; text=$Text };
:set ($NtfyQueue->[ :len $NtfyQueue ]) \
{ url=$Url; user=$User; pass=$Pass; headers=$Headers; text=$Text };
:if ([ :len [ /system/scheduler/find where name="_FlushNtfyQueue" ] ] = 0) do={
/system/scheduler/add name="_FlushNtfyQueue" interval=1m start-time=startup \
on-event=(":global FlushNtfyQueue; \$FlushNtfyQueue;");

View file

@ -51,6 +51,7 @@
124="Added support for links in 'netwatch-notify', these are added below the formatted notification text.";
125=("April's Fool! " . [ $SymbolForNotification "smiley-partying-face" ] . "Well, you missed it... - no charge nor fees. (Anyway... Donations are much appreciated, " . [ $SymbolForNotification "smiley-smiling-face" ] . "thanks!)");
126="Made 'telegram-chat' capable of handling large command output. Telegram messages still limit the size, so it is truncated now.";
127="Added support for authentication to Ntfy notification module.";
};
# Migration steps to be applied on script updates