- Changed event callback signature to `(ctx: EventContext, vars: ContextVars)` - Changed event filter signature to `(ctx: EventContext, vars: ContextVars)` - Partially fixed examples - Refactored `src/mab/__init__.py` to be more structured - Added support for local event context variables so that different filters don't mess with each other - Refactored file structure for context variables - Added support for combination of ContextVars - `ContextVarsPopulator` does not look builders dynamically anymore; `ContextVarsPopulator.autobuilder` is used to mark function as a builder that must be automatically called - `ContextVarsPopulator` does not required to be created anymore - Added context variable `CTX_COMMAND` - `MessageTypeFilter` supports message types passed as strings from now on - `MessageHasFile` renamed to `MessageHasFileFilter` - Added docs for context variables
2.9 KiB
For mab user
What is Context Variable?
Context Variables system is used to get rid of matrix-nio when possible.
To keep it short let's just say that mab looks at the event returned by
matrix-nio and tries to convert it to simplified pydantic model.
Simplified model is more than enough in most cases but you can use raw events
if needed.
How do Context Variables work?
From user's perspective the system is quite easy. Your event callback takes
two positional arguments: ctx (of type EventContext) and vars (of type
ContextVars). One should use ctx argument to access raw matrix-nio data
and MatrixBot that has invoked the callback. vars should be used when you'd
like to access simplified data model (e.g. get message body or attached file
size). Example callback syntax that uses both:
from mab import *
...
async def callback(ctx: EventContext, vars: ContextVars) -> None:
response = f"Your message text is '{vars[CTX_MESSAGE].body}'"
await ctx.bot.send.text(ctx.room, response)
...
CTX_MESSAGE is so called variable key that you should use to access the
context variable. If the key is not set then RuntimeError will be raised.
For mab developer
The following rules must be considered when adding new context variables or models:
- If a model can be added to existing subpackage of
mab.context.varsthen it should be added there - If a model does not fit into any existing subpackage then a new subpackage must be introduced
src/mab/context/vars/SUBPACKAGE/types.pymust ALWAYS exist and define types related to the subpackage (e.g.messagesubpackage introducesMessagemodel)src/mab/context/vars/SUBPACKAGE/keys.pymust exist if the subpackage introduces keys used withContextVars(e.g.messagesubpackage introducesCTX_MESSAGEkey)src/mab/context/vars/SUBPACKAGE/builders.pymust exist if the subpackage introduces so called autobuilders - functions decorated with the following syntax:Functions decorated like this will be called when a new event is returned by@mab.context.vars.ContextVarsPopulator.autobuilder async def _example_autobuilder(ctx: EventContext, vars: ContextVars) -> None: vars[CTX_EXAMPLE] = Example(example=1)matrix-nio.__init__.pyof a subpackage must ALWAYS exist and have the following structure:The goal offrom .types import * from .keys import * from . import builders # if exists__init__.pyis to make sure thatbuilders.pyis imported and to introduce members oftypes.pyandkeys.pyinto its namespace.- All subpackages must be imported in
src/mab/__init__.pyusing the following syntax:from .context.vars.command import * from .context.vars.message import *
Please refer to src/mab/context/vars/ for examples