Pipeline stages
CommandsAuthMiddleware
Intercepts slash commands (e.g.,
/info, /new, /payers). When the ACTIVE_COMMANDS environment variable is set to false, this middleware blocks all command messages and sends a notification to the sender.When commands are enabled, the message passes through to the registered command handlers on the LogicRouter. This allows operators and testers to invoke diagnostic commands during controlled rollouts.| Configuration | Value |
|---|---|
| Environment variable | ACTIVE_COMMANDS |
| Default | true (commands enabled) |
| Behavior when disabled | Blocks all / prefixed messages, sends “Commands are disabled” |
PhoneFakerMiddleware
Development-only middleware that allows testers to simulate different sender identities by overriding the phone number associated with a session. Uses a Redis hash to map session IDs to substitute phone numbers.Testers can use
/phone +1234567890 to set a specific identity, or /new to generate a random phone number — enabling testing of onboarding flows, blocked states, and CIP-pending scenarios without real sender accounts.PayhubAuthMiddleware
Authenticates the sender by phone number against the identity service. This middleware:
- Validates that a phone number is present on the inbound message
- Normalizes the phone number to a standard format
- Queries the identity service for the sender’s profile
- Injects the sender’s identity (name, compliance status, payment methods, country) into the message metadata
unavailable_service flag in metadata but does not block the message. The LogicRouter detects this flag and routes to the service unavailable agent for graceful degradation.This middleware never blocks message delivery. Authentication failures result in routing to the appropriate fallback agent — not in dropped messages.
ContactDecodingMiddleware
Normalizes contact information from the message payload. Standardizes phone numbers and contact data formats before they reach the agent layer.
ChatwootMiddleware
Syncs conversations to Chatwoot for human agent visibility and handoff. When enabled, every message and response is mirrored to a Chatwoot inbox, allowing human agents to:
This middleware is optional and can be disabled entirely without affecting agent behavior.
- Monitor active conversations in real time
- Take over a conversation when escalation is triggered
- Review conversation history for quality assurance
| Configuration | Environment variable |
|---|---|
| Enable/disable | CHATWOOT_ENABLE_MIDDLEWARE |
| Chatwoot URL | CHATWOOT_URL |
| Access key | CHATWOOT_ACCESS_KEY |
| Account ID | CHATWOOT_ACCOUNT_ID |
| Inbox name | CHATWOOT_INBOX_NAME |
Middleware registration order
Middleware stages execute in registration order. The order inmain.py defines the pipeline:
False. For example, CommandsAuthMiddleware returns False when commands are disabled, preventing the command message from reaching subsequent middleware or the agent.
Security considerations
- Phone normalization:
PayhubAuthMiddlewarenormalizes phone numbers before querying the identity service, preventing identity spoofing through formatting variations. - Identity injection: Sender identity is injected into message metadata by the middleware — not provided by the sender. The agent receives pre-authenticated context.
- Graceful failure: Authentication middleware does not drop messages on failure. Service outages result in routing to the service unavailable agent, maintaining a response to the sender.
- Development isolation:
PhoneFakerMiddlewareuses a dedicated Redis hash and is controlled by deployment configuration. It produces no effect in production when disabled.
Configuration and control
| Control | Description |
|---|---|
| Middleware ordering | The bank defines which middleware runs and in what sequence |
| Command availability | ACTIVE_COMMANDS toggles slash command access |
| Chatwoot integration | Independently enabled or disabled via environment variables |
| Phone faker | Disabled in production; enabled only in development environments |
| Custom middleware | The bank can add new middleware stages to the pipeline without modifying existing stages |