commit 10ae419d7fef1ec42aa57bb6730aa5ba69543c57 Author: Felix Albrigtsen Date: Thu Aug 17 21:28:28 2023 +0200 Initial upload dummy matrix code diff --git a/event.py b/event.py new file mode 100644 index 0000000..785dcb8 --- /dev/null +++ b/event.py @@ -0,0 +1,16 @@ +import datetime + +class Event: + def __init__(self, id: int, start: datetime.datetime, end: datetime.datetime, name: str, location: str, description: str): + self.id = id + self.start = start + self.end = end + self.name = name + self.location = location + self.description = description + + def __str__(self): + return f'{self.name} ({self.id})' + + def __repr__(self): + return f'Event({self.id}, {self.start}, {self.name})' diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d5b1119 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +simplematrixbotlib diff --git a/sendMatrix.py b/sendMatrix.py new file mode 100644 index 0000000..fabd5b8 --- /dev/null +++ b/sendMatrix.py @@ -0,0 +1,36 @@ +from event import Event + +from textwrap import dedent +import simplematrixbotlib as botlib + +MATRIX_URL="https://matrix.pvv.ntnu.no" +MATRIX_USER="@calbot:pvv.ntnu.no" +MATRIX_PASS="foo" +ANNOUNCEMENT_CHANNEL = "#announcements:pvv.ntnu.no" + +creds = botlib.Creds(MATRIX_URL, MATRIX_USER, MATRIX_PASS) +bot = botlib.Bot(creds) + +async def sendAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None: + url = f'https://www.pvv.ntnu.no/hendelser/info.php?id=${id}' + msgText = '''\ + # Dagens arrangement / Todays event: {name} + ## 🕒 {start} - {end} + ## 📍 {location} + + {description} + + [Les mer / Read More]({url}) + '''.format(name=event.name, start=event.start.strftime('%H:%M'), end=event.end.strftime('%H:%M'), location=event.location, description=event.description, url=url) + + msgText = dedent(msgText) + + if atEveryone: + msgText = msgText + '\n@room' + + botResult = await bot.api.send_markdown_message( + room_id=channel, + message=msgText, + msgtype="m.text") + +