58 lines
1.5 KiB
Markdown
58 lines
1.5 KiB
Markdown
# mab
|
|
|
|
**mab** *(MAtrix Bot)* is a **very** simple Python package that can be used to
|
|
develop **very** simple Matrix bots. I have decided to make something like this
|
|
because I wasn't satisfied by simplicity and usage of other libraries. So
|
|
this library does not aim to be "the best matrix bot library", it only aims to
|
|
be good enough for me.
|
|
|
|
## Installation
|
|
|
|
Use `pip` to install this package:
|
|
|
|
```bash
|
|
python -m pip install git+https://git.tyukalov.su/nikita/mab
|
|
```
|
|
|
|
## Basic usage
|
|
|
|
This is the most simple bot you can create
|
|
|
|
```python
|
|
import asyncio
|
|
from mab import MatrixBot, MatrixBotConfig
|
|
from nio import MatrixRoom, MatrixMessageText
|
|
|
|
bot: MatrixBot
|
|
|
|
async def on_room_message_text(room: MatrixRoom, event: RoomMessageText) -> None:
|
|
global bot
|
|
# do not respond to ourselves
|
|
if event.sender == bot.get_client().user_id:
|
|
return
|
|
text = f"You message contains {len(event.body)} symbols"
|
|
await bot.send_text_to_room(room.room_id, text)
|
|
|
|
|
|
async def main() -> None:
|
|
global bot
|
|
# create and start the bot
|
|
cfg = MatrixBotConfig(
|
|
matrix_homeserver_url="matrix.domain.su",
|
|
matrix_username_localpart="nagibator666",
|
|
storage_directory=Path("storage_nagibator666")
|
|
)
|
|
bot = MatrixBot(matrix_bot_config)
|
|
bot.start()
|
|
# wait for Ctrl+C
|
|
try:
|
|
while True:
|
|
await asyncio.sleep(1)
|
|
except:
|
|
pass
|
|
# stop the bot
|
|
await bot.stop()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
``` |