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.
This commit is contained in:
Kumi 2024-04-16 12:42:55 +02:00
parent 62e7bc7e7b
commit b4f0da0d1c
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -1,8 +1,9 @@
from typing import Dict, Any 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): 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}" 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