Route projects to different rooms

This commit is contained in:
jeltz 2021-03-15 23:59:36 +01:00
parent 0ec2ccf3fb
commit b3777e8157
2 changed files with 21 additions and 5 deletions

22
bot.py
View file

@ -3,6 +3,7 @@ import argparse
import asyncio
import dataclasses
import logging
import typing
import aiohttp.web
import jinja2
@ -18,7 +19,7 @@ class Config:
matrix_homeserver: str
matrix_user: str
matrix_password: str
matrix_room: str
matrix_rooms: typing.Mapping[int, str]
webhook_token: str
@ -75,6 +76,19 @@ async def format_events(config, src, dest):
while True:
event = await src.get()
logging.debug("Formatting message: %s", event)
try:
project = event["event_data"]["project_id"]
except KeyError:
logging.warning("Missing project ID: %s", event)
continue
try:
room = config.matrix_rooms[project]
except KeyError:
logging.info("Unknown project ID: %d", project)
continue
try:
template = env.from_string(TEMPLATES[event["event_name"]])
except KeyError:
@ -82,7 +96,7 @@ async def format_events(config, src, dest):
continue
rendered = template.render(**event)
await dest.put(rendered)
await dest.put((room, rendered))
async def send_notices(config, src):
@ -90,10 +104,10 @@ async def send_notices(config, src):
await client.login(config.matrix_password)
while True:
formatted = str(await src.get())
room, formatted = await src.get()
await client.room_send(
config.matrix_room,
room,
message_type="m.room.message",
content={
"msgtype": "m.notice",

View file

@ -4,6 +4,8 @@ listen_port: 8000
matrix_homeserver: https://matrix.org
matrix_user: "@mykanbot:matrix.org"
matrix_password: "CHANGE_ME"
matrix_romm: "!change_me:matrix.org"
matrix_room:
1: "!change_me_project_1:matrix.org"
42: "!change_me_project_42:matrix.org"
webhook_token: "change_me"
...