Replace simplematrixbotlib with nio, remove end time

This commit is contained in:
Felix Albrigtsen 2023-08-18 00:33:33 +02:00
parent b7ed45b883
commit 0109645b0f
4 changed files with 43 additions and 22 deletions

View File

@ -1,9 +1,9 @@
import datetime
class Event:
def __init__(self, id: int, time: datetime.datetime, name: str, location: str, organizer: str, description: str):
def __init__(self, id: int, start: datetime.datetime, name: str, location: str, organizer: str, description: str):
self.id = id
self.time = time
self.start = start
self.name = name
self.location = location
self.organizer = organizer
@ -13,4 +13,4 @@ class Event:
return f'{self.name} ({self.id})'
def __repr__(self):
return f'Event({self.id}, {self.time}, {self.name})'
return f'Event({self.id}, {self.start}, {self.name})'

View File

@ -1,3 +1,4 @@
simplematrixbotlib
beautifulsoup4
markdown2
nio
requests

View File

@ -45,7 +45,7 @@ def process_soup(soup: BeautifulSoup) -> List[Event]:
def get_events_today(events: List[Event]):
return list(filter(lambda e: e.time.date() == datetime.datetime.today().date(), events))
return list(filter(lambda e: e.start.date() == datetime.datetime.today().date(), events))
if __name__ == "__main__":

View File

@ -1,36 +1,56 @@
from event import Event
import os
from nio import AsyncClient
from textwrap import dedent
import simplematrixbotlib as botlib
from markdown2 import Markdown
import asyncio
import datetime
MATRIX_URL="https://matrix.pvv.ntnu.no"
MATRIX_USER="@calbot:pvv.ntnu.no"
MATRIX_PASS="foo"
ANNOUNCEMENT_CHANNEL = "#announcements:pvv.ntnu.no"
MATRIX_URL=os.environ["MATRIX_URL"]
MATRIX_USER=os.environ["MATRIX_USER"]
MATRIX_TOKEN=os.environ["MATRIX_TOKEN"]
ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"]
creds = botlib.Creds(MATRIX_URL, MATRIX_USER, MATRIX_PASS)
bot = botlib.Bot(creds)
client = None
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}'
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 / Todays event: {name}
## 🕒 {start} - {end}
## 📍 {location}
## Dagens arrangement / Event of the Day: "{name}"
#### 🕒 {start}
#### 📍 {location}
#### 🛂 av / by "{organizer}"
{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)
'''.format(name=event.name, start=event.start.strftime('%H:%M'), location=event.location, organizer=event.organizer, description=event.description, url=url)
msgText = dedent(msgText)
if atEveryone:
msgText = msgText + '\n@room'
botResult = await bot.api.send_markdown_message(
return await client.room_send(
room_id=channel,
message=msgText,
msgtype="m.text")
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())