VK Bot API
Clean API for VK bots based on long poll with multi-dispatch send messages (~75 per second).
Install
$ npm install node-vk-bot-api
Example
Full example you can check in examples
folder.
const app = require('node-vk-bot-api');
app.auth(process.env.BOT_TOKEN);
app.command('/start', (data) => {
const uid = data.user_id;
app.sendMessage({ user_id: uid, message: 'Hello, this is /start command!' });
});
app.hears('hello', (data) => {
const uid = data.user_id;
app.sendMessage({ user_id: uid, message: 'Hi!' });
});
app.reserve(data => {
const uid = data.user_id;
const msg = data.msg;
app.sendMessage({ user_id: uid, message: msg });
});
app.startLongPoll();
Methods
.auth(token)
Parameter | Type | Requried |
---|
token | string | yes |
Authting with token.
app.setToken('88935996c67c290f47b79a0c8b0093227e916ce14c62e490aa96c8f8ed3090c9cbcdda92c8fadf1f5c74c');
.command(command, callback)
Parameter | Type | Requried |
---|
command | string | yes |
callback | function | yes |
If bot get message which equal to command, then will run callback.
app.command('/start', (data) => {
app.sendMessage({ user_id: data.user_id, message: 'This is start command!' });
});
.hears(command, callback)
Parameter | Type | Requried |
---|
command | string | yes |
callback | function | yes |
If bot hears command in message from user, then will run callback (e.g. user sent 'Hello, world' and bot hears 'hello', then bot will run callback).
app.hears('hello', (data) => {
app.sendMessage({ user_id: data.user_id, message: 'Hi!' });
});
.reserve(callback)
Parameter | Type | Requried |
---|
callback | function | yes |
If bot get message and this isn't command, then will run reserved callback.
app.reserve(data => {
app.sendMessage({ user_id: data.user_id, message: 'Sorry, you sent not command to bot.' });
});
.sendMessage(opts)
Parameter | Type | Requried |
---|
opts | object | yes |
Send message (multi-dispatch).
app.sendMessage({ user_id: data.user_id, message: 'Hello, world!' });
.getLastMessage(update)
Parameter | Type | Requried |
---|
update | object | yes |
Get last message from forward message.
app.getLastMessage({
"response": {
"count": 1,
"items": [{
"id": 480,
"date": 1491653021,
"out": 0,
"user_id": 145003487,
"read_state": 1,
"title": " ... ",
"body": "",
"fwd_messages": [{
"user_id": -138165805,
"date": 1491652976,
"body": "Hello, world!"
}]
}]
}
});
.getForwardMessage(update)
Parameter | Type | Requried |
---|
update | array | yes |
Get message info from forward message. If function detects fwd_messages
, then will call .getLastMessage(update)
.
app.getForwardMessage([ 4, 487, 529, 145003487, 1491653078, ' ... ', '', { fwd: '145003487_2214301' } ]);
.startLongPoll()
Get long poll params.
app.startLongPoll();
.getLongPoll()
Start long poll.
License
MIT.