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
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 theRegExpMatchArray
Update Shortcuts
These properties provide quick access to different parts of the update object.Message | undefined
Alias for
ctx.update.messageMessage | undefined
Alias for
ctx.update.edited_messageMessage | undefined
Alias for
ctx.update.channel_postMessage | undefined
Alias for
ctx.update.edited_channel_postMessage | undefined
Alias for
ctx.update.business_messageMessage | undefined
Alias for
ctx.update.edited_business_messageCallbackQuery | undefined
Alias for
ctx.update.callback_queryInlineQuery | undefined
Alias for
ctx.update.inline_queryMessageReactionUpdated | undefined
Alias for
ctx.update.message_reactionChatMemberUpdated | undefined
Alias for
ctx.update.my_chat_memberChatMemberUpdated | undefined
Alias for
ctx.update.chat_memberAggregation Shortcuts
These properties aggregate data from multiple possible sources in the update.Message | undefined
Get the message object from wherever possible. Checks:
messageeditedMessagechannelPosteditedChannelPostbusinessMessageeditedBusinessMessagecallbackQuery.message
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.
reactions
Analyzes message reaction updates to determine which reactions were added, removed, or kept.ReactionInfo
Object containing information about the reaction update:
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
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.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.replyWithPhoto
Sends a photo to the same chat.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.replyWithVenue
Sends information about a venue.replyWithContact
Sends a phone contact.replyWithPoll
Sends a native poll.replyWithDice
Sends an animated emoji with a random value.forwardMessage
Forwards a message to another chat.copyMessage
Copies a message to another chat (without the forward header).deleteMessage
Deletes the current message.answerCallbackQuery
Answers a callback query from an inline button.editMessageText
Edits the text of a message.editMessageReplyMarkup
Edits the reply markup (keyboard) of a message.Complete Example
See Also
- Bot - The main Bot class
- API Client - Direct API method calls
- Middleware - Understanding middleware
- Context Flavors - Extending the context