""" The bot that send messages. """ import asyncio from typing import ( Any, NoReturn ) import nio from matrix_bot.client import Client from matrix_bot.invite_policy import WhiteList from matrix_bot.utils import ignore_client_message from .config import Config @ignore_client_message async def __pong( client: Client, room: nio.rooms.MatrixRoom, message: nio.events.room_events.RoomMessageText ): """ If the bot is pinged, send "Pong" """ if (client.user_name + ':') in message.body: await client.send_message(room.room_id, "Pong") async def run( config: Config, message_queue: asyncio.Queue[str] ): """ Initialize the bot and send messages added to the queue to the other rooms. """ @ignore_client_message async def __foward_message( client: Client, from_room: nio.rooms.MatrixRoom, message: nio.events.room_events.RoomMessageText ): """ Format and send the message """ if (client.user_name + ':') in message.body: return sender = from_room.user_name(message.sender) # Don't look to work if sender is None: sender = ':'.join(message.sender.split(':')[:-1])[1:] message = f"{ sender }: { message.body }" for to_room in config.rooms: to_room = await client.resolve_room(to_room) if to_room.id != from_room.room_id: await client.send_message(to_room.id, message) bot = await Client( config.username, config.homeserver, config.password ) invite_policy = await WhiteList(bot, config.rooms) bot.set_invite_policy(invite_policy) bot.add_message_callback(__pong) bot.add_message_callback(__foward_message) await bot.run()