Skip to content

Context (ctx)

APIper-update ctx

Context shortcuts

Since1.0.0
ReturnsContext
Context is created for every update and wraps the scoped Telegram client, parsed update fields, session state, scene/wizard helpers, and reply shortcuts.

Context surfaces

Use this page as a map of the most common per-update methods.

Messaging

Reply with text, HTML, Markdown, drafts, and media.

Interaction

Answer callback queries, inline queries, polls, invoices, and chat actions.

Administration

Manage members, permissions, invite links, gifts, and Stars.

The Context object is created for every incoming update and provides shortcuts for all Telegram Bot API operations.

Properties

PropertyTypeDescription
ctx.updateUpdateRaw update object from Telegram
ctx.messageMessageThe incoming message (if any)
ctx.fromUserThe user who sent the update
ctx.chatChatThe chat where the update occurred
ctx.commandobjectParsed command data ({ command, args })
ctx.sessionanySession data (requires session middleware)
ctx.sceneobjectScene navigation controls
ctx.wizardobjectWizard step controls
ctx.i18nobjectInternationalization helper
ctx.telegramTelegramClientScoped direct Telegram API client

Messaging Methods

MethodAPIDescription
ctx.reply(text, extra?)sendMessageSend a text message
ctx.replyWithHTML(text, extra?)sendMessageReply with HTML formatting
ctx.replyWithMarkdown(text, extra?)sendMessageReply with Markdown formatting
ctx.replyWithMarkdownV2(text, extra?)sendMessageReply with MarkdownV2 formatting
ctx.replyWithDraft(text, extra?)sendMessageDraftPre-fill user input field (API 9.5)

Media Methods

MethodAPIDescription
ctx.replyWithPhoto(photo, extra?)sendPhotoSend a photo
ctx.replyWithVideo(video, extra?)sendVideoSend a video
ctx.replyWithAudio(audio, extra?)sendAudioSend an audio file
ctx.replyWithDocument(doc, extra?)sendDocumentSend a document
ctx.replyWithVoice(voice, extra?)sendVoiceSend a voice note
ctx.replyWithVideoNote(note, extra?)sendVideoNoteSend a circular video note
ctx.replyWithAnimation(anim, extra?)sendAnimationSend a GIF/animation
ctx.replyWithSticker(sticker, extra?)sendStickerSend a sticker
ctx.replyWithMediaGroup(media, extra?)sendMediaGroupSend an album
ctx.replyWithPaidMedia(stars, media, extra?)sendPaidMediaSend paid media (Stars)
ctx.replyWithGame(gameShortName, extra?)sendGameSend a game

Interactive Methods

MethodAPIDescription
ctx.replyWithPoll(question, options, extra?)sendPollSend a poll
ctx.replyWithDice(extra?)sendDiceSend an animated dice
ctx.replyWithLocation(lat, lon, extra?)sendLocationSend a location
ctx.replyWithVenue(lat, lon, title, addr, extra?)sendVenueSend a venue
ctx.replyWithContact(phone, name, extra?)sendContactSend a contact
ctx.replyWithInvoice(...)sendInvoiceSend a payment invoice
ctx.sendGift(userId, giftId, extra?)sendGiftSend a gift to a user
ctx.sendGiftToChat(chatId, giftId, extra?)sendGiftSend a gift to a chat

Message Manipulation

MethodAPIDescription
ctx.editMessageText(text, extra?)editMessageTextEdit message text
ctx.editMessageCaption(caption, extra?)editMessageCaptionEdit message caption
ctx.editMessageMedia(media, extra?)editMessageMediaEdit message media
ctx.editMessageLiveLocation(lat, lon, extra?)editMessageLiveLocationEdit a live location
ctx.stopMessageLiveLocation(extra?)stopMessageLiveLocationStop a live location
ctx.editMessageReplyMarkup(markup)editMessageReplyMarkupEdit inline keyboard
ctx.deleteMessage(messageId?)deleteMessageDelete a message
ctx.copyMessage(toChatId, extra?)copyMessageCopy a message
ctx.forwardMessage(toChatId, extra?)forwardMessageForward a message
ctx.pinChatMessage(messageId?, notify?)pinChatMessagePin a message
ctx.unpinChatMessage(messageId?)unpinChatMessageUnpin a message

