1
0
Fork 0

ntfy: Handle non-ASCII characters in message

In order to set the message for a notification with an attachment, the
text must be specified in the `Message` request header.  Unfortunately,
HTTP header values are limited to the Latin-1 character set, so Unicode
characters cannot be included.  As of *ntfy* 2.4.0, however, the server
can decode base64-encoded headers using the RFC 2047 scheme.

To maintain compatibility with older *ntfy* servers, the `ntfy` function
will only encode message contents this way if the string cannoto be
encoded as ASCII.
master
Dustin 2023-06-23 09:33:24 -05:00
parent 43bf08eae8
commit 3b432fc7d6
1 changed files with 15 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import base64
import copy
import datetime
import json
@ -51,7 +52,13 @@ def ntfy(
if filename:
headers['Filename'] = filename
if message:
headers['Message'] = message.replace('\n', '\\n')
try:
message.encode("ascii")
except UnicodeEncodeError:
message = rfc2047_base64encode(message)
else:
message = message.replace('\n', '\\n')
headers['Message'] = message
r = requests.put(
url,
headers=headers,
@ -117,6 +124,13 @@ def rbw_code(
return p.stdout.rstrip('\n')
def rfc2047_base64encode(
message: str,
) -> str:
encoded = base64.b64encode(message.encode("utf-8")).decode("ascii")
return f"=?UTF-8?B?{encoded}?="
def firefly_import(csv: Path, config: dict[str, Any], token: str) -> None:
log.debug('Importing transactions from %s to Firefly III', csv)
env = {