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 import datetime
class Event: 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.id = id
self.time = time self.start = start
self.name = name self.name = name
self.location = location self.location = location
self.organizer = organizer self.organizer = organizer
@ -13,4 +13,4 @@ class Event:
return f'{self.name} ({self.id})' return f'{self.name} ({self.id})'
def __repr__(self): 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 beautifulsoup4
markdown2
nio
requests requests

View File

@ -45,7 +45,7 @@ def process_soup(soup: BeautifulSoup) -> List[Event]:
def get_events_today(events: 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__": if __name__ == "__main__":

View File

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