add webhook
This commit is contained in:
parent
eb8cdefab4
commit
80891eeda2
4 changed files with 69 additions and 5 deletions
|
@ -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"
|
||||
...
|
||||
|
|
|
@ -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__":
|
||||
|
|
|
@ -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:
|
||||
"""
|
||||
|
|
35
src/kassandra/webhook.py
Normal file
35
src/kassandra/webhook.py
Normal 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()
|
Loading…
Reference in a new issue