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.
64 lines
1.6 KiB
Python
64 lines
1.6 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
|
|
|
|
@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
|
|
message = f"{ message.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()
|