Contexts
Context classes represent different types of VK events and updates. All contexts extend the baseContext class and provide type-safe access to event data.
Base Context Class
The foundational class for all event contexts.Properties
The context type (e.g., ‘message’, ‘message_event’, ‘wall_post’)
Array of sub-types (e.g., [‘message_new’], [‘message_edit’])
Custom state object for storing data across middleware
Group ID for group events
Methods
is(types)
Checks if the context matches any of the specified types or sub-types.Array of types to check against
boolean
toJSON()
Returns JSON representation of the context. Returns:object
MessageContext
Represents an incoming or outgoing message.Type
- type:
'message' - subTypes:
'message_new'|'message_edit'|'message_reply'| event types
Properties
Message ID
Message ID within the conversation
Destination ID (user, chat, or community)
Sender ID
Message text
Message timestamp
Whether the message is outgoing
Whether the message is marked as important
Whether the message has text
Whether the message is a reply
Whether the message has forwarded messages
Whether the message has a payload
Whether the message has geolocation
Whether the message is from a chat
Whether the sender is a user
Whether the sender is a group
Whether this is a service message (chat event)
Event type for service messages
User ID involved in the event
Event text (e.g., new chat title)
Message attachments
Forwarded messages collection
Reply message context
Message payload (from keyboards)
Geolocation data
Regex match result (from hear middleware)
Methods
loadMessagePayload(options?)
Loads full message data from VK API if not already loaded.Force reload even if already loaded
Promise<void>
send(text, params?)
Sends a message to the same conversation.Message text or options object
Additional send parameters
Promise<MessageContext>
reply(text, params?)
Replies to the message.Reply text or options object
Additional send parameters
Promise<MessageContext>
sendSticker(stickerId)
Sends a sticker to the conversation.Sticker ID
Promise<MessageContext>
sendPhoto(source, params?)
Uploads and sends a photo.Photo source (file path, buffer, stream, or URL)
Additional send parameters
Promise<MessageContext>
sendDocument(source, params?)
Uploads and sends a document.Document source
Additional send parameters
Promise<MessageContext>
sendAudioMessage(source, params?)
Uploads and sends a voice message.Audio source (must be OGG format)
Additional send parameters
Promise<MessageContext>
markAsRead()
Marks the message as read.Promise<1>
editMessage(options)
Edits the message (only for messages sent by the bot).Edit parameters (message, attachment, keyboard, etc.)
Promise<1>
deleteMessage(options?)
Deletes the message.Delete options (spam, delete_for_all)
Promise<1>
renameChat(title)
Renames the chat (only works in chats).New chat title
Promise<1>
setActivity()
Sets “typing” activity in the conversation.Promise<1>
IMessageContextSendOptions
Usage Example
MessageEventContext
Represents a callback button click event.Type
- type:
'message_event' - subTypes:
'message_event'
Properties
User ID who clicked the button
Conversation ID
Message ID within the conversation
Event ID (valid for 1 minute)
Button payload
Methods
answer(eventData)
Sends an event answer (shows snackbar, opens link, or opens app).Event action to perform
Promise<1>
send(text, params?)
Sends a message to the conversation.Message text or options
Promise<MessageContext>
MessageEventAction Types
Usage Example
WallPostContext
Represents a wall post event.Type
- type:
'wall_post' - subTypes:
'wall_post_new'|'wall_repost'
Properties
Post ID
Wall owner ID
Author ID
Post timestamp
Post text
Signer ID (if signed)
Post attachments
Whether the post is a repost
CommentContext
Represents a comment event (wall, photo, video, market).Type
- type:
'comment' - subTypes: Various comment types
Properties
Comment ID
Owner ID
Author ID
Comment text
Comment attachments
Other Context Types
VK-IO provides many other context types:- TypingContext - User typing events
- VoteContext - Poll vote events
- GroupMemberContext - Group join/leave events
- GroupUserContext - User block/unblock events
- LikeContext - Like add/remove events
- MarketOrderContext - Market order events
- MessageFlagsContext - Message flag changes
- MessagesReadContext - Message read receipts
- NewAttachmentsContext - New attachments in chat
- DialogFlagsContext - Dialog flag changes
- FriendActivityContext - Friend online/offline
- DonutSubscriptionContext - Donut subscription events
- VKPayTransactionContext - VK Pay transaction events
- VKAppPayloadContext - VK App events
Mixins
Some contexts include additional functionality through mixins:Attachmentable Mixin
Contexts with attachments include these methods:hasAttachments(type?)
Checks if the context has attachments.Optional attachment type to filter
boolean
getAttachments(type?)
Returns attachments, optionally filtered by type.Optional attachment type to filter
Attachment[]
State Management
All contexts support astate property for passing data between middleware: