From 55d5f6bd1a1680f3860709a4f907f52f4094e0df Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Fri, 12 May 2023 08:26:51 -0500 Subject: [PATCH] Include error with ntfy failure messages Although it is undocumented, *ntfy* accepts a `Message` header along with a file upload, which sets the message content of the notification when a file is attached. Since HTTP headers cannot contain multiple lines, the newline character has to be escaped. The *ntfy* server performs unescaping automatically. --- xactfetch.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/xactfetch.py b/xactfetch.py index ec32b7d..6b09053 100644 --- a/xactfetch.py +++ b/xactfetch.py @@ -47,20 +47,22 @@ def ntfy( if tags: headers['Tags'] = tags url = f'{NTFY_URL}/{topic}' - if message: - r = requests.post( - url, - headers=headers, - data=message, - ) - else: + if attach: if filename: headers['Filename'] = filename + if message: + headers['Message'] = message.replace('\n', '\\n') r = requests.put( url, headers=headers, data=attach, ) + else: + r = requests.post( + url, + headers=headers, + data=message, + ) r.raise_for_status() @@ -269,6 +271,7 @@ class ntfyerror: if ss := self.page.screenshot(): save_screenshot(ss) ntfy( + message=str(exc_value), title=f'xactfetch failed for {self.bank}', tags='warning', attach=ss,