-
Notifications
You must be signed in to change notification settings - Fork 183
Improve Apprise integration, with a focus on Ntfy #619
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9517ea1
[Ntfy] Correct usage documentation
amotl d4af1f3
[Apprise] Improve integration
amotl cf52fb7
[Apprise] Add deprecation warning about outdated usage
amotl a8307fa
[Ntfy] Add dedicated test case, exercising additional template arguments
amotl 2d3f9ef
[Apprise] Improve documentation about propagating plugin parameters
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
import warnings | ||
|
||
from mqttwarn.services.apprise_single import plugin | ||
|
||
warnings.warn("`mqttwarn.services.apprise` will be removed in a future release of mqttwarn. " | ||
"Please use `mqttwarn.services.apprise_single` or `mqttwarn.services.apprise_multi` instead.", | ||
category=DeprecationWarning) |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2021-2023 The mqttwarn developers | ||
from __future__ import absolute_import | ||
|
||
from functools import lru_cache | ||
from urllib.parse import urlparse, urlencode | ||
|
||
from apprise import Apprise, ContentLocation | ||
|
||
from mqttwarn.model import ProcessorItem | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
def get_all_template_argument_names(): | ||
""" | ||
Inquire all possible parameter names from all Apprise plugins. | ||
""" | ||
a = Apprise(asset=None, location=ContentLocation.LOCAL) | ||
results = a.details() | ||
plugin_infos = results['schemas'] | ||
|
||
all_arg_names = [] | ||
for plugin_info in plugin_infos: | ||
arg_names = plugin_info["details"]["args"].keys() | ||
all_arg_names += arg_names | ||
|
||
return sorted(set(all_arg_names)) | ||
|
||
|
||
def obtain_apprise_arguments(item: ProcessorItem, arg_names: list) -> dict: | ||
""" | ||
Obtain eventual Apprise parameters from data dictionary. | ||
|
||
https://github.com/caronc/apprise/wiki/Notify_ntfy#parameter-breakdown | ||
""" | ||
params = dict() | ||
for arg_name in arg_names: | ||
if isinstance(item.data, dict) and arg_name in item.data: | ||
params[arg_name] = item.data[arg_name] | ||
return params | ||
|
||
|
||
def add_url_params(url: str, params: dict) -> str: | ||
""" | ||
Serialize query parameter dictionary and add it to URL. | ||
""" | ||
url_parsed = urlparse(url) | ||
if params: | ||
seperator = "?" | ||
if url_parsed.query: | ||
seperator = "&" | ||
url += seperator + urlencode(params) | ||
return url |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2021-2023 The mqttwarn developers | ||
from unittest import mock | ||
from unittest.mock import call | ||
|
||
from mqttwarn.model import ProcessorItem as Item | ||
from mqttwarn.util import load_module_by_name | ||
|
||
|
||
@mock.patch("apprise.Apprise", create=True) | ||
@mock.patch("apprise.AppriseAsset", create=True) | ||
def test_ntfy_success(apprise_asset, apprise_mock, srv, caplog): | ||
module = load_module_by_name("mqttwarn.services.apprise_multi") | ||
|
||
item = Item( | ||
addrs=[ | ||
{ | ||
"baseuri": "ntfy://user:password@ntfy.example.org/topic1/topic2?email=test@example.org", | ||
} | ||
], | ||
title="⚽ Message title ⚽", | ||
message="⚽ Notification message ⚽", | ||
data={"priority": "high", "tags": "foo,bar", "click": "https://httpbin.org/headers"}, | ||
) | ||
|
||
outcome = module.plugin(srv, item) | ||
|
||
assert apprise_mock.mock_calls == [ | ||
call(asset=mock.ANY), | ||
call().add( | ||
"ntfy://user:password@ntfy.example.org/topic1/topic2?email=test@example.org" | ||
"&click=https%3A%2F%2Fhttpbin.org%2Fheaders&priority=high&tags=foo%2Cbar" | ||
), | ||
call().notify(body="⚽ Notification message ⚽", title="⚽ Message title ⚽"), | ||
call().notify().__bool__(), | ||
] | ||
|
||
assert "Successfully sent message using Apprise" in caplog.messages | ||
assert outcome is True |
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.
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.
Currently,
tags
are propagated 1:1 from a string to a string, without any transformation in between.The Apprise Ntfy parameter breakdown documentation says:
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.
However, @zoic21 outlined at #607 (comment), that they would like to submit
tags
as alist
? Please raise your voice if this would be important to you, then we can try to add a corresponding translator.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.
Hello,
No string to string is better, I used array because it's mandatory by ntfy when we used json but it's no necessary by apprise
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.
Excellent. Then let's just keep it as is, for the sake of simplicity.