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.

58 lines
1.3 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
from .format import Message
async def __send_messsages(
message_queue: asyncio.Queue[Message],
bot: Client,
rooms: list[str]
)->NoReturn:
"""
Actually send the messages from the queue.
"""
while True:
message = await message_queue.get()
for room in rooms:
await bot.send_formated_message(
room,
message.formated_body,
unformated_message=message.body
)
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
)
)