Callback & Query Responses

MethodAPIDescription
ctx.answerCbQuery(text?, showAlert?)answerCallbackQueryRespond to button press
ctx.answerInlineQuery(results, extra?)answerInlineQueryRespond to inline query
ctx.answerPreCheckoutQuery(ok, errorMsg?)answerPreCheckoutQueryRespond to checkout

Admin Methods

MethodAPIDescription
ctx.banChatMember(userId, extra?)banChatMemberBan a user
ctx.unbanChatMember(userId, extra?)unbanChatMemberUnban a user
ctx.restrictChatMember(userId, perms, extra?)restrictChatMemberRestrict a user
ctx.promoteChatMember(userId, perms?)promoteChatMemberPromote to admin
ctx.setChatPermissions(perms, extra?)setChatPermissionsSet default permissions
ctx.getChatMember(userId)getChatMemberGet member info
ctx.getChatMembersCount()getChatMemberCountGet member count
ctx.approveChatJoinRequest(userId)approveChatJoinRequestApprove join request
ctx.declineChatJoinRequest(userId)declineChatJoinRequestDecline join request
ctx.leaveChat()leaveChatLeave the current chat

Utility Methods

MethodAPIDescription
ctx.sendChatAction(action)sendChatActionShow typing indicator, etc.
ctx.setReaction(emoji)setMessageReactionReact to a message
ctx.getChat()getChatGet full chat info
ctx.createChatInviteLink(extra?)createChatInviteLinkCreate invite link
ctx.exportChatInviteLink()exportChatInviteLinkExport primary invite link
ctx.getFileLink(fileId)getFileGet file download URL
ctx.downloadFile(fileId, destPath?)Download file to disk or buffer
ctx.getAvailableGifts()getAvailableGiftsList gifts available to the bot
ctx.getUserGifts(userId, extra?)getUserGiftsList gifts owned by a user
ctx.getBusinessAccountGifts(id, extra?)getBusinessAccountGiftsList gifts owned by a business account
ctx.getMyStarBalance()getMyStarBalanceGet the bot's Star balance
ctx.getBusinessAccountStarBalance(id)getBusinessAccountStarBalanceGet a business account's Star balance
ctx.getStarTransactions(extra?)getStarTransactionsGet Star transaction history
ctx.transferBusinessAccountStars(id, count)transferBusinessAccountStarsTransfer Stars from business account
ctx.approveSuggestedPost(messageId, extra?)approveSuggestedPostApprove a suggested post
ctx.declineSuggestedPost(messageId, extra?)declineSuggestedPostDecline a suggested post

Examples

typescript
// Send a photo with caption and inline keyboard
await ctx.replyWithPhoto('https://example.com/image.jpg', {
    caption: '<b>Product Name</b>\nPrice: $9.99',
    parse_mode: 'HTML',
    reply_markup: Markup.inlineKeyboard([[Markup.button.callback('Buy Now', 'buy_1')]]),
});

// Create a poll
await ctx.replyWithPoll('Favorite color?', [{ text: 'Red' }, { text: 'Blue' }, { text: 'Green' }]);

// Send a checklist using the current Bot API payload shape
await ctx.replyWithChecklist({
    title: 'Launch tasks',
    tasks: [{ text: 'Update changelog' }, { text: 'Run release checklist' }],
});

// Edit a message after delay
const msg = await ctx.reply('Loading...');
setTimeout(() => ctx.editMessageText('Done!'), 2000);

// Call a Telegram API method that has no shortcut yet
await ctx.telegram.callApi('sendChatAction', {
    chat_id: ctx.chat!.id,
    action: 'typing',
});

Released under the ISC License.