from event import Event import os from nio import AsyncClient from textwrap import dedent from markdown2 import Markdown import asyncio import datetime MATRIX_URL=os.environ["MATRIX_URL"] MATRIX_USER=os.environ["MATRIX_USER"] MATRIX_TOKEN=os.environ["MATRIX_TOKEN"] ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"] client = None async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None: url = "https://www.pvv.ntnu.no/hendelser/info.php?id={}".format(event.id) msgText = '''\ ## Dagens arrangement / Event of the Day: "{name}" - 🕒 **{start}** - 📍 **{location}** {description} [Les mer / Read More]({url}) '''.format(name=event.name, start=event.start.strftime('%H:%M'), location=event.location, description=event.description, url=url) msgText = dedent(msgText) if atEveryone: msgText = msgText + '\n@room' return await client.room_send( room_id=channel, message_type="m.room.message", content={ "msgtype": "m.text", "body": msgText, "format": "org.matrix.custom.html", "formatted_body": Markdown().convert(msgText) } ) async def main() -> None: global client client = AsyncClient(MATRIX_URL, MATRIX_USER) client.access_token = MATRIX_TOKEN await sendMatrixAnnouncement(Event(1, datetime.datetime.now(), "Name", "Location", "PVV", "Description"), ANNOUNCEMENT_CHANNEL, False) await client.close() asyncio.run(main())