feat(notifiers): Add Matrix notifier support

Introduced a new notifier for Matrix to allow messages to be sent to specified rooms on a Matrix server. This extends the application's notification system, enabling it to interact with Matrix homeservers using room IDs and access tokens defined in the configuration. The added `Matrix` class supports basic notification functionality, including determining if the notifier is enabled and sending messages. This enhancement caters to users within the Matrix ecosystem, providing a more integrated experience for alert and notification management.
This commit is contained in:
Kumi 2024-04-16 10:47:07 +02:00
parent cc7230e8aa
commit d985235393
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -0,0 +1,24 @@
from typing import Dict, Any
from urllib.request import urlopen
from trackbert.classes.notifier import BaseNotifier
class Matrix(BaseNotifier):
def __init__(self, config: Dict[str, Any], *args, **kwargs):
self.config = config
def enabled(self) -> bool:
return bool(self.config)
def notify(self, title: str, message: str, urgent: bool = False) -> None:
homeserver = self.config["homeserver"]
room_id = self.config["room_id"]
token = self.config["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}"}
urlopen(url, data=data)