-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
... for sending notifications via Ntfy (https://ntfy.sh/). TODO: use proper formatting once supported in Android app: binwiederhier/ntfy#889
- Loading branch information
Showing
19 changed files
with
243 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
Send notifications via Ntfy | ||
=========================== | ||
|
||
[⬅️ Go back to main README](../../README.md) | ||
|
||
> ℹ️️ **Info**: This module can not be used on its own but requires the base | ||
> installation. See [main README](../../README.md) for details. | ||
Description | ||
----------- | ||
|
||
This module adds support for sending notifications via | ||
[Ntfy](https://ntfy.sh/). A queue is used to make sure | ||
notifications are not lost on failure but sent later. | ||
|
||
Requirements and installation | ||
----------------------------- | ||
|
||
Just install the module: | ||
|
||
$ScriptInstallUpdate mod/notification-ntfy; | ||
|
||
Also install the Ntfy app on your mobile device or use the | ||
[web app](https://ntfy.sh/app) in a browser of your choice. | ||
|
||
Configuration | ||
------------- | ||
|
||
Creating an account is not required. Just choose a topic and you are good | ||
to go. | ||
|
||
> ⚠️ **Warning**: If you use ntfy without sign-up, the topic is essentially | ||
> a password, so pick something that's not easily guessable. | ||
Edit `global-config-overlay`, add `NtfyServer` (leave it unchanged, unless | ||
you are self-hosting the service) and `NtfyTopic` with your choosen topic. | ||
Then reload the configuration. | ||
|
||
> ℹ️ **Info**: Copy relevant configuration from | ||
> [`global-config`](../../global-config.rsc) (the one without `-overlay`) to | ||
> your local `global-config-overlay` and modify it to your specific needs. | ||
Usage and invocation | ||
-------------------- | ||
|
||
There's nothing special to do. Every script or function sending a notification | ||
will now send it to your Ntfy topic. | ||
|
||
But of course you can use the function to send notifications directly. Give | ||
it a try: | ||
|
||
$SendNtfy "Subject..." "Body..." | ||
|
||
Alternatively this sends a notification with all available and configured | ||
methods: | ||
|
||
$SendNotification "Subject..." "Body..." | ||
|
||
To use the functions in your own scripts you have to declare them first. | ||
Place this before you call them: | ||
|
||
:global SendNtfy; | ||
:global SendNotification; | ||
|
||
In case there is a situation when the queue needs to be purged there is a | ||
function available: | ||
|
||
$PurgeNtfyQueue; | ||
|
||
See also | ||
-------- | ||
|
||
* [Send notifications via e-mail](notification-email.md) | ||
* [Send notifications via Matrix](notification-matrix.md) | ||
* [Send notifications via Telegram](notification-telegram.md) | ||
|
||
--- | ||
[⬅️ Go back to main README](../../README.md) | ||
[⬆️ Go back to top](#top) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!rsc by RouterOS | ||
# RouterOS script: mod/notification-ntfy | ||
# Copyright (c) 2013-2023 Christian Hesse <mail@eworm.de> | ||
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md | ||
# | ||
# send notifications via Ntfy (ntfy.sh) | ||
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/notification-ntfy.md | ||
|
||
:global FlushNtfyQueue; | ||
:global NotificationFunctions; | ||
:global PurgeNtfyQueue; | ||
:global SendNtfy; | ||
:global SendNtfy2; | ||
|
||
# flush ntfy queue | ||
:set FlushNtfyQueue do={ | ||
:global NtfyQueue; | ||
:global NtfyMessageIDs; | ||
|
||
:global IsFullyConnected; | ||
:global LogPrintExit2; | ||
|
||
:if ([ $IsFullyConnected ] = false) do={ | ||
$LogPrintExit2 debug $0 ("System is not fully connected, not flushing.") false; | ||
:return false; | ||
} | ||
|
||
:local AllDone true; | ||
:local QueueLen [ :len $NtfyQueue ]; | ||
|
||
:if ([ :len [ /system/scheduler/find where name=$0 ] ] > 0 && $QueueLen = 0) do={ | ||
$LogPrintExit2 warning $0 ("Flushing Ntfy messages from scheduler, but queue is empty.") false; | ||
} | ||
|
||
:foreach Id,Message in=$NtfyQueue do={ | ||
: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; | ||
:set ($NtfyQueue->$Id); | ||
} on-error={ | ||
$LogPrintExit2 debug $0 ("Sending queued Ntfy message failed.") false; | ||
:set AllDone false; | ||
} | ||
} | ||
} | ||
|
||
:if ($AllDone = true && $QueueLen = [ :len $NtfyQueue ]) do={ | ||
/system/scheduler/remove [ find where name=$0 ]; | ||
:set NtfyQueue; | ||
} | ||
} | ||
|
||
# send notification via ntfy - expects one array argument | ||
:set ($NotificationFunctions->"ntfy") do={ | ||
:local Notification $1; | ||
|
||
:global Identity; | ||
:global IdentityExtra; | ||
:global NtfyQueue; | ||
:global NtfyServer; | ||
:global NtfyServerOverride; | ||
:global NtfyTopic; | ||
:global NtfyTopicOverride; | ||
|
||
:global EitherOr; | ||
:global IfThenElse; | ||
:global LogPrintExit2; | ||
:global SymbolForNotification; | ||
:global UrlEncode; | ||
|
||
:local Server [ $EitherOr ($NtfyServerOverride->($Notification->"origin")) $NtfyServer ]; | ||
:local Topic [ $EitherOr ($NtfyTopicOverride->($Notification->"origin")) $NtfyTopic ]; | ||
|
||
:if ([ :len $Topic ] = 0) do={ | ||
:return false; | ||
} | ||
|
||
:local Url ("https://" . $NtfyServer . "/" . [ $UrlEncode $NtfyTopic ]); | ||
:local Headers ({ ("Priority: " . [ $IfThenElse ($Notification->"silent") "low" "default" ]); \ | ||
("Title: " . "[" . $IdentityExtra . $Identity . "] " . ($Notification->"subject")) }); | ||
:local Text (($Notification->"message") . "\n"); | ||
:if ([ :len ($Notification->"link") ] > 0) do={ | ||
:set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . ($Notification->"link")); | ||
} | ||
|
||
:do { | ||
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \ | ||
$Url http-header-field=$Headers http-data=$Text as-value; | ||
} on-error={ | ||
$LogPrintExit2 info $0 ("Failed sending ntfy notification! Queuing...") false; | ||
|
||
:if ([ :typeof $NtfyQueue ] = "nothing") do={ | ||
:set NtfyQueue ({}); | ||
} | ||
: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 }; | ||
: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;"); | ||
} | ||
} | ||
} | ||
|
||
# purge the Ntfy queue | ||
:set PurgeNtfyQueue do={ | ||
:global NtfyQueue; | ||
|
||
/system/scheduler/remove [ find where name="\$FlushNtfyQueue" ]; | ||
:set NtfyQueue; | ||
} | ||
|
||
# send notification via ntfy - expects at least two string arguments | ||
:set SendNtfy do={ | ||
:global SendNtfy2; | ||
|
||
$SendNtfy2 ({ subject=$1; message=$2; link=$3; silent=$4 }); | ||
} | ||
|
||
# send notification via ntfy - expects one array argument | ||
:set SendNtfy2 do={ | ||
:local Notification $1; | ||
|
||
:global NotificationFunctions; | ||
|
||
($NotificationFunctions->"ntfy") ("\$NotificationFunctions->\"ntfy\"") $Notification; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters