node-vk-bot-api
Advanced tools
Comparing version 2.4.11 to 3.0.0
const axios = require('axios'); | ||
const { stringify } = require('querystring'); | ||
const ApiError = require('./errors/ApiError'); | ||
module.exports = async function (method, settings = {}) { | ||
try { | ||
const { data } = await axios.post(`https://api.vk.com/method/${method}`, stringify({ | ||
v: 5.80, | ||
...settings, | ||
})); | ||
const { data } = await axios.post(`https://api.vk.com/method/${method}`, stringify({ | ||
v: 5.80, | ||
...settings, | ||
})); | ||
if (data.error) { | ||
throw JSON.stringify(data); | ||
} | ||
if (data.error) { | ||
throw new ApiError(data.error); | ||
} | ||
return data; | ||
} catch (err) { | ||
throw (typeof err === 'object' ? JSON.stringify(err) : err); | ||
} | ||
return data; | ||
}; |
const methods = require('./methods'); | ||
const api = require('./api'); | ||
const { callExecute } = require('./utils'); | ||
const { execute } = require('./utils'); | ||
@@ -15,8 +15,14 @@ class VkBot { | ||
this.methods = []; | ||
this.settings = Object.assign({}, { | ||
polling_timeout: 25, | ||
execute_timeout: 50, | ||
}, typeof settings === 'object' ? settings : { token: settings }); | ||
Object.entries({ ...methods, api, callExecute }).forEach(([key, method]) => { | ||
this.settings = Object.assign( | ||
{ | ||
polling_timeout: 25, | ||
execute_timeout: 50, | ||
}, | ||
typeof settings === 'object' | ||
? settings | ||
: { token: settings }, | ||
); | ||
Object.entries({ ...methods, api }).forEach(([key, method]) => { | ||
this[key] = method.bind(this); | ||
@@ -26,3 +32,7 @@ }); | ||
setInterval(() => { | ||
this.callExecute(this.methods); | ||
execute( | ||
this.methods, | ||
this.settings.token, | ||
); | ||
this.methods = []; | ||
@@ -29,0 +39,0 @@ }, settings.execute_timeout); |
@@ -1,3 +0,3 @@ | ||
module.exports = function (method, settings, callback = () => {}) { | ||
this.methods.push({ | ||
module.exports = function (method, settings, callback) { | ||
const request = { | ||
code: `API.${method}(${JSON.stringify({ | ||
@@ -8,5 +8,12 @@ v: '5.80', | ||
callback, | ||
}; | ||
const promise = new Promise((resolve, reject) => { | ||
request.resolve = resolve; | ||
request.reject = reject; | ||
}); | ||
return this; | ||
this.methods.push(request); | ||
return promise; | ||
}; |
@@ -0,1 +1,3 @@ | ||
const PollingError = require('../errors/PollingError'); | ||
module.exports = async function () { | ||
@@ -10,15 +12,12 @@ if (!this.settings.group_id) { | ||
const { response } = await this.api('groups.getLongPollServer', { | ||
group_id: this.settings.group_id, | ||
access_token: this.settings.token, | ||
}).catch((err) => { | ||
const { error } = JSON.parse(err); | ||
try { | ||
const { response } = await this.api('groups.getLongPollServer', { | ||
group_id: this.settings.group_id, | ||
access_token: this.settings.token, | ||
}); | ||
if (error.error_code === 15) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
}); | ||
return response; | ||
return response; | ||
} catch (err) { | ||
throw new PollingError(err); | ||
} | ||
}; |
const axios = require('axios'); | ||
const Context = require('../context'); | ||
const PollingError = require('../errors/PollingError'); | ||
class PollingError extends Error {} | ||
module.exports = async function (callback, ts) { | ||
@@ -23,22 +22,13 @@ try { | ||
}, | ||
}).catch(() => { | ||
throw new PollingError(); | ||
}); | ||
if (body.failed === 1) { | ||
return this.startPolling(null, body.ts); | ||
} | ||
if (body.failed) { | ||
switch (body.failed) { | ||
case 1: | ||
return this.startPolling(null, body.ts); | ||
case 2: | ||
case 3: | ||
this.longPollParams = null; | ||
this.longPollParams = null; | ||
this.startPolling(); | ||
return this.startPolling(); | ||
default: | ||
console.error(`Listening Error: ${JSON.stringify(body)}`); | ||
this.longPollParams = null; | ||
return this.startPolling(); | ||
} | ||
return; | ||
} | ||
@@ -53,6 +43,8 @@ | ||
this.startPolling(); | ||
} else { | ||
throw err; | ||
return; | ||
} | ||
throw err; | ||
} | ||
}; |
@@ -19,2 +19,4 @@ class Request { | ||
set body(body) { | ||
this.response.status = this.response.statusCode = 200; | ||
this.response.end(body); | ||
@@ -21,0 +23,0 @@ } |
@@ -1,5 +0,5 @@ | ||
const callExecute = require('./callExecute'); | ||
const execute = require('./execute'); | ||
module.exports = { | ||
callExecute, | ||
execute, | ||
}; |
{ | ||
"name": "node-vk-bot-api", | ||
"version": "2.4.11", | ||
"version": "3.0.0", | ||
"description": "🤖 VK bot framework for Node.js, based on Bots Long Poll API and Callback API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -70,2 +70,3 @@ [![node-vk-bot-api](https://img.shields.io/npm/v/node-vk-bot-api.svg?style=flat-square)](https://www.npmjs.com/package/node-vk-bot-api/) | ||
* [constructor(settings)](#constructorsettings) | ||
# [.execute(method, settings)](#executemethod-settings) | ||
* [.use(middleware)](#usemiddleware) | ||
@@ -100,2 +101,12 @@ * [.command(triggers, ...middlewares)](#commandtriggers-middlewares) | ||
### .execute(method, settings) | ||
Execute request to the VK API. | ||
```js | ||
const response = await bot.execute('users.get', { | ||
user_ids: 1, | ||
}) | ||
``` | ||
### .use(middleware) | ||
@@ -102,0 +113,0 @@ |
39197
38
971
411
12