From 21bb17743f7845be112127c755e50e9a1861c5ba Mon Sep 17 00:00:00 2001 From: pataar Date: Tue, 30 Aug 2022 22:27:47 +0200 Subject: [PATCH] notify: add ability to notify using a remote action Signed-off-by: Pieter Willekens --- docs/configuration.md | 22 +++++++++++++++++++++- moonraker/components/notifier.py | 18 ++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 0c5c615..1184c7a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1924,7 +1924,8 @@ events: * body: "Your printer status has changed to {event_name}" # The body of the notification. This option accepts Jinja2 templates. # You can use {event_name} to print the current event trigger name. And {event_args} for -# the arguments that came with it. +# the arguments that came with it. When using the notify functionality in a macro context, you can +# use {event_message} to print out your message. title: # The optional title of the notification. Just as the body, this option accepts Jinja2 templates. attach: @@ -1953,6 +1954,25 @@ url: tgram://{bottoken}/{ChatID} events: error body: {event_args[1].message} attach: http://192.168.1.100/webcam/?action=snapshot + +[notifier gcode_telegram] +url: tgram://{bottoken}/{ChatID} +events: gcode +body: {event_message} +attach: http://192.168.1.100/webcam/?action=snapshot +``` + +#### Notifying from Klipper +It is possible to invoke your notifiers from the Klippy host, this can be done +with a gcode_macro, such as: +```ini +# printer.cfg + +[gcode_macro NOTIFY_FILAMENT_CHANGE] +gcode: + {action_call_remote_method("notify", + name="telegram", + message="Filament change needed!")} ``` ### `[simplyprint]` diff --git a/moonraker/components/notifier.py b/moonraker/components/notifier.py index 3e5ec48..c7f4bed 100644 --- a/moonraker/components/notifier.py +++ b/moonraker/components/notifier.py @@ -35,6 +35,7 @@ class Notifier: prefix_sections = config.get_prefix_sections("notifier") self.register_events(config) + self.register_remote_actions() for section in prefix_sections: cfg = config[section] @@ -53,6 +54,16 @@ class Notifier: continue self.notifiers[notifier.get_name()] = notifier + def register_remote_actions(self): + self.server.register_remote_method("notify", self.notify_action) + + async def notify_action(self, name: str, message: str = ""): + if name not in self.notifiers: + raise self.server.error(f"Notifier '{name}' not found", 404) + notifier = self.notifiers[name] + + await notifier.notify("remote_action", [], message) + def register_events(self, config: ConfigHelper): self.events["started"] = NotifierEvent( @@ -145,10 +156,13 @@ class NotifierInstance: self.apprise.add(self.url) - async def notify(self, event_name: str, event_args: List) -> None: + async def notify( + self, event_name: str, event_args: List, message: str = "" + ) -> None: context = { "event_name": event_name, - "event_args": event_args + "event_args": event_args, + "event_message": message } rendered_title = (