From 80891eeda2d05632998b6b6e29c8c99c785680a5 Mon Sep 17 00:00:00 2001 From: Jean-Marie Mineau Date: Tue, 5 Oct 2021 23:36:38 +0200 Subject: [PATCH] add webhook --- config.yaml.exp | 4 +++- src/kassandra/__main__.py | 31 ++++++++++++++++++++++++++++--- src/kassandra/config.py | 4 +++- src/kassandra/webhook.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/kassandra/webhook.py diff --git a/config.yaml.exp b/config.yaml.exp index 0860f93..f91e69d 100644 --- a/config.yaml.exp +++ b/config.yaml.exp @@ -2,7 +2,9 @@ username: kassandra homeserver: https://matrix.org password: beware of greeks bearing gifts -allert_rooms: +port: 8000 +host: 127.0.0.1 +alert_rooms: - "#troy:matrix.org" - "#ithaca:matrix.org" ... diff --git a/src/kassandra/__main__.py b/src/kassandra/__main__.py index 66f648f..f7be0ae 100644 --- a/src/kassandra/__main__.py +++ b/src/kassandra/__main__.py @@ -1,9 +1,30 @@ import argparse import asyncio +from typing import ( + Any, + NoReturn +) + from matrix_bot.client import Client from matrix_bot.invite_policy import WhiteList from .config import load_config +from .webhook import run_webhook + +async def send_messages( + message_queue: asyncio.Queue[dict[str, Any]], + bot: Client, + rooms: list[str] +)->NoReturn: + """ + Read messages from a queue and send them via the bot. + """ + while True: + message = await message_queue.get() + message = str(message) + for room in rooms: + await bot.send_message(room, message) + message_queue.task_done() async def main(): parser = argparse.ArgumentParser() @@ -11,20 +32,24 @@ async def main(): args = parser.parse_args() config = load_config(args.config) + alert_queue = asyncio.Queue() + client = await Client( config.username, config.homeserver, config.password ) - invite_policy = await WhiteList(client, config.allert_rooms) + invite_policy = await WhiteList(client, config.alert_rooms) client.set_invite_policy(invite_policy) # Test: - for room in config.allert_rooms: + for room in config.alert_rooms: await client.send_message(room, f"Hello from {config.username}") await asyncio.gather( - client.run() + client.run(), + run_webhook(alert_queue, config.host, config.port), + send_messages(alert_queue, client, config.alert_rooms) ) if __name__ == "__main__": diff --git a/src/kassandra/config.py b/src/kassandra/config.py index 92578ad..d695be9 100644 --- a/src/kassandra/config.py +++ b/src/kassandra/config.py @@ -10,7 +10,9 @@ class Config: username: str homeserver: str password: str - allert_rooms: list[str] + port: int + host: str + alert_rooms: list[str] def load_config(file:str)->Config: """ diff --git a/src/kassandra/webhook.py b/src/kassandra/webhook.py new file mode 100644 index 0000000..709959c --- /dev/null +++ b/src/kassandra/webhook.py @@ -0,0 +1,35 @@ +""" +The webhook receiving the alerts from alertmanager. +""" + +import asyncio +import aiohttp.web +import aiohttp.web_request + +from typing import ( + Any, + NoReturn +) + +ENDPOINT = "/webhook" + +async def run_webhook( + alert_queue: asyncio.Queue[dict[str, Any]], + host: str, + port: int +)->NoReturn: + """ + Run the webhook endpoint and put the alerts in the queue. + """ + + async def handler(request:aiohttp.web_request.Request): + alert = await request.json() + await alert_queue.put(alert) + return aiohttp.web.Response() + + app = aiohttp.web.Application() + app.add_routes([aiohttp.web.post(ENDPOINT, handler)]) + runner = aiohttp.web.AppRunner(app) + await runner.setup() + site = aiohttp.web.TCPSite(runner, host, port) + await site.start()