node-vk-bot-api
Advanced tools
Comparing version 0.1.3 to 0.1.4
const app = require('node-vk-bot-api'); | ||
const api = require('node-vk-bot-api/method'); | ||
// Set token | ||
app.setToken('88935996c67c290f47b79a0c8b0093227e916ce14c62e490aa96c8f8ed3090c9cbcdda92c8fadf1f5c74c'); | ||
app.auth(process.env.BOT_TOKEN); | ||
// Send 'Hello' to user on command '/start' | ||
app.addCommand('/start', (data) => { | ||
app.command('/start', (data) => { | ||
const uid = data.user_id; | ||
app.sendMessage({ user_id: uid, message: 'Hello, this is /start command!' }); // => '{ response: [ 1 ] }' | ||
app.sendMessage({ user_id: uid, message: 'Hello, this is /start command!' }); | ||
}); | ||
// Send name and surname to user on command '/me' | ||
app.addCommand('/me', (data) => { | ||
app.command('/me', (data) => { | ||
const uid = data.user_id; | ||
@@ -25,11 +22,13 @@ | ||
}); | ||
// => '{ response: [ 2 ] }' | ||
}); | ||
}); | ||
// Reply same message if user sent not command. | ||
app.notCommand(data => { | ||
app.hears('hello', (data) => { | ||
const uid = data.user_id; | ||
const date = data.date; | ||
app.sendMessage({ user_id: uid, message: 'Hi!' }); | ||
}); | ||
app.reserve(data => { | ||
const uid = data.user_id; | ||
const msg = data.msg; | ||
@@ -40,3 +39,2 @@ | ||
// Start long poll | ||
app.startLongPoll(); |
28
index.js
@@ -7,2 +7,5 @@ const request = require('request'); | ||
action.commands = {}; | ||
action.hears = {}; | ||
let execute = []; | ||
@@ -28,11 +31,14 @@ | ||
module.exports = { | ||
setToken: function(token) { | ||
auth: function(token) { | ||
group.token = token; | ||
}, | ||
addCommand: function(command, callback) { | ||
action[command] = callback; | ||
command: function(command, callback) { | ||
action.commands[command.toLowerCase()] = callback; | ||
}, | ||
notCommand: function(callback) { | ||
action.not_command = callback; | ||
hears: function(command, callback) { | ||
action.hears[command.toLowerCase()] = callback; | ||
}, | ||
reserve: function(callback) { | ||
action.reserve = callback; | ||
}, | ||
sendMessage: function(opts) { | ||
@@ -122,6 +128,12 @@ execute.push(opts); | ||
if (action[update.msg]) { | ||
action[update.msg](update); | ||
if (action.commands[update.msg.toLowerCase()]) { | ||
action.commands[update.msg.toLowerCase()](update); | ||
} else { | ||
action.not_command(update); | ||
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); | ||
} | ||
}); | ||
} | ||
@@ -128,0 +140,0 @@ }); |
{ | ||
"name": "node-vk-bot-api", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"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", |
114
README.md
@@ -18,16 +18,18 @@ # VK Bot API | ||
// Set token | ||
app.setToken('88935996c67c2qwed1547b79a0c8b0093227e61sd15ce14c62e490aa96c8f8ed3090c9cbcdda92c8fadf1f5c74c'); | ||
app.auth(process.env.BOT_TOKEN); | ||
// Send 'Hello' to user on command '/start' | ||
app.addCommand('/start', (data) => { | ||
app.command('/start', (data) => { | ||
const uid = data.user_id; | ||
app.sendMessage({ user_id: uid, message: 'Hello, this is /start command!' }); // => '{ response: [ 1 ] }' | ||
app.sendMessage({ user_id: uid, message: 'Hello, this is /start command!' }); | ||
}); | ||
// Reply same message if user sent not command. | ||
app.notCommand(data => { | ||
app.hears('hello', (data) => { | ||
const uid = data.user_id; | ||
const date = data.date; | ||
app.sendMessage({ user_id: uid, message: 'Hi!' }); | ||
}); | ||
app.reserve(data => { | ||
const uid = data.user_id; | ||
const msg = data.msg; | ||
@@ -38,3 +40,2 @@ | ||
// Start long poll | ||
app.startLongPoll(); | ||
@@ -45,15 +46,20 @@ ``` | ||
* [.setToken(token)](https://www.npmjs.com/package/node-vk-bot-api#settokentoken) | ||
* [.addCommand(command, callback)](https://www.npmjs.com/package/node-vk-bot-api#addcommandcommand-callback) | ||
* [.notCommand(callback)](https://www.npmjs.com/package/node-vk-bot-api#notcommandcallback) | ||
* [.sendMessage(opts)](https://www.npmjs.com/package/node-vk-bot-api#sendmessageopts) | ||
* [.getLastMessage(update)](https://www.npmjs.com/package/node-vk-bot-api#getlastmessageupdate) | ||
* [.getForwardMessage(update)](https://www.npmjs.com/package/node-vk-bot-api#getforwardmessageupdate) | ||
* [.startLongPoll()](https://www.npmjs.com/package/node-vk-bot-api#startlongpoll) | ||
* [.getLongPoll(longPollParams)](https://www.npmjs.com/package/node-vk-bot-api#getlongpolllongpollparams) | ||
* [.auth(token)](https://github.com/bifot/node-vk-bot-api#authtoken) | ||
* [.command(command, callback)](https://github.com/bifot/node-vk-bot-api#commandcommand-callback) | ||
* [.hears(command, callback)](https://github.com/bifot/node-vk-bot-api#hearscommand-callback) | ||
* [.reserve(callback)](https://github.com/bifot/node-vk-bot-api#reservecallback) | ||
* [.sendMessage(opts)](https://github.com/bifot/node-vk-bot-api#sendmessageopts) | ||
* [.getLastMessage(update)](https://github.com/bifot/node-vk-bot-api#getlastmessageupdate) | ||
* [.getForwardMessage(update)](https://github.com/bifot/node-vk-bot-api#getforwardmessageupdate) | ||
* [.startLongPoll()](https://github.com/bifot/node-vk-bot-api#startlongpoll) | ||
* [.getLongPoll()](https://github.com/bifot/node-vk-bot-api#getlongpoll) | ||
### .setToken(token) | ||
### .auth(token) | ||
Set token to bot. Function takes one argument: `token` **(string)**. | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ---------:| | ||
| token | string | yes | | ||
Authting with token. | ||
```javascript | ||
@@ -63,21 +69,43 @@ app.setToken('88935996c67c290f47b79a0c8b0093227e916ce14c62e490aa96c8f8ed3090c9cbcdda92c8fadf1f5c74c'); | ||
### .addCommand(command, callback) | ||
### .command(command, callback) | ||
Add command to bot. Function takes two arguments: `command` **(string)** and `callback` **(function)**. | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ---------:| | ||
| command | string | yes | | ||
| callback | function | yes | | ||
If bot get message which equal to command, then will run callback. | ||
```javascript | ||
app.addCommand('/start', (data) => { | ||
// Make your magic here... | ||
app.command('/start', (data) => { | ||
app.sendMessage({ user_id: data.user_id, message: 'This is start command!' }); | ||
}); | ||
``` | ||
### .notCommand(callback) | ||
### .hears(command, callback) | ||
Set reply if user sent not command. Function takes one argument: `callback` **(function)**. | ||
| 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). | ||
```javascript | ||
app.notCommand(data => { | ||
const uid = data.user_id; | ||
app.hears('hello', (data) => { | ||
app.sendMessage({ user_id: data.user_id, message: 'Hi!' }); | ||
}); | ||
``` | ||
app.sendMessage({ user_id: uid, message: 'Sorry, you sent not command to bot.' }); | ||
### .reserve(callback) | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ---------:| | ||
| callback | function | yes | | ||
If bot get message and this isn't command, then will run reserved callback. | ||
```javascript | ||
app.reserve(data => { | ||
app.sendMessage({ user_id: data.user_id, message: 'Sorry, you sent not command to bot.' }); | ||
}); | ||
@@ -88,6 +116,10 @@ ``` | ||
Send message (multi-dispatch) to users. Function takes one argument: `opts` **(object)**. | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ---------:| | ||
| opts | object | yes | | ||
Send message (multi-dispatch). | ||
```javascript | ||
app.sendMessage({ user_id: uid, message: 'Hello, world!' }); | ||
app.sendMessage({ user_id: data.user_id, message: 'Hello, world!' }); | ||
``` | ||
@@ -97,4 +129,8 @@ | ||
Get last message from forward message. Function takes one argument: `update` **(object)**. | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ---------:| | ||
| update | object | yes | | ||
Get last message from forward message. | ||
```javascript | ||
@@ -124,4 +160,8 @@ app.getLastMessage({ | ||
Get message info from forward message. If function detects `fwd_messages`, then will call `.getLastMessage(update)`. Function takes one argument: `update` **(array)**. | ||
| Parameter | Type | Requried | | ||
| -----------|:---------:| ---------:| | ||
| update | array | yes | | ||
Get message info from forward message. If function detects `fwd_messages`, then will call `.getLastMessage(update)`. | ||
```javascript | ||
@@ -133,3 +173,3 @@ app.getForwardMessage([ 4, 487, 529, 145003487, 1491653078, ' ... ', '', { fwd: '145003487_2214301' } ]); | ||
Get long poll params. Function doesn't take arguments. | ||
Get long poll params. | ||
@@ -140,12 +180,8 @@ ```javascript | ||
### .getLongPoll(longPollParams) | ||
### .getLongPoll() | ||
Start long poll. Function takes one argument: `longPollParams` **(object)**. | ||
Start long poll. | ||
``` | ||
System function. Not used by user in bot. | ||
``` | ||
## License | ||
MIT. |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
11451
172
179
1