notifier: add support for 'attach' property

Signed-off-by: Pieter Willekens <me@pataar.nl>
This commit is contained in:
pataar 2022-02-25 15:13:33 +01:00 committed by Eric Callahan
parent 553f8862b5
commit 9d49659884
2 changed files with 40 additions and 3 deletions

View File

@ -1686,7 +1686,7 @@ Enables the notification service. Multiple "notifiers" may be configured,
each with their own section, ie: `[notifier my_discord_server]`, `[notifier my_phone]`.
All notifiers require an url for a service to be set up. Moonraker uses [Apprise](https://github.com/caronc/apprise) internally.
You can find the available services and their corresponding urls here: https://github.com/caronc/apprise/wiki.
You can find the available services and their corresponding urls here: [https://github.com/caronc/apprise/wiki](https://github.com/caronc/apprise/wiki).
```ini
# moonraker.conf
@ -1708,7 +1708,10 @@ body: "Your printer status has changed to {event_name}"
# the arguments that came with it.
title:
# The optional title of the notification. Just as the body, this option accepts Jinja2 templates.
attach:
# An optional attachment. Can be an url of a webcam for example. Note: this isn't available for all
# notification services. You can check if it's supported on the Apprise Wiki. Be aware that links in
# your internal network can only be viewed within your network.
```
#### An example:
@ -1720,6 +1723,17 @@ url: tgram://{bottoken}/{ChatID}
events: started
body: Your printer started printing '{event_args[1].filename}'
[notifier print_completed]
url: tgram://{bottoken}/{ChatID}
events: completed
body: Your printer completed printing '{event_args[1].filename}'
attach: http://192.168.1.100/webcam/?action=snapshot
[notifier print_error]
url: tgram://{bottoken}/{ChatID}
events: error
body: {event_args[1].message}
attach: http://192.168.1.100/webcam/?action=snapshot
```
## Jinja2 Templates

View File

@ -17,6 +17,7 @@ from typing import (
Dict,
Any,
List,
Union,
)
if TYPE_CHECKING:
@ -112,6 +113,14 @@ class NotifierInstance:
self.server = config.get_server()
self.name = name_parts[1]
self.apprise = apprise.Apprise()
self.warned = False
self.attach_requires_file_system_check = True
self.attach = config.get("attach", None)
if self.attach is None or \
(self.attach.startswith("http://") or
self.attach.startswith("https://")):
self.attach_requires_file_system_check = False
url_template = config.gettemplate('url')
self.url = url_template.render()
@ -139,9 +148,23 @@ class NotifierInstance:
event_name if self.body is None else self.body.render(context)
)
# Verify the attachment
if self.attach_requires_file_system_check and self.attach is not None:
fm = self.server.lookup_component("file_manager")
if not fm.can_access_path(self.attach):
if not self.warned:
self.server.add_warning(
f"Attachment of notifier '{self.name}' is not "
"valid. The location of the "
"attachment is not "
"accessible.")
self.warned = True
self.attach = None
await self.apprise.async_notify(
rendered_body.strip(),
rendered_title.strip()
rendered_title.strip(),
attach=self.attach
)
def get_name(self) -> str: