Added branches system, updated database, v0.8.0
- Updated to v0.8.0 - Added note about rate limitting to README - Updated examples to work with new callback system - Updated README to describe new callback system correctly - Added branch_bot.py example - Updated database so internal functions are prefixed with underscore now - Replaced MatrixBot.add_room_event_callback with MatrixBot.add_callback - Database schema version is 4 now - Added room state to database to simplify user code - Added RoomStateFilter
This commit is contained in:
43
README.md
43
README.md
@@ -4,6 +4,11 @@
|
||||
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.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> You may need to tweak rate limits for your bot account in the server admin
|
||||
> settings in order the server not to throttle your bot. Check
|
||||
> [Ketasa](https://cloud.ketesa.app) if you use Synapse.
|
||||
|
||||
## ✨ Features
|
||||
|
||||
The library supports the following features:
|
||||
@@ -23,7 +28,7 @@ install the latest stable version of the library:
|
||||
|
||||
```bash
|
||||
apt install libmagic1-dev libolm-dev
|
||||
python -m pip install git+https://git.tyukalov.su/nikita/mab@v0.7.1
|
||||
python -m pip install git+https://git.tyukalov.su/nikita/mab@v0.8.0
|
||||
```
|
||||
|
||||
`libmagic1-dev` is needed for automatic file MIME type detection, `libolm-dev`
|
||||
@@ -58,7 +63,6 @@ of creating a bot:
|
||||
import asyncio
|
||||
from mab import *
|
||||
|
||||
# Run until Ctrl+C
|
||||
async def main():
|
||||
# Create the config for the bot object
|
||||
bot_config = MatrixBotConfig(
|
||||
@@ -76,16 +80,10 @@ async def main():
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
To reduce amount of `if`s in your code, **filters** concept is introduced. When
|
||||
To reduce amount of `if`s in your code, **filter** concept is introduced. When
|
||||
you add a callback to the instance of the bot, you also provide a filter object.
|
||||
Filters are created like that:
|
||||
```python
|
||||
# Filter that is True for new message
|
||||
new_message = NewMessageFilter()
|
||||
|
||||
# Filter that is True for message sent not by the user we are logged in as
|
||||
sent_by_user = ~SenderIsBotFilter()
|
||||
|
||||
# Filter that is True for users in `admins_list`
|
||||
admins_list = [
|
||||
"@server-admin:matrix.example.org",
|
||||
@@ -113,11 +111,34 @@ admin_list = [
|
||||
]
|
||||
# Define the filter
|
||||
the_filter = (
|
||||
MessageReactionFilter()
|
||||
& ~SenderIsBotFilter()
|
||||
MessageHasFileFilter()
|
||||
& SenderIsFilter(admin_list)
|
||||
)
|
||||
```
|
||||
|
||||
Sometimes it is useful to add some sort of hierarchy for your callbacks to
|
||||
simplify the code. That's the moment when **Branch** concept becomes useful.
|
||||
Let's assume you want to add specific event handlers that are executed only for
|
||||
users from `admin_list`. You would write something like that:
|
||||
```python
|
||||
is_admin = SenderIsFilter(admin_list)
|
||||
delete_command_filter = is_admin & BodyCommandFilter("delete")
|
||||
create_command_filter = is_admin & BodyCommandFilter("create")
|
||||
bot.callbacks.add_callback(delete_command_filter, callback_1)
|
||||
bot.callbacks.add_callback(create_command_filter, callback_2)
|
||||
```
|
||||
|
||||
You can logically organize these events in one branch to manage them more
|
||||
efficiently:
|
||||
```python
|
||||
admin_branch = Branch(SenderIsFilter(admin_list))
|
||||
admin_branch.add_callback(BodyCommandFilter("delete"), callback_1)
|
||||
admin_branch.add_callback(BodyCommandFilter("create"), callback_2)
|
||||
bot.callbacks.add_branch(admin_branch)
|
||||
```
|
||||
Please refer to [branch_bot.py](examples/branch_bot.py) for detailed usage
|
||||
example.
|
||||
|
||||
The concept of **Context Variables** is introduced to avoid the usage of
|
||||
*somewhat* complex `matrix-nio` objects. Context variables are key-value pairs
|
||||
passed to your event callbacks. Think of them as *environment variables* passed
|
||||
|
||||
Reference in New Issue
Block a user