Skip to main content
Filter types enable powerful type narrowing in grammY. When you use filter queries with bot.on(), TypeScript automatically narrows the context type to guarantee certain properties exist.

Filter<C, Q>

The Filter type is the heart of grammY’s filtering system. It takes a context type C and a filter query Q, and produces a narrowed context type.
How it works: When you filter updates, the resulting context is guaranteed to have specific properties. TypeScript enforces this at compile time.

FilterQuery

FilterQuery represents all possible filter query strings you can use with bot.on().
Filter queries have three levels:

Level 1: Update Type

Filter by the update type:
Available L1 filters:
  • 'message'
  • 'edited_message'
  • 'channel_post'
  • 'edited_channel_post'
  • 'business_connection'
  • 'business_message'
  • 'edited_business_message'
  • 'deleted_business_messages'
  • 'inline_query'
  • 'chosen_inline_result'
  • 'callback_query'
  • 'shipping_query'
  • 'pre_checkout_query'
  • 'poll'
  • 'poll_answer'
  • 'my_chat_member'
  • 'chat_member'
  • 'chat_join_request'
  • 'message_reaction'
  • 'message_reaction_count'
  • 'chat_boost'
  • 'removed_chat_boost'

Level 2: Message Properties

Filter by message content or properties:
Common L2 filters:
  • 'message:text'
  • 'message:photo'
  • 'message:video'
  • 'message:audio'
  • 'message:document'
  • 'message:sticker'
  • 'message:animation'
  • 'message:voice'
  • 'message:video_note'
  • 'message:contact'
  • 'message:location'
  • 'message:venue'
  • 'message:poll'
  • 'message:dice'
  • 'message:new_chat_members'
  • 'message:left_chat_member'
  • 'message:entities'
  • 'message:caption'
  • 'message:forward_origin'

Level 3: Deep Properties

Filter by nested properties:
Entity type filters:
  • 'mention' - @username
  • 'hashtag' - #hashtag
  • 'cashtag' - $USD
  • 'bot_command' - /start
  • 'url' - https://example.com
  • 'email' - user@example.com
  • 'phone_number' - +1234567890
  • 'bold' - bold text
  • 'italic' - italic
  • 'code' - code
  • 'pre' - code block
  • 'text_link' - link
  • 'text_mention' - mention without @
  • 'custom_emoji' - custom emoji

Filter Shortcuts

grammY provides shortcuts for common filtering patterns:

Empty L1 Shortcut (: prefix)

Filter messages AND channel posts:
The : shortcut expands to ['message', 'channel_post'].

Media Shortcut

File Shortcut

Entity Shortcut (Empty L2)

Combined Shortcuts

Multiple Filter Queries

You can pass an array of filter queries for OR logic:

Chaining for AND Logic

Chain .on() calls for AND logic:

matchFilter Function

You can create filter predicates programmatically:

FilterCore Type

FilterCore is similar to Filter but returns only the filtered update structure without wrapping it in a context:
This is mainly used internally by grammY.

Type Narrowing Examples

Example 1: Text Messages

Example 2: Photos with Captions

Example 3: Callback Queries

Example 4: New Chat Members

Special Filter: me

Use :me to filter for your bot:

Advanced: Custom Type Predicates

For complete control, define custom type predicates:
Filter types are grammY’s secret weapon for writing type-safe, expressive bot code. Use them to eliminate undefined checks and catch errors at compile time!