Middleware Order
Middleware order is a product decision, not just a wiring detail.
A neutral production sequence is:
- observability
- concurrency control
- database or storage session setup
- idempotency
- tenant or chat resolution
- user loading
- i18n setup
- incoming message persistence
- command or feature permissions
- business rules specific to the bot
- post-processing side effects
Why this order works:
- observability wraps everything
- idempotency and throttling happen before expensive business logic
- i18n is available before handlers start rendering
- persistence runs before downstream consumers depend on canonical message data
- feature-specific checks happen after core context is already loaded
Use separate middleware stacks for:
messageedited_messagecallback_query
Do not assume the same middleware set is correct for all update types.