Members
(static) this.commandHandlers :Array.<Object>
Command handlers
Type:
-
Array.<Object>
(static) this.dynPayloadHandlers :Array.<Object>
Dynamic payload handlers (which use functions to determine whether a handler is suitable)
Type:
-
Array.<Object>
(static) this.escapedCmdPrefix :string
Command prefix, escaped for usage in regular expressions
Type:
-
string
(static) this.eventWarnings :boolean
Are event warnings enabled?
Type:
-
boolean
(static) this.exactPayloadHandlers :Object
Exact payload handlers
Type:
-
Object
(static) this.regexHandlers :Array.<Object>
Regular expression handlers
Type:
-
Array.<Object>
Methods
cmd(command, callback, descriptionopt)
Registers a command handler
Handler is called if the message begins with cmd_prefix
(defined in the parameters) + command
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
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
event name |
$ |
Context
|
context object |
help() → {string}
Returns the help message
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
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
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
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
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.
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
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);
});
(async) tryHandleCommand($) → {boolean}
Tries to handle the message in the given Context
with a command handler
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
Parameters:
Name | Type | Description |
---|---|---|
$ |
Context
|
context object |
Returns:
- Type:
-
boolean
was the message handled?