diff --git a/src/pvv_calendar_bot/main.py b/src/pvv_calendar_bot/main.py index f4ea93d..cfe2cad 100644 --- a/src/pvv_calendar_bot/main.py +++ b/src/pvv_calendar_bot/main.py @@ -8,13 +8,12 @@ from nio import AsyncClient from textwrap import dedent from markdown2 import Markdown import asyncio -import datetime from sql_connector import fetch_events, Event MATRIX_URL=os.environ.get("MATRIX_URL","https://matrix.pvv.ntnu.no") MATRIX_USER=os.environ.get("MATRIX_USER","@bot_calendar:pvv.ntnu.no") -MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN","test") -ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL","!announcements:pvv.ntnu.no") +MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN") +ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL", "!announcements:pvv.ntnu.no") client = None @@ -37,8 +36,6 @@ def create_announcement(event,atEveryone): url=url, ) - # msgText = dedent(msgText) - if atEveryone: msgText = msgText + '\n@room' return msgText @@ -46,29 +43,28 @@ def create_announcement(event,atEveryone): async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None: msgText = create_announcement(event,atEveryone) - print(msgText) - # 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) - # } - # ) + 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 sendCalendarEvents() -> None: - # global client - # client = AsyncClient(MATRIX_URL, MATRIX_USER) - # client.access_token = MATRIX_TOKEN + global client + client = AsyncClient(MATRIX_URL, MATRIX_USER) + client.access_token = MATRIX_TOKEN eventsToday = fetch_events() for event in eventsToday: await sendMatrixAnnouncement(event, ANNOUNCEMENT_CHANNEL, False) - # await client.close() + await client.close() def main(): asyncio.run(sendCalendarEvents()) diff --git a/src/pvv_calendar_bot/sql_connector.py b/src/pvv_calendar_bot/sql_connector.py index b2844cf..a7776a4 100644 --- a/src/pvv_calendar_bot/sql_connector.py +++ b/src/pvv_calendar_bot/sql_connector.py @@ -35,8 +35,6 @@ def fetch_events(): db_result = mycursor.fetchall() - - results = [] for row in db_result: results.append(Event(id=row[0],