add webhook

This commit is contained in:
histausse 2021-10-05 23:36:38 +02:00
parent eb8cdefab4
commit 80891eeda2
Signed by: histausse
GPG key ID: 67486F107F62E9E9
4 changed files with 69 additions and 5 deletions

View file

@ -2,7 +2,9 @@
username: kassandra username: kassandra
homeserver: https://matrix.org homeserver: https://matrix.org
password: beware of greeks bearing gifts password: beware of greeks bearing gifts
allert_rooms: port: 8000
host: 127.0.0.1
alert_rooms:
- "#troy:matrix.org" - "#troy:matrix.org"
- "#ithaca:matrix.org" - "#ithaca:matrix.org"
... ...

View file

@ -1,9 +1,30 @@
import argparse import argparse
import asyncio import asyncio
from typing import (
Any,
NoReturn
)
from matrix_bot.client import Client from matrix_bot.client import Client
from matrix_bot.invite_policy import WhiteList from matrix_bot.invite_policy import WhiteList
from .config import load_config 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(): async def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -11,20 +32,24 @@ async def main():
args = parser.parse_args() args = parser.parse_args()
config = load_config(args.config) config = load_config(args.config)
alert_queue = asyncio.Queue()
client = await Client( client = await Client(
config.username, config.username,
config.homeserver, config.homeserver,
config.password config.password
) )
invite_policy = await WhiteList(client, config.allert_rooms) invite_policy = await WhiteList(client, config.alert_rooms)
client.set_invite_policy(invite_policy) client.set_invite_policy(invite_policy)
# Test: # Test:
for room in config.allert_rooms: for room in config.alert_rooms:
await client.send_message(room, f"Hello from {config.username}") await client.send_message(room, f"Hello from {config.username}")
await asyncio.gather( 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__": if __name__ == "__main__":

View file

@ -10,7 +10,9 @@ class Config:
username: str username: str
homeserver: str homeserver: str
password: str password: str
allert_rooms: list[str] port: int
host: str
alert_rooms: list[str]
def load_config(file:str)->Config: def load_config(file:str)->Config:
""" """

35
src/kassandra/webhook.py Normal file
View file

@ -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()