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.

54 lines
1.2 KiB
Python

"""
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
)
)