-
-
Notifications
You must be signed in to change notification settings - Fork 94
added ntfy notifier #289
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
added ntfy notifier #289
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1985ba
Merge pull request #288 from Der-Henning/dev
Der-Henning 1f9808d
added ntfy notifier
06e0d07
Update src/notifiers/ntfy.py
72ee847
implement some suggestions of @Der-Henning
3b9b5c3
fix typo for ntfy.password
b9b66d1
fix unnecessary warning in case that no authentication parameters are…
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import json | ||
| import logging | ||
|
|
||
| import requests | ||
|
|
||
| from models import Config, Item | ||
| from models.errors import MaskConfigurationError, NtfyConfigurationError | ||
| from notifiers import Notifier | ||
|
|
||
| log = logging.getLogger('tgtg') | ||
|
|
||
|
|
||
| class Ntfy(Notifier): | ||
| """Notifier for ntfy""" | ||
|
|
||
| def __init__(self, config: Config): | ||
| self.enabled = config.ntfy.get("enabled", False) | ||
| self.server = config.ntfy.get("server", "https://ntfy.sh") | ||
| self.topic = config.ntfy.get("topic") | ||
| self.title = config.ntfy.get("title", "tgtg") | ||
| self.body = config.ntfy.get("body") | ||
| self.priority = config.ntfy.get("priority", "default") | ||
| self.tags = config.ntfy.get("tags", "tgtg") | ||
| self.username = config.ntfy.get("username") | ||
| self.password = config.ntfy.get("password") | ||
| self.timeout = config.ntfy.get("timeout", 60) | ||
| self.cron = config.ntfy.get("cron") | ||
| if self.enabled: | ||
| if not self.server or not self.topic: | ||
| raise NtfyConfigurationError() | ||
|
|
||
| self.url = f"{self.server}/{self.topic}" | ||
| log.debug("ntfy url: %s", self.url) | ||
|
|
||
| self.auth = None | ||
| if (self.username and self.password) is not None: | ||
| self.auth = requests.auth.HTTPBasicAuth(self.username, self.password) | ||
| log.debug("Using basic auth with user '%s' for ntfy", self.username) | ||
| elif (self.username or self.password) is not None: | ||
| log.warning("Username or Password missing for ntfy authentication, defaulting to no auth") | ||
|
|
||
| try: | ||
| Item.check_mask(self.title) | ||
| Item.check_mask(self.body) | ||
| Item.check_mask(self.tags) | ||
| except MaskConfigurationError as exc: | ||
| raise NtfyConfigurationError(exc.message) from exc | ||
|
|
||
| def send(self, item: Item) -> None: | ||
| """Sends item information via configured ntfy endpoint""" | ||
| if self.enabled and self.cron.is_now: | ||
|
|
||
| log.debug("Sending ntfy Notification") | ||
|
|
||
| title = item.unmask(self.title) | ||
| title = title.encode("utf-8") | ||
|
|
||
| body = item.unmask(self.body) | ||
m33rc47 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| body = body.encode("utf-8") | ||
|
|
||
| tags = item.unmask(self.tags) | ||
| tags = tags.encode("utf-8") | ||
|
|
||
| log.debug("ntfy body: %s", body) | ||
| headers = { | ||
| "X-Title": title, | ||
| "X-Message": body, | ||
| "X-Priority": self.priority, | ||
| "X-Tags": tags, | ||
| } | ||
| log.debug("ntfy headers: %s", headers) | ||
| res = requests.post(self.url, headers=headers, timeout=self.timeout, auth=self.auth) | ||
| if not res.ok: | ||
| log.error("ntfy Request failed with status code %s", | ||
| res.status_code) | ||
| log.debug("Response content: %s", res.text) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"ntfy: {self.server}/{self.topic}" | ||
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.
The notifier could also be based on the
Webhooknotifier like theIFTTTnotifier. It would only be necessary to add theauthproperty to theWebhooknotifier, which would in general be a good addition.This would centralize the request logic and make maintaining the code easier.
Using
self.__class__.__name__can be used in theWebhooklogs to display the calling Notifier.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.
Good idea, but I think adding authentication to the
WebHookshould be an other pull request, because it is not part of this feature and affectsWebHookandIFTTTfunctionality. But I like the idea to centralize the request logic and will do that. So are you fine with this argumentation?