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 see here.
const app = require('node-vk-bot-api');
app.auth(process.env.BOT_TOKEN);
app.command('/start', (data) => {
const uid = data.user_id;
app.sendMessage(uid, 'Hello, this is /start command!');
});
app.hears('hello', (data) => {
const uid = data.user_id;
app.sendMessage(uid, 'Hi!');
});
app.reserve(data => {
const uid = data.user_id;
const msg = data.msg;
app.sendMessage(uid, msg, 'wall145003487_1900');
});
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(uid, msg, attach)
Parameter | Type | Requried |
---|
uid | number | yes |
msg | string | yes (no, if setten attach) |
attach | string | yes (no, if setten msg) |
Send message (multi-dispatch). Also you can only one argument opts
, it's must be equal to object
All params for this object you can see on messages.send page.
app.sendMessage(data.user_id, 'Hello, world!');
app.sendMessage({
user_id: data.user_id,
message: 'Hello, function takes only one argument now. It\'s opts.',
forward_messages: '123,431,544'
});
.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.