Compare commits

...

2 Commits

3 changed files with 31 additions and 26 deletions

View File

@ -106,7 +106,7 @@ in
'' ''
install -Dm600 ${envFile} /run/pvv-calendar-bot/env install -Dm600 ${envFile} /run/pvv-calendar-bot/env
${pkgs.replace-secret}/bin/replace-secret '@MATRIX_ACCESS_TOKEN@' ${cfg.settings.secretsFile} /run/pvv-calendar-bot/env ${pkgs.replace-secret}/bin/replace-secret '@MATRIX_ACCESS_TOKEN@' ${cfg.settings.secretsFile} /run/pvv-calendar-bot/env
#${pkgs.replace-secret}/bin/replace-secret '@MYSQL_PASSWORD@' ${cfg.settings.database.passwordFile} /run/pvv-calendar-bot/env ${pkgs.replace-secret}/bin/replace-secret '@MYSQL_PASSWORD@' ${cfg.settings.database.passwordFile} /run/pvv-calendar-bot/env
''; '';
serviceConfig = { serviceConfig = {

View File

@ -10,15 +10,14 @@ from markdown2 import Markdown
import asyncio import asyncio
from .sql_connector import fetch_events, Event from .sql_connector import fetch_events, Event
MATRIX_URL=os.environ.get("MATRIX_URL","https://matrix.pvv.ntnu.no") MATRIX_URL=os.environ.get("MATRIX_URL","https://matrix.pvv.ntnu.no").strip()
MATRIX_USER=os.environ.get("MATRIX_USER","@bot_calendar:pvv.ntnu.no") MATRIX_USER=os.environ.get("MATRIX_USER","@bot_calendar:pvv.ntnu.no").strip()
MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN") MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN").strip()
ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL", "!announcements:pvv.ntnu.no") ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL", "!announcements:pvv.ntnu.no").strip()
client = None client = None
def create_announcement(event: Event, atEveryone: bool) -> str:
def create_announcement(event,atEveryone):
url = "https://www.pvv.ntnu.no/hendelser/info.php?id={}".format(event.id) url = "https://www.pvv.ntnu.no/hendelser/info.php?id={}".format(event.id)
msgText = dedent('''\ msgText = dedent('''\
## Dagens arrangement / Event of the Day: "{name}" ## Dagens arrangement / Event of the Day: "{name}"
@ -38,10 +37,15 @@ def create_announcement(event,atEveryone):
if atEveryone: if atEveryone:
msgText = msgText + '\n@room' msgText = msgText + '\n@room'
return msgText return msgText
async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None: async def sendMatrixAnnouncement(
event: Event,
channel: str = ANNOUNCEMENT_CHANNEL,
atEveryone: bool = False
) -> None:
msgText = create_announcement(event,atEveryone) msgText = create_announcement(event,atEveryone)
return await client.room_send( return await client.room_send(
room_id=channel, room_id=channel,
@ -50,7 +54,7 @@ async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANN
"msgtype": "m.text", "msgtype": "m.text",
"body": msgText, "body": msgText,
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"formatted_body": Markdown().convert(msgText) "formatted_body": Markdown().convert(msgText),
} }
) )

View File

@ -20,31 +20,32 @@ class Event:
def fetch_events(): def fetch_events():
mydb = mysql.connector.connect( mydb = mysql.connector.connect(
host = os.environ.get("MYSQL_URL","mysql.pvv.ntnu.no"), host = os.environ.get("MYSQL_URL","mysql.pvv.ntnu.no").strip(),
user = os.environ.get("MYSQL_USER","calendar-bot"), user = os.environ.get("MYSQL_USER","calendar-bot").strip(),
password = os.environ["MYSQL_PASSWORD"], #lmao password = os.environ["MYSQL_PASSWORD"].strip(),
database = "www-data_nettside" database = "www-data_nettside",
) )
mycursor = mydb.cursor() mycursor = mydb.cursor()
mycursor.execute(""" mycursor.execute("""
SELECT id,name,start,stop,location,description,organiser SELECT id,name,start,stop,location,description,organiser
FROM events FROM events
WHERE DATE(start) = CURDATE() WHERE DATE(start) = CURDATE()
""") """)
db_result = mycursor.fetchall() db_result = mycursor.fetchall()
results = [] results = []
for row in db_result: for row in db_result:
results.append(Event(id=row[0], results.append(Event(
name=row[1], id = row[0],
start=datetime.fromisoformat(row[2]), name = row[1],
stop=datetime.fromisoformat(row[3]), start = datetime.fromisoformat(row[2]),
location=row[4], stop = datetime.fromisoformat(row[3]),
description=row[5], location = row[4],
organiser=row[6], description = row[5],
)) organiser = row[6],
))
return results return results