ntfy support - Fixed UTF-8 encoding

This commit is contained in:
Stefano Marinelli 2023-10-15 19:32:34 +02:00
parent 330abd83ba
commit 2577a52c1a

View file

@ -156,15 +156,17 @@ class NTFYNotificationProvider(NotificationProvider):
def send_notification(self, mail_from, mail_subject): def send_notification(self, mail_from, mail_subject):
mail_subject = mail_subject if mail_subject is not None else "No Subject" mail_subject = mail_subject if mail_subject is not None else "No Subject"
mail_from = mail_from if mail_from is not None else "Unknown Sender" mail_from = mail_from if mail_from is not None else "Unknown Sender"
sanitized_subject = mail_subject.encode('latin-1', errors='replace').decode('latin-1')
sanitized_from = mail_from.encode('latin-1', errors='replace').decode('latin-1') # Encode the strings in UTF-8
encoded_from = mail_from.encode('utf-8')
encoded_subject = mail_subject.encode('utf-8')
for ntfy_url in self.ntfy_urls: for ntfy_url in self.ntfy_urls:
try: try:
response = requests.post( response = requests.post(
ntfy_url, ntfy_url,
data=sanitized_from.encode(encoding='utf-8'), data=encoded_from,
headers={"Title": sanitized_subject} headers={"Title": encoded_subject}
) )
if response.status_code == 200: if response.status_code == 200:
print(f"Notification sent successfully to {ntfy_url}!") print(f"Notification sent successfully to {ntfy_url}!")
@ -178,6 +180,7 @@ class NTFYNotificationProvider(NotificationProvider):
finally: finally:
time.sleep(5) # Ensure a delay between notifications time.sleep(5) # Ensure a delay between notifications
class PushoverNotificationProvider(NotificationProvider): class PushoverNotificationProvider(NotificationProvider):
def __init__(self, api_token, user_key): def __init__(self, api_token, user_key):
self.api_token = api_token self.api_token = api_token