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.
71 lines
1.7 KiB
Python
71 lines
1.7 KiB
Python
"""
|
|
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
|
|
from .format import Message
|
|
|
|
@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 __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
|
|
config: Config
|
|
):
|
|
"""
|
|
Initialize the bot and send messages added to the queue to the alert_rooms.
|
|
"""
|
|
|
|
bot = await Client(
|
|
config.username,
|
|
config.homeserver,
|
|
config.password
|
|
)
|
|
invite_policy = await WhiteList(bot, config.alert_rooms)
|
|
bot.set_invite_policy(invite_policy)
|
|
bot.add_message_callback(__pong)
|
|
await asyncio.gather(
|
|
bot.run(),
|
|
__send_messsages(
|
|
message_queue,
|
|
bot,
|
|
config.alert_rooms
|
|
)
|
|
)
|