calendar-bot/sendMatrix.py

56 lines
1.6 KiB
Python
Raw Normal View History

2023-08-17 21:28:28 +02:00
from event import Event
import os
from nio import AsyncClient
2023-08-17 21:28:28 +02:00
from textwrap import dedent
from markdown2 import Markdown
import asyncio
import datetime
2023-08-17 21:28:28 +02:00
MATRIX_URL=os.environ["MATRIX_URL"]
MATRIX_USER=os.environ["MATRIX_USER"]
MATRIX_TOKEN=os.environ["MATRIX_TOKEN"]
ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"]
2023-08-17 21:28:28 +02:00
client = None
2023-08-17 21:28:28 +02:00
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)
2023-08-17 21:28:28 +02:00
msgText = '''\
## Dagens arrangement / Event of the Day: "{name}"
2023-08-18 01:21:03 +02:00
- 🕒 **{start}**
- 📍 **{location}**
2023-08-17 21:28:28 +02:00
{description}
[Les mer / Read More]({url})
2023-08-18 01:21:03 +02:00
'''.format(name=event.name, start=event.start.strftime('%H:%M'), location=event.location, description=event.description, url=url)
2023-08-17 21:28:28 +02:00
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)
}
)
2023-08-17 21:28:28 +02:00
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())