nikita f58c8601d1 Filter update, fixed bot stop, v0.4.0
- Fixed `asyncio.shield` not being awaited in `ClientManager.stop()` (that led to `next_batch` value not being saved and session not being closed properly)
- `BaseEventFilter` does not have any abstract methods anymore and can be used to use callback for any event
- `BaseEventFilter.__repr__` prints class name now
- `BaseEventFilter.__call__` returns True now
- Added `EventTypeFilter` which can be used to check if the event is an instance of some class
- Removed most room events
- Added `NewMessageFilter`, `EditedMessageFilter`, `RedactedMessageFilter`, `SenderIsFilter` and `SenderIsBotFilter`
- Removed `FormattedTextFilter`
- Text filters are derived from `NewMessageFilter` so they won't match edited messages anymore
2026-09-09 19:43:29 +03:00
2026-09-07 00:56:05 +03:00
2026-08-24 20:39:02 +03:00

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.

Features

The package supports the following features:

  • Filter-based callback system
  • Images sending
  • Videos sending with automatic thumbnail generation (requires ffmpeg)

Installation

Use pip to install this package:

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 contain unstable code.

Basic usage

This is the most simple bot you can create. It would respond to any message that contains hello and hi words.

import asyncio
from pathlib import Path
from mab import MatrixBot, MatrixBotConfig
from mab import TextContainsFilter, SenderIsBotFilter
from mab.types import RoomEventData

async def on_message(data: RoomEventData) -> None:
    text = f"Your message contains {len(data.event.body)} symbols"
    await data.bot.send_text_to_room(data.room, text)

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(["test", "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())
Description
Простой асинхронный Python бот для matrix на базе библиотеки matrix-nio
Readme MIT 245 KiB
Languages
Python 100%