Files
twitch-miner/TwitchChannelPointsMiner/classes/Matrix.py
0815Cracky ff22f47b90 update
2024-02-27 11:46:37 +01:00

41 lines
1.3 KiB
Python

from textwrap import dedent
import logging
import requests
from urllib.parse import quote
from TwitchChannelPointsMiner.classes.Settings import Events
class Matrix(object):
__slots__ = ["access_token", "homeserver", "room_id", "events"]
def __init__(self, username: str, password: str, homeserver: str, room_id: str, events: list):
self.homeserver = homeserver
self.room_id = quote(room_id)
self.events = [str(e) for e in events]
body = requests.post(
url=f"https://{self.homeserver}/_matrix/client/r0/login",
json={
"user": username,
"password": password,
"type": "m.login.password"
}
).json()
self.access_token = body.get("access_token")
if not self.access_token:
logging.getLogger(__name__).info("Invalid Matrix password provided. Notifications will not be sent.")
def send(self, message: str, event: Events) -> None:
if str(event) in self.events:
requests.post(
url=f"https://{self.homeserver}/_matrix/client/r0/rooms/{self.room_id}/send/m.room.message?access_token={self.access_token}",
json={
"body": dedent(message),
"msgtype": "m.text"
}
)