From b4f0da0d1c3057f87b3b43057ec8e88430b9106c Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 16 Apr 2024 12:42:55 +0200 Subject: [PATCH] feat: enhance Matrix notifier with JSON encoding Improved the Matrix notifier's message delivery by converting data payloads to JSON format before sending. This change includes encoding the message data as UTF-8 and setting the appropriate `Content-Type` header in the request. This adjustment ensures better compatibility with the Matrix API and potentially reduces issues related to content formatting and data interpretation on the receiving end. Refactored import statements for clarity and consistency within the module. --- src/trackbert/notifiers/matrix.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/trackbert/notifiers/matrix.py b/src/trackbert/notifiers/matrix.py index 43b2857..cbd31de 100644 --- a/src/trackbert/notifiers/matrix.py +++ b/src/trackbert/notifiers/matrix.py @@ -1,8 +1,9 @@ from typing import Dict, Any +from urllib.request import Request, urlopen -from urllib.request import urlopen +import json -from trackbert.classes.notifier import BaseNotifier +from ..classes.notifier import BaseNotifier class Matrix(BaseNotifier): @@ -19,6 +20,13 @@ class Matrix(BaseNotifier): url = f"{homeserver}/_matrix/client/r0/rooms/{room_id}/send/m.room.message?access_token={token}" - data = {"msgtype": "m.text", "body": f"{title}\n\n{message}"} + data = json.dumps( + {"msgtype": "m.text", "body": f"{title}\n\n{message}"} + ).encode("utf-8") - urlopen(url, data=data) + req = Request(url, data=data, headers={"Content-Type": "application/json"}) + + urlopen(req) + + +notifier = Matrix