node-vk-bot-api
Advanced tools
Comparing version 0.2.0 to 0.2.1
const app = require('node-vk-bot-api'); | ||
const api = require('node-vk-bot-api/method'); | ||
app.auth(process.env.BOT_TOKEN); | ||
app.auth(process.env.BOT_TOKEN, { | ||
subscribers: 1, | ||
gid: 138165805, | ||
msg: 'Bot available only for subscribers. Subscribe and then try again. <3' | ||
}); | ||
@@ -32,5 +36,5 @@ app.command('/start', (data) => { | ||
app.sendMessage(uid, msg, 'wall145003487_1900'); // => '{ response: [ 3 ] }' | ||
app.sendMessage(uid, msg, 'wall145003487_1900'); | ||
}); | ||
app.startLongPoll(); |
74
index.js
@@ -30,4 +30,8 @@ const request = require('request'); | ||
module.exports = { | ||
auth: function(token) { | ||
auth: function(token, opts) { | ||
group.token = token; | ||
if (opts) { | ||
group.mode = opts; | ||
} | ||
}, | ||
@@ -43,2 +47,17 @@ command: function(command, callback) { | ||
}, | ||
isMember: function(gid, uid) { | ||
return new Promise((resolve, reject) => { | ||
api('groups.isMember', { | ||
group_id: gid, | ||
user_id: uid, | ||
v: 5.62 | ||
}).then(body => { | ||
if (body.response) { | ||
resolve('User is subscriber'); | ||
} else { | ||
reject('User isn\'t subscriber'); | ||
} | ||
}); | ||
}); | ||
}, | ||
sendMessage: function(uid, msg, attach) { | ||
@@ -49,2 +68,25 @@ const options = (typeof uid == 'object') ? uid : { user_id: uid, message: msg, attachment: attach }; | ||
}, | ||
replyMessage: function(updates) { | ||
this.getForwardMessage(updates).then(data => { | ||
const update = (Object.keys(data).length == 3) | ||
? { user_id: updates[3], date: data.date, msg: data.body } | ||
: { user_id: updates[3], date: data[4], msg: data[6] }; | ||
if (action.commands[update.msg.toLowerCase()]) { | ||
action.commands[update.msg.toLowerCase()](update); | ||
} else { | ||
if (Object.keys(action.hears).length) { | ||
Object.keys(action.hears).forEach((cmd, i) => { | ||
if (new RegExp(cmd, 'i').test(update.msg.toLowerCase())) { | ||
action.hears[cmd](update); | ||
} else if (i == Object.keys(action.hears).length - 1) { | ||
action.reserve(update); | ||
} | ||
}); | ||
} else { | ||
action.reserve(update); | ||
} | ||
} | ||
}); | ||
}, | ||
getLastMessage: function(update) { | ||
@@ -130,23 +172,11 @@ if (update.fwd_messages && update.fwd_messages.length) { | ||
this.getForwardMessage(update).then(data => { | ||
const update = (Object.keys(data).length == 3) | ||
? { user_id: uid, date: data.date, msg: data.body } | ||
: { user_id: uid, date: data[4], msg: data[6] }; | ||
if (action.commands[update.msg.toLowerCase()]) { | ||
action.commands[update.msg.toLowerCase()](update); | ||
} else { | ||
if (Object.keys(action.hears).length) { | ||
Object.keys(action.hears).forEach((cmd, i) => { | ||
if (new RegExp(cmd, 'i').test(update.msg.toLowerCase())) { | ||
action.hears[cmd](update); | ||
} else if (i == Object.keys(action.hears).length - 1) { | ||
action.reserve(update); | ||
} | ||
}); | ||
} else { | ||
action.reserve(update); | ||
} | ||
} | ||
}); | ||
if (group.mode && group.mode.subscribers) { | ||
this.isMember(group.mode.gid, uid).then(() => { | ||
this.replyMessage(update); | ||
}).catch(() => { | ||
this.sendMessage(uid, group.mode.msg); | ||
}); | ||
} else { | ||
this.replyMessage(update); | ||
} | ||
} | ||
@@ -153,0 +183,0 @@ |
{ | ||
"name": "node-vk-bot-api", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Clean API for VK bots based on long poll with multi-dispatch send messages (~75 per second).", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,3 @@ | ||
[![node-vk-bot-api](https://img.shields.io/npm/v/node-vk-bot-api.svg)](https://www.npmjs.com/package/node-vk-bot-api/) | ||
# VK Bot API | ||
@@ -44,3 +46,3 @@ | ||
* [.auth(token)](#authtoken) | ||
* [.auth(token)](#authtoken-opts) | ||
* [.command(command, callback)](#commandcommand-callback) | ||
@@ -50,8 +52,9 @@ * [.hears(command, callback)](#hearscommand-callback) | ||
* [.sendMessage(uid, msg, attach)](#sendmessageuid-msg-attach) | ||
* [.replyMessage(updates)](#replymessageupdates) | ||
* [.getLastMessage(update)](#getlastmessageupdate) | ||
* [.getForwardMessage(update)](#getforwardmessageupdate) | ||
* [.startLongPoll()](#startlongpoll) | ||
* [.getLongPoll()](#getlongpoll) | ||
* [.getLongPoll(longPollParams)](#getlongpolllongpollparams) | ||
### .auth(token) | ||
### .auth(token, opts) | ||
@@ -61,9 +64,21 @@ | Parameter | Type | Requried | | ||
| token | string | yes | | ||
| opts | object | no | | ||
Authting with token. | ||
Authting with token. Also you can set `subscribers mode` and bot will reply only to subscribers. | ||
```javascript | ||
app.auth('88935996c67c290f47b79a0c8b0093227e916ce14c62e490aa96c8f8ed3090c9cbcdda92c8fadf1f5c74c'); | ||
// Bot will reply to all | ||
app.auth(process.env.BOT_TOKEN); | ||
``` | ||
```javascript | ||
// Bot will reply only to subscribers. | ||
// If user isn't subscriber, bot will send 'Bot available only for subscribers ...' | ||
app.auth(process.env.BOT_TOKEN, { | ||
subscribers: 1, // mode on | ||
gid: 138165805, // group_id | ||
msg: 'Bot available only for subscribers. Subscribe and then try again. <3' // message | ||
}); | ||
``` | ||
### .command(command, callback) | ||
@@ -133,2 +148,10 @@ | ||
### .replyMessage(updates) | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ----------------------------:| | ||
| updates | array | yes | | ||
Core function for reply message to user. In the start function calls `[getForwardMessage](#getforwardmessageupdate)` and then see is the message a command or action and calls `[sendMessage(#sendmessageuid-msg-attach)]`. | ||
### .getLastMessage(update) | ||
@@ -135,0 +158,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13819
211
214