Handler Builder
The Handler Builder API provides a fluent, programmatic way to create handlers without defining class-based handlers. This is useful for simple handlers, dynamic handler creation, or when you prefer a more functional programming style.Why Use Handler Builder?
No Class Boilerplate
Create handlers inline without defining separate classes
Dynamic Creation
Generate handlers at runtime based on configuration
Functional Style
Use lambda expressions and functional programming patterns
Quick Prototyping
Rapidly test handler logic without ceremony
Basic Message Handler
Create a simple message handler using the builder:Adding Filters
Add filters to control when the handler executes:Multiple Filters
Using AddFilters for Multiple Filters
Setting Priority and Concurrency
Control handler execution order and concurrency:Setting Concurrency
Setting Both Priority and Concurrency
Adding State Keepers
Create stateful handlers using the builder:Numeric State
String State
Enum State
Custom State Key Resolver
Callback Query Handlers
Build callback query handlers:Targeted Filters
Filter on specific parts of the update:Update Validation
Add custom update validation:Building Different Update Types
Create handlers for any update type:Chaining Builder Methods
The builder uses a fluent API - chain multiple configuration methods:Complex Example: Order Flow
Here’s a complete order processing flow using handler builder:Best Practices
- Keep handlers simple - Complex logic should be in separate methods/classes
- Use meaningful variable names - Make lambda parameters clear
- Extract reusable filters - Define filters once, reuse across handlers
- Consider class-based handlers - For complex logic, class-based handlers are more maintainable
- Document complex builders - Add comments explaining the flow
- Use builder for prototyping - Great for testing, refactor to classes later
- Leverage fluent API - Chain methods for readable configuration
When to Use Class-Based vs Builder
Use Handler Builder When:
- Creating simple, one-off handlers
- Prototyping and testing
- Handler logic is a few lines
- You prefer functional programming style
Use Class-Based Handlers When:
- Handler has complex logic
- You need dependency injection
- Handler logic is reusable
- You want better testability
- Multiple related handlers share code
Next Steps
Creating Handlers
Learn about class-based handler creation
Working with Filters
Explore the complete filtering system