send the content of alert received by the web hook to rooms
parent
80891eeda2
commit
d8aff91f6f
@ -0,0 +1,53 @@
|
|||||||
|
"""
|
||||||
|
The bot that send messages.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
NoReturn
|
||||||
|
)
|
||||||
|
from matrix_bot.client import Client
|
||||||
|
from matrix_bot.invite_policy import WhiteList
|
||||||
|
|
||||||
|
async def __send_messsages(
|
||||||
|
message_queue: asyncio.Queue[dict[str, Any]], # For now, type will change in the futur
|
||||||
|
bot: Client,
|
||||||
|
rooms: list[str]
|
||||||
|
)->NoReturn:
|
||||||
|
"""
|
||||||
|
Actually send the messages from the queue.
|
||||||
|
"""
|
||||||
|
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 send_messages(
|
||||||
|
message_queue: asyncio.Queue[dict[str, Any]], # For now, type will change in the futur
|
||||||
|
username: str,
|
||||||
|
homeserver: str,
|
||||||
|
password: str,
|
||||||
|
alert_rooms: list[str]
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Initialize the bot and send messages added to the queue to the alert_rooms.
|
||||||
|
"""
|
||||||
|
|
||||||
|
bot = await Client(
|
||||||
|
username,
|
||||||
|
homeserver,
|
||||||
|
password
|
||||||
|
)
|
||||||
|
invite_policy = await WhiteList(bot, alert_rooms)
|
||||||
|
bot.set_invite_policy(invite_policy)
|
||||||
|
await asyncio.gather(
|
||||||
|
bot.run(),
|
||||||
|
__send_messsages(
|
||||||
|
message_queue,
|
||||||
|
bot,
|
||||||
|
alert_rooms
|
||||||
|
)
|
||||||
|
)
|
Loading…
Reference in New Issue