bot.on('message:text') instead of checking if (ctx.message?.text).
What are Filter Queries?
A filter query is a string that describes which updates to match. Filter queries work withbot.on() and automatically narrow the TypeScript types.
Filter Query Syntax
Filter queries have three levels:- L1: Update type (
message,callback_query, etc.) - L2: Property on the update type (
text,photo, etc.) - L3: Nested property (
entities, specific entity types, etc.)
Level 1 - Update Types
message- New messagesedited_message- Edited messageschannel_post- New channel postsedited_channel_post- Edited channel postsbusiness_connection- Business connection updatesbusiness_message- Business account messagesedited_business_message- Edited business messagesdeleted_business_messages- Deleted business messagesinline_query- Inline querieschosen_inline_result- Chosen inline resultscallback_query- Callback queriesshipping_query- Shipping queriespre_checkout_query- Pre-checkout queriespoll- Poll updatespoll_answer- Poll answersmy_chat_member- Bot’s chat member status changedchat_member- Chat member status changedchat_join_request- Join requestsmessage_reaction- Message reactionsmessage_reaction_count- Reaction countschat_boost- Chat boostsremoved_chat_boost- Removed boostspurchased_paid_media- Paid media purchases
Level 2 - Message Properties
:text- Text messages:photo- Photos:video- Videos:audio- Audio files:document- Documents:animation- Animations/GIFs:voice- Voice messages:video_note- Video notes:sticker- Stickers:location- Locations:contact- Contacts:poll- Polls:dice- Dice:game- Games:invoice- Invoices
Level 3 - Nested Properties
:entities or :caption_entities):
:mention- @username mentions:hashtag- #hashtag:cashtag- $USD:bot_command- /command:url- https://example.com:email- email@example.com:phone_number- Phone numbers:bold- Bold text:italic- Italic text:underline- Underlined text:strikethrough- Strikethrough text:spoiler- Spoiler text:code- Inline code:pre- Code blocks:text_link- Text links:text_mention- Text mentions:custom_emoji- Custom emoji
Shortcuts
Filter queries support powerful shortcuts:Empty L1 - Messages and Channel Posts
: matches both message and channel_post:
msg - All Message Types
msg: matches all message-like updates:
edit - Edited Messages
edit: matches edited messages and channel posts:
Double Colon - Entity Shortcuts
:: expands to both :entities: and :caption_entities::
media - Photo or Video
file - Any File Type
Combining Filters
OR Logic - Array of Filters
Pass an array to match any of the filters:AND Logic - Chaining
Chain.on() calls for AND logic:
Advanced Examples
Forwarded Messages
Service Messages
Bot-Specific Filters
The specialme filter matches your bot:
Business Updates
Reactions
Stickers
Using Filter Functions
Create reusable filter predicates:Type Safety
Filter queries provide automatic type narrowing:Performance
Filter queries are compiled to efficient predicate functions:Common Patterns
Ignore Bot Messages
Admin-Only Commands
Private Chat Only
Group Features
Debugging Filter Queries
If a filter query isn’t working as expected:Best Practices
Related
- Context - Context object structure
- Middleware - Using middleware
- Updates - Understanding updates