Introduction
Filters are the backbone of Telegrator’s message routing system. They allow you to define precise conditions that determine when your handlers should execute. By applying filters as attributes to your handler methods, you create a declarative and maintainable routing system for your bot.How Filters Work
Filters in Telegrator operate on a simple principle: they evaluate incoming updates and return a boolean value indicating whether the update should be processed by the associated handler.Filter Execution Flow
- Update Received: A Telegram update arrives at your bot
- Filter Evaluation: Each filter attribute on a handler is evaluated in order
- Logical AND: All filters must pass for the handler to execute
- Handler Execution: If all filters pass, the handler method is invoked
Filter Categories
Telegrator organizes filters into several categories:Command Filters
Filter command arguments and validate command structure.- Argument count validation
- Argument content matching
- Pattern matching on arguments
Text Filters
Match and validate message text content.- Text pattern matching
- Content validation
- Word detection
Chat Filters
Filter based on chat properties.- Chat type (private, group, channel)
- Chat identification
- Forum chat detection
Sender Filters
Filter based on message sender properties.- User identification
- Bot detection
- Premium user detection
Message Filters
Filter based on message properties and content.- Message entities
- Dice throws
- Service messages
- Reply chain validation
Callback Filters
Filter callback query updates.- Callback data matching
- Inline message identification
Environment Filters
Filter based on runtime environment.- Debug/Release mode
- Environment variables
Combining Filters
Filters can be combined using logical operations:Multiple Attributes (AND Logic)
Programmatic Filter Composition
For more complex scenarios, you can compose filters programmatically:Filter Base Classes
Telegrator provides several base classes for different filter types:MessageFilterAttribute: Base for all message-related filtersCallbackQueryAttribute: Base for callback query filtersEnvironmentFilterAttribute: Base for environment-based filters
Best Practices
Order Your Filters Strategically
Place the most restrictive or fastest-to-evaluate filters first:Use Specific Filters
Choose the most specific filter for your needs:Keep Handlers Focused
Use filters to create focused, single-purpose handlers rather than complex branching logic:Creating Custom Filters
You can create custom filters by extending theFilter<T> base class:
Performance Considerations
- Filters are evaluated synchronously in order
- Failed filters short-circuit evaluation (remaining filters are not checked)
- Regular expression filters are more expensive than simple string operations
- Cache compiled regexes when possible
Next Steps
Explore specific filter categories:- Command Filters - Validate command arguments
- Text Filters - Match message text patterns
- Chat Filters - Filter by chat properties
- Sender Filters - Filter by sender attributes
- Message Filters - Filter by message properties
- Callback Filters - Handle callback queries
- Environment Filters - Filter by runtime environment