""" 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" """ print("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 """ print("foward") if (client.user_name + ':') in message.body: return message = f"{ message.sender_key }: { message.body }" for to_room in config.rooms: await client.send_message(to_room, 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()