You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.4 KiB
Python
57 lines
1.4 KiB
Python
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()
|
|
parser.add_argument("-c", "--config", default="config.yaml")
|
|
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.alert_rooms)
|
|
client.set_invite_policy(invite_policy)
|
|
|
|
# Test:
|
|
for room in config.alert_rooms:
|
|
await client.send_message(room, f"Hello from {config.username}")
|
|
|
|
await asyncio.gather(
|
|
client.run(),
|
|
run_webhook(alert_queue, config.host, config.port),
|
|
send_messages(alert_queue, client, config.alert_rooms)
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|