Events (astra.events)
Overview
Event modules provide the filtering, dispatch and context objects used by the application layer.
- class EventEmitter[source]
Event registry.
Allows functions to subscribe to specific event names and provides mechanisms for firing events with automatic task scheduling for sync and async callbacks.
- on(event, callback=None, criteria=None)[source]
Subscribes a listener to an event.
Can be used as a decorator or a direct function call.
- Parameters:
Example:
@client.on("message") async def handle_message(msg): print(msg.body)
- class EventDispatcher(client)[source]
Routes incoming events across the application.
Handles priority-based execution, command parsing, and event interception via middlewares.
- async wait_for(event_name, criteria=None, timeout=None)[source]
Suspends execution until a specific event arrives.
- subscribe(event_name, callback, criteria=None, priority=0, is_command=False, command_name=None)[source]
Registers a new event handler.
This module provides the criteria (filters) used to match events in the Astra framework.
- class Criterion[source]
Base class for all event matching criteria.
Criteria can be combined using logical operators: - & (AND) - | (OR) - ~ (NOT)
- class Filters[source]
Collection of event matching criteria.
- static command(name, prefixes='/.')[source]
Matches if the message is a specific command.
Example
command(“ping”, “.”) -> Matches “.ping” command(“.ping”) -> Matches “.ping” (infer prefix from name)
- static text_contains(text, ignore_case=True)[source]
Matches if the message text contains the given substring.
- static text_is(text, ignore_case=True)[source]
Matches if the message text exactly matches the given string.
- class EventContext(client, event, command=None, args=None, prefix=None)[source]
The context of a WhatsApp event.
Provides a high-level API for interacting with the message or chat that triggered the event.
- Parameters:
- __init__(client, event, command=None, args=None, prefix=None)[source]
Initialize the event context.
Example: registering an event
from astra import Client
from astra.events import Filters
client = Client()
@client.on("message", Filters.text_contains("urgent"))
async def on_urgent(ctx):
await ctx.reply("Received — will review asap")
Async behavior
Dispatcher schedules handler invocations as asyncio tasks to avoid blocking
the incoming event loop.
Filters can be coroutine-backed — they are awaited during dispatch.