• Public
  • Public/Protected
  • All

Class Core

Dispatches message handling to appropriate handlers, and is used for setting these handlers.

Handlers for the message_new event will be searched in this order:

  1. If service action message => on handler for the service_action event
  2. If user pressed the Start button => on handler for the start event
  3. payload
  4. cmd
  5. regex

For other events, a matching on handler will be called.


  • Core




  • new Core(api: API, stats: Stats, cmdPrefix: string, groupId: string | number): Core
  • Creates a new Core.


    • api: API

      API object

    • stats: Stats

      Stats object

    • cmdPrefix: string

      command prefix

    • groupId: string | number

      group ID

    Returns Core



api: API

Private cmdPrefix

cmdPrefix: string

Command prefix.

Private commandHandlers

commandHandlers: CommandHandler[] = []

Command handlers.

Private dynPayloadHandlers

dynPayloadHandlers: DynPayloadHandler[] = []

Dynamic payload handlers. (those which use functions to determine whether a handler is suitable)

Private escapedCmdPrefix

escapedCmdPrefix: string

Command prefix, escaped for usage in regular expressions

Private eventWarnings

eventWarnings: boolean = true

Are event warnings enabled?

Private exactPayloadHandlers

exactPayloadHandlers: object

Exact payload handlers.

Type declaration

Private groupId

groupId: string

Group ID.

Private helpMessage

helpMessage: string = ""

The help message.

Private locked

locked: boolean = false

Is this Core locked?

Private regexHandlers

regexHandlers: object[] = []

Regular expression handlers.


stats: Stats



  • cmd(command: string, handler: Handler, description?: string): void
  • Registers a command handler.

    Handler is called if the message begins with cmd_prefix (defined in the parameters) + command

    core.cmd('help', $ => {
      // core.help() returns the help message
      $.text('Test Bot' + core.help());
    }, 'shows the help message');


    • command: string


    • handler: Handler

      function which will handle the message

    • Default value description: string = ""

      the description of what this command does, to be used in help messages.

    Returns void

Private event

  • event(name: string, $: Context): Promise<void>
  • Handles an event.


    Returns Promise<void>

Private generateHelpMessage

  • generateHelpMessage(): void
  • Generates the help message.

    Returns void


  • getHandlerCounts(): object
  • Returns handler counts, except message_new event since it is built-in.

    Returns object

    • cmd: number
    • evt: number
    • pld: number
    • reg: number


  • help(): string
  • Returns the help message.

    Returns string

Private isLocked

  • isLocked(): boolean
  • Indicates whether this Core is locked, and prints a message to notify the user if it is locked.

    Returns boolean


  • lock(): void
  • Locks this Core, so new handlers can't be added, and generates the help message for later usage.

    Returns void


  • noEventWarnings(): void
  • Disables warnings about missing event handlers.

    Returns void


  • on(event: string, handler: Handler): void
  • Registers an event handler.

    Does not work for message_new, as its handler is defined by vk-chat-bot itself.


    Callback API Events

    Event Description
    message_allow User allowed sending messages to him/her
    message_deny User disallowed sending messages to him/her
    message_reply New message sent by community administrator (or by the bot itself)
    message_edit Message edited by user
    message_typing_state User is typing a message

    Other Events

    Event type When handler is called
    start If the message's payload is {"command": "start"} (i.e. Start button pressed)
    service_action Service action message received
    no_match When no matching cmd() or regex() handler is found
    handler_error If an error is thrown in a handler

    The service_action event

    The $.obj.action object contains information about the service action. It contains the following fields:

    type (string) — action type, one of:
       `chat_photo_update` — chat photo updated
       `chat_photo_remove` — chat photo removed
       `chat_create` — chat created
       `chat_title_update` — chat title updated
       `chat_invite_user` — user was invited to chat
       `chat_kick_user` — user was kicked from the chat
       `chat_pin_message` — a message was pinned
       `chat_unpin_message` — a message was unpinned
       `chat_invite_user_by_link` — user joined the chat by link
    member_id (integer):
      user id (if > 0), which was invited or kicked (if < 0, see `email` field)
        (`chat_invite_user`, `chat_invite_user_by_link`,  `chat_kick_user`)
      user id, which pinned or unpinned a message
        (`chat_pin_message`, `chat_unpin_message`)
    text (string):
      chat name
        (`chat_create`, `chat_title_update`)
    email (string):
      email, which was invited or kicked
        (`chat_invite_user`, `chat_kick_user`, member_id < 0)
    photo (object) — chat picture, contains:
        photo_50 (string): URL of image 50 x 50 px
        photo_100 (string): URL of image 100 x 100 px
        photo_200 (string): URL of image 200 x 200 px
    core.on('no_match', $ => {
      $.text('I don\'t know how to respond to your message.');


    Returns void


  • parseRequest(body: any): Promise<void>
  • Parses the request, creates a Context, and proceeds to call Core.event to handle the event


    • body: any

      body of the request, in parsed JSON

    Returns Promise<void>


  • Registers a payload handler.

    Note: exact handlers are searched first, and only if they don't match, the search for a dynamic handler begins.

    // -------> KEYBOARD (for sending the payload)
    // Create a keyboard
    const { colors, Keyboard, Button } = vk.kbd;
    var kbd = new Keyboard([
            // Clicking on this button will send the payload {a: 'b'}
            button.text('Test 1', colors.default, {a: 'b'}),
            button.text('Test 2', colors.default, {a: 'b', c: 'd'})
    ], false);
    // When asked, send the keyboard
    core.regex(/keyboard/i, $ => {
       $.text('Here it is!');
    // -------> EXACT PAYLOAD
    core.payload({a: 'b'}, $ => {
       $.text('Received secret payload!');
    // -------> DYNAMIC PAYLOAD
    // In this case, the handler will run only if the
    // payload's property `c` contains the value `d`.
    core.payload((payload, parsed) => {
       if (parsed) { // If the payload is a valid JSON
         return parsed.c === 'd';
       } else {
         return false;
    }, $ => {
       $.text(`In message '${$.msg}', payload.c is 'd'!`);


    • payload: Payload

      exact payload to handle, or a function (type Tester) which will determine whether to handle the payload or not.

    • handler: Handler

      function which will handle the message

    Returns void


  • regex(regex: RegExp, handler: Handler): void
  • Registers a regex handler.

    core.regex(/h(i|ello|ey)/i, $ => {
       $.text('Hello, I am a test bot. You said: ' + $.msg);


    • regex: RegExp
    • handler: Handler

      function which will handle the message

    Returns void

Private registerMessageNewHandler

  • registerMessageNewHandler(): void
  • Registers a handler for message_new event.

    Returns void

Private tryHandleCommand

  • tryHandleCommand($: Context): Promise<boolean>
  • Tries to handle the message in the given Context with a command handler.


    Returns Promise<boolean>

    was the message handled?

Private tryHandlePayload

  • tryHandlePayload($: Context): Promise<boolean>
  • Tries to handle the message in the given Context with a payload handler.


    Returns Promise<boolean>

    was the message handled?

Private tryHandleRegex

  • tryHandleRegex($: Context): Promise<boolean>
  • Tries to handle the message in the given Context with a regex handler.


    Returns Promise<boolean>

    was the message handled?

Object literals

Private eventHandlers

eventHandlers: object

Handlers for events.


handler_error: null = null


message_allow: null = null


message_deny: null = null


message_edit: null = null


message_new: null = null


message_reply: null = null


message_typing_state: null = null


no_match: null = null


service_action: null = null


start: null = null

Generated using TypeDoc