Framework Design
Telegrator is a mediator-pattern-based framework for building Telegram bots in C#. It follows a clean, modular architecture that separates concerns and provides powerful abstractions for handling Telegram updates.Key Components
The framework is built around several core components that work together to process Telegram updates:Core Layers
Routing Layer
Routing Layer
The routing layer is responsible for receiving updates from Telegram and directing them to appropriate handlers.Key Classes:
UpdateRouter- Main router implementation that manages update distributionIUpdateRouter- Interface defining router capabilitiesUpdateHandlersPool- Manages concurrent handler execution
- Receiving updates from Telegram Bot API
- Finding matching handlers through providers
- Managing handler execution order and concurrency
- Exception handling and error routing
Handler Layer
Handler Layer
The handler layer contains the business logic for processing different types of updates.Base Classes:
UpdateHandlerBase- Base for all handlersMessageHandler- For message updatesCommandHandler- For bot commandsCallbackQueryHandler- For callback queriesInlineQueryHandler- For inline queries
- Type-safe update handling
- Lifecycle management
- Result-based control flow
- Aspect-oriented programming support
Filter Layer
Filter Layer
The filter layer provides declarative update filtering through attributes.Common Filters:
- Text filters (contains, equals, starts with, ends with)
- Sender filters (user ID, username, bot/premium)
- State filters (numeric, string, enum)
- Command filters (aliases, arguments)
- Declarative configuration
- Composable filter chains
- Reusable filter logic
- Early rejection of non-matching updates
State Management
State Management
State management allows tracking conversation state across multiple updates.State Keepers:
NumericStateKeeper- Integer-based statesStringStateKeeper- String-based statesEnumStateKeeper<TEnum>- Enum-based states
- Per-user or per-chat state tracking
- Automatic state transitions
- Custom key resolvers
- Integration with filter system
Update Processing Flow
When a Telegram update arrives, it goes through the following processing pipeline:1. Update Reception
TheUpdateRouter receives the update from the Telegram Bot API through either polling or webhooks.
2. Handler Discovery
The router queries theHandlersProvider and AwaitingProvider to find handlers registered for the update type.
3. Filter Validation
Each handler’s filters are validated against the update. Only handlers with passing filters proceed to execution.4. Handler Execution
Matching handlers are executed through a three-phase process:- Pre-Processing: Execute aspect-based preprocessors
- Main Execution: Run the handler’s
Executemethod - Post-Processing: Execute aspect-based post-processors
5. Result Handling
Handlers return aResult that controls the router’s behavior:
Result.Ok()- Handler succeeded, stop routingResult.Next()- Continue to next matching handlerResult.Fault()- Handler failed, stop routingResult.Next<T>()- Continue only with handlers of type T
Configuration Options
The framework provides several configuration options throughTelegratorOptions:
Design Principles
Separation of Concerns
Each component has a single, well-defined responsibility:- Routers handle update distribution
- Handlers contain business logic
- Filters perform validation
- State keepers manage state
Type Safety
The framework leverages C#‘s type system to provide compile-time safety:- Generic handler base classes
- Strongly-typed filters
- Type-safe state management
Extensibility
The framework is designed to be extended:- Custom handlers through base classes
- Custom filters through
IFilter<T> - Custom state keepers through
StateKeeperBase<TKey, TState> - Custom aspects through
IPreProcessorandIPostProcessor
The mediator pattern allows for clean separation between update reception and handling, making the codebase maintainable and testable.
Next Steps
Mediator Pattern
Learn about the UpdateRouter and dispatching mechanism
Handlers
Explore different handler types and their usage
Filters
Master the filter system for update validation
State Management
Understand conversation state tracking