Skip to main content
Hooks let you inject custom logic at four points in the message processing pipeline. Use them for logging, access control, content filtering, analytics, or any cross-cutting concern.

Hook Points

The InterfaceHook Protocol

Implement any combination of these four methods. All are optional — only implement the ones you need:

Adding Hooks

Use .add_hook() for fluent chaining:
Or pass them at construction:
Hooks run in the order they are added. For on_message_received, if any hook returns False, processing stops and subsequent hooks are skipped.

Built-in Hooks

LoggingHook

Logs incoming messages and errors for observability:
Logs on_message_received (user, chat, text preview) and on_error (exception details).

AllowlistHook

Restricts access to a set of user IDs:
Messages from users not in the set are silently dropped (returns False from on_message_received).

Writing Custom Hooks

Access Control

Content Filtering

Analytics

Message Enrichment

Error Notification

Hook Execution Order

When multiple hooks are registered:
  • on_message_received: Runs in order. First False vetoes the message and stops the chain.
  • on_before_respond: Runs in order. If a hook returns a modified message, subsequent hooks see the modified version.
  • on_after_respond: Runs in order. If a hook returns a modified response, subsequent hooks see the modified version.
  • on_error: All hooks run, regardless of whether earlier hooks raised exceptions.