Repo improvements

- Improved README.md
- Added examples
- Added `session_storage/` to .gitignore so that examples do not introduce leftover files after execution
This commit is contained in:
2026-09-12 20:16:39 +03:00
parent 1fe4434e16
commit fa97e4b098
5 changed files with 216 additions and 49 deletions

View File

@@ -1,67 +1,61 @@
# mab
# 🤖 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.
develop **very** simple Matrix bots. It does not aim to be the best library out
there, but it aims to be convenient and usable for relatively serious projects.
## Features
## ✨ Features
The package supports the following features:
The library supports the following features:
- **Completely `asyncio` based**
- **Filter-based callback system**
- **Images sending**
- **Videos sending with automatic thumbnail generation (requires `ffmpeg`)**
- **Sending images**
- **Sending videos with automatic thumbnail generation (requires `ffmpeg`)**
## Installation
## 🚀 Usage
Use `pip` to install this package:
Use `apt` to install required system packages and `pip` to install the package.
You may need to use `root` privileges to use `apt`. It's highly recommended you
use `venv` or another Python virtual environment. Here are the commands to
install the latest version of the library:
```bash
apt install libmagic1-dev libolm-dev
python -m pip install git+https://git.tyukalov.su/nikita/mab@v0.4.0
```
You should specify package version you want to use, because `main` without tags
contains unstable code.
`libmagic1-dev` is needed for automatic file MIME type detection, `libolm-dev`
is needed for E2EE to work.
## Basic usage
Please inspect [`examples/shell_bot.py`](examples/shell_bot.py),
[`examples/echo_bot.py`](examples/echo_bot.py) or open [`examples/`](examples/)
directory to find usage examples. Examples require that you set
`MATRIX_HOMESERVER` and `MATRIX_USERNAME` environment variables. Examples create
`session_storage` directory in working directory.
This is the most simple bot you can create. It would respond to any message
that contains `hello` and `hi` words.
## 🏷️ Versioning
```python
import asyncio
from pathlib import Path
from mab import MatrixBot, MatrixBotConfig
from mab import TextContainsFilter, SenderIsBotFilter
from mab.types import RoomEventData
Releases are tagged in this repository using the `vX.Y.Z` format. If the commit
is not tagged, it must be treated as versionless and should not be used for your
application.
- `X` **(Major)**: Breaking architectiral changes or complete rewrites. Existing
code will break. Note that `0.Y.Z` versions are considered **very unstable**,
the API may change at any time and some features do not work as expected.
- `Y` **(Minor)**: Breaking API changes, feature removals, or behavioral
modifications. Existing code will likely break.
- `Z` **(Patch)**: Backward-compatible feature additions, bug fixes, or internal
changes. Existing code will not break.
async def on_message(data: RoomEventData) -> None:
text = f"Your message contains {len(data.event.body)} symbols"
await data.bot.send_text(data.room, text)
## 🛠️ Development
async def main() -> None:
# 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.add_callback(
~SenderIsBotFilter() & TextContainsFilter(["hello", "hi"]),
on_message
)
await 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())
Here's the list of commands you should execute to get started with development
(including cloning the repository and installing required packages). Please note
that your workflow may use something other than `venv`.
```bash
apt install libmagic1-dev libolm-dev
git clone https://git.tyukalov.su/nikita/mab
cd mab
python3 -m venv .venv
. .venv/bin/activate
pip install -e .
```