Core

Core

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 => Core#on handler for the service_action event
  2. If user pressed the Start button => Core#on handler for the start event
  3. Core#payload
  4. Core#cmd
  5. Core#regex

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

Constructor

new Core(api, stats, cmdPrefix, groupId) → {Core}

Source:
Parameters:
Name Type Description
api API

API object

stats Stats

statistics object

cmdPrefix string

command prefix

groupId string | number

group id

Returns:
Type:
Core

Members

(static, readonly) this.api :API

Source:
Type:

(static) this.cmdPrefix :string

Command prefix

Source:
Type:
  • string

(static) this.commandHandlers :Array.<Object>

Command handlers

Source:
Type:
  • Array.<Object>

(static) this.dynPayloadHandlers :Array.<Object>

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

Source:
Type:
  • Array.<Object>

(static) this.escapedCmdPrefix :string

Command prefix, escaped for usage in regular expressions

Source:
Type:
  • string

(static) this.eventHandlers :Object

Handlers for events

Source:
Type:
  • Object

(static) this.eventWarnings :boolean

Are event warnings enabled?

Source:
Type:
  • boolean

(static) this.exactPayloadHandlers :Object

Exact payload handlers

Source:
Type:
  • Object

(static) this.groupId :string

Group ID

Source:
Type:
  • string

(static) this.helpMessage :string

The help message

Source:
Type:
  • string

(static) this.locked :boolean

Is this Core locked?

Source:
Type:
  • boolean

(static) this.regexHandlers :Array.<Object>

Regular expression handlers

Source:
Type:
  • Array.<Object>

(static, readonly) this.stats :Stats

Source:
Type:

Methods

cmd(command, callback, descriptionopt)

Registers a command handler

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

Source:
Parameters:
Name Type Attributes Default Description
command string

command

callback handler

function, which will handle the message

description string <optional>
""

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

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

(async) event(name, $)

Handles an event

Source:
Parameters:
Name Type Description
name string

event name

$ Context

context object

generateHelpMessage()

Generates the help message

Source:

help() → {string}

Returns the help message

Source:
Returns:
Type:
string

the help message

isLocked() → {boolean}

Returns whether this Core is locked, and prints a message to notify the user if it is locked

Source:
Returns:
Type:
boolean

is this Core locked?

lock()

Locks this Core, so new handlers can't be added, and generates the help message for later usage

Source:

noEventWarnings()

Disables warnings about missing event handlers

Source:

on(event, callback)

Registers an event handler

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

Events

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
Source:
Parameters:
Name Type Description
event string

event name

callback handler

function, which will handle the message

Example
core.on('no_match', $ => {
  $.text('I don\'t know how to respond to your message.');
});

(async) parseRequest(body)

Parses the request, creates a Context, and proceeds to call Core#event to handle the event

Source:
Parameters:
Name Type Description
body Object

body of the request, in parsed JSON

payload(payload, callback)

Registers a payload handler

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

Source:
Parameters:
Name Type Description
payload Object | payload_tester

exact payload to handle, or a function which will determine whether to handle the payload or not

callback handler

function, which will handle the message

Example
// -------> 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'}
     new Button('Test 1', colors.default, {a: 'b'}),
     new Button('Test 2', colors.default, {a: 'b', c: 'd'})
]], false);

// When asked, send the keyboard
core.regex(/keyboard/i, $ => {
   $.keyboard(kbd);
   $.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'!`);
});

regex(regex, callback)

Registers a regex handler

Source:
Parameters:
Name Type Description
regex RegExp

regular expression

callback handler

function, which will handle the message

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

registerMessageNewHandler()

Registers a handler for message_new event

Source:

(async) tryHandleCommand($) → {boolean}

Tries to handle the message in the given Context with a command handler

Source:
Parameters:
Name Type Description
$ Context

context object

Returns:
Type:
boolean

was the message handled?

(async) tryHandlePayload($) → {boolean}

Tries to handle the message in the given Context with a payload handler

Source:
Parameters:
Name Type Description
$ Context

context object

Returns:
Type:
boolean

was the message handled?

(async) tryHandleRegex($) → {boolean}

Tries to handle the message in the given Context with a regex handler

Source:
Parameters:
Name Type Description
$ Context

context object

Returns:
Type:
boolean

was the message handled?