Updated README

This commit is contained in:
2026-09-02 18:24:24 +03:00
parent d8bf1818d7
commit b62473c468

View File

@@ -16,24 +16,26 @@ python -m pip install git+https://git.tyukalov.su/nikita/mab
## Basic usage ## Basic usage
This is the most simple bot you can create This is the most simple bot you can create. It would respond to any message
that starts with `!test`, `!hello` or `!hi`.
```python ```python
import asyncio import asyncio
from mab import MatrixBot, MatrixBotConfig from mab import MatrixBot, MatrixBotConfig
from mab import TextCommandFilter
from nio import MatrixRoom, MatrixMessageText from nio import MatrixRoom, MatrixMessageText
bot: MatrixBot bot: MatrixBot
async def on_room_message_text(room: MatrixRoom, event: RoomMessageText) -> None: async def on_valid_command(room: MatrixRoom, event: RoomMessageText) -> None:
global bot global bot
# do not respond to ourselves # do not respond to ourselves
if event.sender == bot.get_client().user_id: if event.sender == bot.get_client().user_id:
return return
text = f"You message contains {len(event.body)} symbols" text = f"Your message contains {len(event.body)} symbols"
await bot.send_text_to_room(room.room_id, text) await bot.send_text_to_room(room.room_id, text)
async def main() -> None: async def main() -> None:
global bot global bot
# create and start the bot # create and start the bot
@@ -43,6 +45,10 @@ async def main() -> None:
storage_directory=Path("storage_nagibator666") storage_directory=Path("storage_nagibator666")
) )
bot = MatrixBot(matrix_bot_config) bot = MatrixBot(matrix_bot_config)
bot.add_event_callback(
on_valid_command,
TextCommandFilter(["test", "hello", "hi"])
)
bot.start() bot.start()
# wait for Ctrl+C # wait for Ctrl+C
try: try: