Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-vk-bot-api

Package Overview
Dependencies
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-vk-bot-api - npm Package Compare versions

Comparing version 0.3.3 to 0.4.0

lib/index.js

39

examples/bot.js

@@ -1,39 +0,38 @@

const app = require('node-vk-bot-api');
const api = require('node-vk-bot-api/method');
const app = require('../lib')
const api = require('../lib/modules/api')
app.auth(process.env.BOT_TOKEN, {
subscribers: 1,
gid: 138165805,
msg: 'Bot available only for subscribers. Subscribe and then try again. <3'
});
})
app.command('/start', (data) => {
const uid = data.user_id;
const uid = data.user_id
app.sendMessage(uid, 'Hello, this is /start command!');
});
app.sendMessage(uid, 'Hello, this is /start command!')
})
app.command('/me', (data) => {
const uid = data.user_id;
const uid = data.user_id
api('users.get', { user_id: uid }).then(body => {
const user = body.response[0];
const user = body.response[0]
app.sendMessage(uid, `You are ${user.first_name} ${user.last_name}.`, 'wall145003487_1900');
});
});
app.sendMessage(uid, `You are ${user.first_name} ${user.last_name}.`, 'wall145003487_1900')
})
})
app.hears('hello', (data) => {
const uid = data.user_id;
const uid = data.user_id
app.sendMessage(uid, 'Hi!');
});
app.sendMessage(uid, 'Hi!')
})
app.reserve(data => {
const uid = data.user_id;
const msg = data.msg;
const uid = data.user_id
const msg = data.msg
app.sendMessage(uid, msg, 'wall145003487_1900');
});
app.sendMessage(uid, msg, 'wall145003487_1900')
})
app.startLongPoll();
app.startLongPoll()

@@ -1,190 +0,1 @@

const request = require('request');
const api = require('./method');
var execute = [];
var longPollParams = {};
var group = {};
var action = {};
action.commands = {};
action.hears = {};
setInterval(() => {
if (execute.length) {
const method = [];
execute.forEach(msg => {
method.push(`API.messages.send(${JSON.stringify(msg)})`);
});
api('execute', {
code: `return [ ${method.join(',')} ];`,
access_token: group.token
}).then(console.log).catch(console.log);
execute = [];
}
}, 350);
module.exports = {
auth: function(token, opts) {
group.token = token;
if (opts) {
group.mode = opts;
}
},
command: function(command, callback) {
action.commands[command.toLowerCase()] = callback;
},
hears: function(command, callback) {
action.hears[command.toLowerCase()] = callback;
},
reserve: function(callback) {
action.reserve = 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) {
const options = (typeof uid == 'object') ? uid : { user_id: uid, message: msg, attachment: attach };
execute.push(options);
},
replyMessage: function(updates) {
this.getForwardMessage(updates).then(data => {
if (data.body || data[3]) {
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) {
if (update.fwd_messages && update.fwd_messages.length) {
return this.getLastMessage(update.fwd_messages[0]);
}
return update;
},
getForwardMessage: function(update) {
return new Promise(resolve => {
if (update[7].fwd) {
api('messages.getById', {
message_ids: update[1],
access_token: group.token
}).then(body => {
resolve(this.getLastMessage(body.response.items[0]));
});
} else {
resolve(update);
}
});
},
startLongPoll: function() {
return new Promise((resolve, reject) => {
api('messages.getLongPollServer', {
need_pts: 1,
access_token: group.token,
v: 5.62
}).then(body => {
if (body.failed) {
this.startLongPoll();
} else {
longPollParams = body.response;
this.getLongPoll();
}
});
});
},
getLongPoll: function() {
request({
url: `https://${longPollParams.server}`,
method: 'POST',
form: {
act: 'a_check',
key: longPollParams.key,
ts: longPollParams.ts,
wait: 25,
mode: 2,
version: 1
},
json: true
}, (err, res, body) => {
if (!err && res.statusCode == 200) {
if (body.ts) {
longPollParams.ts = body.ts;
} else {
this.startLongPoll();
return;
}
const updates = body.updates;
if (!updates || updates.length == 0) {
this.getLongPoll();
return;
}
for (let i = 0, l = updates.length - 1; i <= l; i++) {
const update = updates[i];
if (update[0] != 4) {
continue;
}
const flags = update[2];
if ((flags & 2) != 0) {
continue;
}
const uid = update[3];
if (group.mode && group.mode.subscribers) {
this.isMember(group.mode.gid, uid).then(() => {
this.replyMessage(update);
}).catch(err => {
console.log(err);
this.sendMessage(uid, group.mode.msg);
});
} else {
this.replyMessage(update);
}
}
this.getLongPoll();
}
});
}
};
module.exports = require('./lib/index')
{
"name": "node-vk-bot-api",
"version": "0.3.3",
"description": "Clean API for VK bots based on long poll with multi-dispatch send messages (~75 per second).",
"version": "0.4.0",
"description": "API for VK bots based on long poll with multi-dispatch send messages (~75 per second).",
"main": "index.js",

@@ -39,3 +39,7 @@ "scripts": {

"request": "^2.81.0"
}
},
"directories": {
"example": "examples"
},
"devDependencies": {}
}
[![node-vk-bot-api](https://img.shields.io/npm/v/node-vk-bot-api.svg)](https://www.npmjs.com/package/node-vk-bot-api/)
[![node-vk-bot-api](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

@@ -18,26 +19,26 @@ # VK Bot API

```javascript
const app = require('node-vk-bot-api');
const app = require('node-vk-bot-api')
app.auth(process.env.BOT_TOKEN);
app.auth(process.env.BOT_TOKEN)
app.command('/start', (data) => {
const uid = data.user_id;
const uid = data.user_id
app.sendMessage(uid, 'Hello, this is /start command!');
});
app.sendMessage(uid, 'Hello, this is /start command!')
})
app.hears('hello', (data) => {
const uid = data.user_id;
const uid = data.user_id
app.sendMessage(uid, 'Hi!');
});
app.sendMessage(uid, 'Hi!')
})
app.reserve(data => {
const uid = data.user_id;
const msg = data.msg;
const uid = data.user_id
const msg = data.msg
app.sendMessage(uid, msg, 'wall145003487_1900');
});
app.sendMessage(uid, msg, 'wall145003487_1900')
})
app.startLongPoll();
app.startLongPoll()
```

@@ -69,3 +70,3 @@

// Bot will reply to all
app.auth(process.env.BOT_TOKEN);
app.auth(process.env.BOT_TOKEN)
```

@@ -77,6 +78,5 @@

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
});
gid: 138165805, // group id
msg: 'Bot available only for subscribers. Subscribe and then try again. <3'
})
```

@@ -95,4 +95,4 @@

app.command('/start', (data) => {
app.sendMessage(data.user_id, 'This is start command!');
});
app.sendMessage(data.user_id, 'This is start command!')
})
```

@@ -111,4 +111,4 @@

app.hears('hello', (data) => {
app.sendMessage(data.user_id, 'Hi!');
});
app.sendMessage(data.user_id, 'Hi!')
})
```

@@ -126,4 +126,4 @@

app.reserve(data => {
app.sendMessage(data.user_id, 'Sorry, you sent not command to bot.');
});
app.sendMessage(data.user_id, 'Sorry, you sent not command to bot.')
})
```

@@ -142,3 +142,3 @@

```javascript
app.sendMessage(data.user_id, 'Hello, world!');
app.sendMessage(data.user_id, 'Hello, world!')

@@ -149,3 +149,3 @@ app.sendMessage({

forward_messages: '123,431,544'
});
})
```

@@ -188,3 +188,3 @@

}
});
})
```

@@ -201,3 +201,3 @@

```javascript
app.getForwardMessage([ 4, 487, 529, 145003487, 1491653078, ' ... ', '', { fwd: '145003487_2214301' } ]);
app.getForwardMessage([ 4, 487, 529, 145003487, 1491653078, ' ... ', '', { fwd: '145003487_2214301' } ])
```

@@ -210,3 +210,3 @@

```javascript
app.startLongPoll();
app.startLongPoll()
```

@@ -213,0 +213,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc