-
Notifications
You must be signed in to change notification settings - Fork 183
[ntfy] Add dedicated service plugin ntfy
#638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9b495b
Tests: Move `test_ntfy` out of the way
amotl 79ea76a
[ntfy] Add dedicated service plugin `ntfy`
amotl 40e2efd
[ntfy] Use `file` option to signal uploading a local file as attachment
amotl 609d79f
[ntfy] Use RFC 2047 for encoding HTTP header values
amotl 7332f9b
[ntfy] Add more fields: icon, cache, firebase, unifiedpush
amotl b67e390
[ntfy] Improve RFC 2047 header value encoding
amotl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -223,22 +223,22 @@ template arguments. mqttwarn supports propagating them from either the | |
``baseuri`` configuration setting, or from its data dictionary to the Apprise | ||
plugin invocation. | ||
|
||
So, for example, you can propagate parameters to the [Apprise Ntfy plugin] | ||
by either pre-setting them as URL query parameters, like | ||
So, for example, you can propagate parameters to the [Apprise JSON HTTP POST | ||
Notifications plugin] by either pre-setting them as URL query parameters, like | ||
``` | ||
ntfy://user:password@ntfy.example.org/topic1/topic2?email=test@example.org | ||
json://localhost/?:sound=oceanwave | ||
``` | ||
or by submitting them within a JSON-formatted MQTT message, like | ||
```json | ||
{"priority": "high", "tags": "foo,bar", "click": "https://httpbin.org/headers"} | ||
{":sound": "oceanwave", "tags": "foo,bar", "click": "https://httpbin.org/headers"} | ||
``` | ||
|
||
|
||
[Apprise]: https://github.com/caronc/apprise | ||
[Apprise documentation]: https://github.com/caronc/apprise/wiki | ||
[Apprise URL Basics]: https://github.com/caronc/apprise/wiki/URLBasics | ||
[Apprise Notification Services]: https://github.com/caronc/apprise/wiki#notification-services | ||
[Apprise Ntfy plugin]: https://github.com/caronc/apprise/wiki/Notify_ntfy | ||
[Apprise JSON HTTP POST Notifications plugin]: https://github.com/caronc/apprise/wiki/Notify_Custom_JSON | ||
|
||
|
||
### `apprise_single` | ||
|
@@ -256,7 +256,7 @@ Apprise to E-Mail, an HTTP endpoint, and a Discord channel. | |
|
||
```ini | ||
[defaults] | ||
launch = apprise-mail, apprise-json, apprise-discord, apprise-ntfy | ||
launch = apprise-mail, apprise-json, apprise-discord | ||
|
||
[config:apprise-mail] | ||
; Dispatch message as e-mail. | ||
|
@@ -283,16 +283,9 @@ baseuri = 'json://localhost:1234/mqtthook' | |
module = 'apprise_single' | ||
baseuri = 'discord://4174216298/JHMHI8qBe7bk2ZwO5U711o3dV_js' | ||
|
||
[config:apprise-ntfy] | ||
; Dispatch message to ntfy. | ||
; https://github.com/caronc/apprise/wiki/URLBasics | ||
; https://github.com/caronc/apprise/wiki/Notify_ntfy | ||
module = 'apprise_single' | ||
baseuri = 'ntfy://user:password@ntfy.example.org/topic1/topic2' | ||
|
||
[apprise-single-test] | ||
topic = apprise/single/# | ||
targets = apprise-mail:demo, apprise-json, apprise-discord, apprise-ntfy | ||
targets = apprise-mail:demo, apprise-json, apprise-discord | ||
format = Alarm from {device}: {payload} | ||
title = Alarm from {device} | ||
``` | ||
|
@@ -325,7 +318,6 @@ module = 'apprise_multi' | |
targets = { | ||
'demo-http' : [ { 'baseuri': 'json://localhost:1234/mqtthook' }, { 'baseuri': 'json://daq.example.org:5555/foobar' } ], | ||
'demo-discord' : [ { 'baseuri': 'discord://4174216298/JHMHI8qBe7bk2ZwO5U711o3dV_js' } ], | ||
'demo-ntfy' : [ { 'baseuri': 'ntfy://user:password@ntfy.example.org/topic1/topic2' } ], | ||
'demo-mailto' : [ { | ||
'baseuri': 'mailtos://smtp_username:smtp_password@mail.example.org', | ||
'recipients': ['foo@example.org', 'bar@example.org'], | ||
|
@@ -336,7 +328,7 @@ targets = { | |
|
||
[apprise-multi-test] | ||
topic = apprise/multi/# | ||
targets = apprise-multi:demo-http, apprise-multi:demo-discord, apprise-multi:demo-mailto, apprise-multi:demo-ntfy | ||
targets = apprise-multi:demo-http, apprise-multi:demo-discord, apprise-multi:demo-mailto | ||
format = Alarm from {device}: {payload} | ||
title = Alarm from {device} | ||
``` | ||
|
@@ -1746,10 +1738,151 @@ Requires: | |
|
||
### `ntfy` | ||
|
||
Support for [ntfy] is provided through Apprise, see [apprise_single](#apprise_single) | ||
and [apprise_multi](#apprise_multi). | ||
> [ntfy] (pronounce: _notify_) is a simple HTTP-based [pub-sub] notification service. | ||
> It allows you to send notifications to your phone or desktop via scripts from | ||
> any computer, entirely without signup, cost or setup. | ||
> [ntfy is also open source](https://github.com/binwiederhier/ntfy), if you want to | ||
> run an instance on your own premises. | ||
|
||
ntfy uses topics to address communication channels. This topic is part of the | ||
HTTP API URL. | ||
|
||
To use the hosted variant on `ntfy.sh`, just provide an URL including the topic. | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': 'https://ntfy.sh/testdrive', | ||
} | ||
``` | ||
|
||
When running your own instance, you would use a custom URL here. | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': 'http://username:password@localhost:5555/testdrive', | ||
} | ||
``` | ||
|
||
In order to specify more options, please wrap your ntfy URL into a dictionary | ||
under the `url` key. This way, additional options can be added. | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': { | ||
'url': 'https://ntfy.sh/testdrive', | ||
}, | ||
} | ||
``` | ||
|
||
:::{important} | ||
[ntfy publishing options] outlines different ways to marshal data to the ntfy | ||
HTTP API. mqttwarn is using HTTP headers for serializing values, because the | ||
HTTP body will already be used for the attachment file. Because of this, you | ||
are not able to use UTF-8 characters within your message text, they will be | ||
replaced by placeholder characters like `?`. | ||
::: | ||
|
||
{#ntfy-remote-attachments} | ||
#### Remote attachments | ||
In order to submit notifications with an attachment file at a remote location, | ||
use the `attach` field. Optionally, the `filename` field can be used to assign | ||
a different name to the file. | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': { | ||
'url': 'https://ntfy.sh/testdrive', | ||
'attach': 'https://unsplash.com/photos/spdQ1dVuIHw/download?w=320', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
'filename': 'goat.jpg', | ||
}, | ||
} | ||
``` | ||
|
||
{#ntfy-local-attachments} | ||
#### Local attachments | ||
By using the `attachment` option, you can add an attachment to your message, local | ||
to the machine mqttwarn is running on. The file will be uploaded when submitting | ||
the notification, and ntfy will serve it for clients so that you don't have to. In | ||
order to address the file, you can provide a path template, where the transformation | ||
data will also get interpolated into. | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': { | ||
'url': 'https://ntfy.sh/testdrive', | ||
'file': '/tmp/ntfy-attachment-{slot}-{label}.png', | ||
} | ||
} | ||
``` | ||
:::{important} | ||
In order to allow users to **upload** and attach files to notifications, you will | ||
need to enable the corresponding ntfy feature by simply configuring an attachment | ||
cache directory and a base URL (`attachment-cache-dir`, `base-url`), see | ||
[ntfy stored attachments]. | ||
::: | ||
:::{note} | ||
When mqttwarn processes a message, and accessing the file raises an error, it gets | ||
handled gracefully. In this way, notifications will be triggered even when attaching | ||
the file fails for whatever reasons. | ||
::: | ||
|
||
#### Publishing options | ||
You can use all the available [ntfy publishing options], by using the corresponding | ||
option names listed within `NTFY_FIELD_NAMES`, which are: `message`, `title`, `tags`, | ||
`priority`, `actions`, `click`, `attach`, `filename`, `delay`, and `email`. | ||
|
||
You can obtain ntfy option fields from _three_ contexts in total, as implemented | ||
by the `obtain_ntfy_fields` function. Effectively, that means that you can place | ||
them either within the `targets` address descriptor, within the configuration | ||
section, or submit them using a JSON MQTT message and a corresponding decoder | ||
function into the transformation data dictionary. | ||
|
||
For example, you can always send a `priority` field using MQTT/JSON, or use one of | ||
those configuration snippets, which are equivalent. | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': { | ||
'url': 'https://ntfy.sh/testdrive', | ||
'priority': 'high', | ||
} | ||
} | ||
``` | ||
```ini | ||
[config:ntfy] | ||
targets = { | ||
'test': { | ||
'url': 'https://ntfy.sh/testdrive', | ||
} | ||
} | ||
priority = high | ||
``` | ||
|
||
The highest precedence takes data coming in from the transformation data dictionary, | ||
followed by option fields coming in from the per-recipient `targets` address descriptor, | ||
followed by option fields defined on the `[config:ntfy]` configuration section. | ||
|
||
#### Examples | ||
|
||
1. This is another way to write the "[remote attachments](#ntfy-remote-attachments)" | ||
example, where all ntfy options are located on the configuration section, so they | ||
will apply for all configured target addresses. | ||
```ini | ||
[config:ntfy] | ||
targets = {'test': 'https://ntfy.sh/testdrive'} | ||
attach = https://unsplash.com/photos/spdQ1dVuIHw/download?w=320 | ||
filename = goat.jpg | ||
``` | ||
|
||
2. The tutorial [](#processing-frigate-events) explains how to configure mqttwarn to | ||
notify the user with events emitted by Frigate, a network video recorder (NVR) | ||
with realtime local object detection for IP cameras. | ||
|
||
|
||
[ntfy]: https://ntfy.sh/ | ||
[ntfy publishing options]: https://docs.ntfy.sh/publish/ | ||
[ntfy stored attachments]: https://docs.ntfy.sh/config/#attachments | ||
[pub-sub]: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern | ||
|
||
|
||
### `desktopnotify` | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@binwiederhier outlined a potential solution for this slight drawback, and figured it would be trivial to implement on behalf of ntfy, see caronc/apprise#866 (comment). 🌻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said in the other comment; ntfy server does support UTF-8 in headers, so if you send valid UTF-8 in headers it'll work. But because the HTTP spec does not officially support UTF-8, your libraries or language may not support it.
The suggested RFC encoding would fix this, obviously.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Philipp.
Ah. Thanks for mentioning it once again.
Right, the Python client libraries would currently already croak on that detail, raising a corresponding exception. I will see if a workaround can be applied in form of a monkey patch or such.
But we agree it's dangerous, because there may be HTTP intermediaries in between, which might scramble those headers, right?
I will very much appreciate such an improvement to ntfy. Until it will converge, I will eventually look into the monkey patch solution, or just let it sit like it is.
Thank you so much for your swift responses, and keep up the spirit.
With kind regards,
Andreas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works wonderfully: binwiederhier/ntfy#707
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's merged and will be in the next release
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was fast. Thank you very much!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've adjusted the client-side implementation correspondingly with 609d79f. Thank you again!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi again,
at 609d79f#r110220447, you've commented:
This has already been added with binwiederhier/ntfy@59a5077713 now - thank you so much! I've complemented this improvement with b67e390, where RFC 2047 encoding will now only be applied to specific fields,
title
,message
, andtags
.When looking through the available fields once more, I only discovered that the Action's
label
value might be another candidate for being encoded with RFC 2047, in order to use UTF-8 characters on the action buttons, when using HTTP header transport mode. But please consider this only as a discovery on my end, I personally do not have a need for that.Thanks again for the quick turnaround on this matter.
With kind regards,
Andreas.