Skip to main content
The Context class is passed to all middleware functions in grammY. It wraps the update object from Telegram and provides convenient shortcuts for accessing information and calling API methods.

Constructor

Update
required
The update object from Telegram
Api
required
An API instance for calling Bot API methods
UserFromGetMe
required
Information about the bot itself
Note: You typically don’t create Context objects manually. grammY creates them for you when processing updates.

Core Properties

Update
The complete update object from Telegram containing all information about the incoming update.
Api
Full access to the Telegram Bot API. Allows you to call any API method.
Tip: Use context shortcuts like ctx.reply() instead of calling API methods directly when possible.
UserFromGetMe
Information about the bot itself as returned by getMe().
string | RegExpMatchArray | undefined
Used by some middleware to store information about how a string or regular expression was matched.
  • For bot.command(): Contains the text after the command
  • For bot.hears() with regex: Contains the RegExpMatchArray

Update Shortcuts

These properties provide quick access to different parts of the update object.
Message | undefined
Alias for ctx.update.message
Message | undefined
Alias for ctx.update.edited_message
Message | undefined
Alias for ctx.update.channel_post
Message | undefined
Alias for ctx.update.edited_channel_post
Message | undefined
Alias for ctx.update.business_message
Message | undefined
Alias for ctx.update.edited_business_message
CallbackQuery | undefined
Alias for ctx.update.callback_query
InlineQuery | undefined
Alias for ctx.update.inline_query
MessageReactionUpdated | undefined
Alias for ctx.update.message_reaction
ChatMemberUpdated | undefined
Alias for ctx.update.my_chat_member
ChatMemberUpdated | undefined
Alias for ctx.update.chat_member
And many more for other update types. See the Telegram Bot API documentation for all available update types.

Aggregation Shortcuts

These properties aggregate data from multiple possible sources in the update.
Message | undefined
Get the message object from wherever possible. Checks:
  • message
  • editedMessage
  • channelPost
  • editedChannelPost
  • businessMessage
  • editedBusinessMessage
  • callbackQuery.message
Returns the first non-undefined value.
Chat | undefined
Get the chat object from wherever possible.
User | undefined
Get the user object (message sender) from wherever possible.
Chat | undefined
Get the sender chat object. Alias for ctx.msg?.sender_chat.
number | undefined
Get the message identifier from wherever possible.
number | undefined
Get the chat identifier from wherever possible.
string | undefined
Get the inline message identifier from callback queries or chosen inline results.
string | undefined
Get the business connection identifier from wherever possible.

Utility Methods

entities

Extracts entities from the message text or caption.
string | string[]
Optional filter for specific entity types (e.g., 'url', 'mention', 'hashtag')
Array<MessageEntity & { text: string }>
Array of entities with their extracted text. Returns empty array if no text or entities found.
Example:

reactions

Analyzes message reaction updates to determine which reactions were added, removed, or kept.
ReactionInfo
Object containing information about the reaction update:
Example:

Context Probing Methods

These methods check if the context matches certain conditions.

has

Checks if the context matches a filter query.
FilterQuery | FilterQuery[]
required
The filter query to check
Example:

hasText

Checks if the context contains specific text or matches a regex.

hasCommand

Checks if the context contains a specific command.

hasReaction

Checks if the context contains a specific reaction.

hasChatType

Checks if the context belongs to a specific chat type.
Example:

Static Methods

Context.has

Provides static methods to generate predicate functions for context probing.

API Shortcut Methods

The Context class provides convenient shortcuts for common API operations.

reply

Sends a text message to the same chat.
string
required
Text of the message to send (1-4096 characters)
object
Optional parameters like parse_mode, reply_markup, etc.
Example:

replyWithPhoto

Sends a photo to the same chat.
Example:

replyWithAudio

Sends an audio file.

replyWithDocument

Sends a document file.

replyWithVideo

Sends a video file.

replyWithAnimation

Sends an animation (GIF or video without sound).

replyWithVoice

Sends a voice message.

replyWithVideoNote

Sends a video note (round video message).

replyWithMediaGroup

Sends a group of photos, videos, documents or audios as an album.

replyWithLocation

Sends a location point.
Example:

replyWithVenue

Sends information about a venue.

replyWithContact

Sends a phone contact.

replyWithPoll

Sends a native poll.
Example:

replyWithDice

Sends an animated emoji with a random value.
Example:

forwardMessage

Forwards a message to another chat.

copyMessage

Copies a message to another chat (without the forward header).

deleteMessage

Deletes the current message.
Example:

answerCallbackQuery

Answers a callback query from an inline button.
Example:

editMessageText

Edits the text of a message.
Example:

editMessageReplyMarkup

Edits the reply markup (keyboard) of a message.

Complete Example

See Also