node-vk-bot-api
Advanced tools
Comparing version 3.3.4 to 3.3.5
@@ -16,4 +16,5 @@ { | ||
}], | ||
"no-multi-spaces": "off" | ||
"no-multi-spaces": "off", | ||
"no-param-reassign": "off" | ||
} | ||
} |
@@ -1,3 +0,1 @@ | ||
const PollingError = require('../errors/PollingError'); | ||
module.exports = async function () { | ||
@@ -12,12 +10,8 @@ if (!this.settings.group_id) { | ||
try { | ||
const { response } = await this.api('groups.getLongPollServer', { | ||
group_id: this.settings.group_id, | ||
access_token: this.settings.token, | ||
}); | ||
const { response } = await this.api('groups.getLongPollServer', { | ||
group_id: this.settings.group_id, | ||
access_token: this.settings.token, | ||
}); | ||
return response; | ||
} catch (err) { | ||
throw new PollingError(err); | ||
} | ||
return response; | ||
}; |
const axios = require('axios'); | ||
const Context = require('../context'); | ||
const PollingError = require('../errors/PollingError'); | ||
@@ -15,3 +14,4 @@ module.exports = async function (callback, ts) { | ||
if (typeof callback === 'function') { | ||
if (callback && !callback.called) { | ||
callback.called = true; | ||
callback(); | ||
@@ -30,3 +30,3 @@ } | ||
if (body.failed === 1) { | ||
return this.startPolling(null, body.ts); | ||
return this.startPolling(callback, body.ts); | ||
} | ||
@@ -36,3 +36,3 @@ | ||
this.longPollParams = null; | ||
this.startPolling(); | ||
this.startPolling(callback); | ||
@@ -42,17 +42,14 @@ return; | ||
this.ts = body.ts; | ||
this.startPolling(null, body.ts); | ||
this.ts = body.ts; | ||
this.startPolling(callback, body.ts); | ||
body.updates.forEach(update => this.next(new Context(update, this))); | ||
} catch (err) { | ||
if (err instanceof PollingError) { | ||
this.longPollParams = null; | ||
this.startPolling(); | ||
return; | ||
if (callback) { | ||
callback(err); | ||
} | ||
throw err; | ||
this.longPollParams = null; | ||
this.startPolling(callback); | ||
} | ||
}; |
{ | ||
"name": "node-vk-bot-api", | ||
"version": "3.3.4", | ||
"version": "3.3.5", | ||
"description": "🤖 VK bot framework for Node.js, based on Bots Long Poll API and Callback API", | ||
@@ -57,4 +57,4 @@ "main": "lib/index.js", | ||
"eslint-plugin-import": "^2.13.0", | ||
"mocha": "^5.2.0" | ||
"mocha": "^8.0.1" | ||
} | ||
} |
195
README.md
@@ -18,11 +18,11 @@ [![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/) | ||
```javascript | ||
const VkBot = require('node-vk-bot-api') | ||
const VkBot = require('node-vk-bot-api'); | ||
const bot = new VkBot(process.env.TOKEN) | ||
const bot = new VkBot(process.env.TOKEN); | ||
bot.command('/start', (ctx) => { | ||
ctx.reply('Hello!') | ||
}) | ||
ctx.reply('Hello!'); | ||
}); | ||
bot.startPolling() | ||
bot.startPolling(); | ||
``` | ||
@@ -33,21 +33,21 @@ | ||
```javascript | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
const VkBot = require('node-vk-bot-api') | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const VkBot = require('node-vk-bot-api'); | ||
const app = express() | ||
const app = express(); | ||
const bot = new VkBot({ | ||
token: process.env.TOKEN, | ||
confirmation: process.env.CONFIRMATION, | ||
}) | ||
}); | ||
bot.on((ctx) => { | ||
ctx.reply('Hello!') | ||
}) | ||
ctx.reply('Hello!'); | ||
}); | ||
app.use(bodyParser.json()) | ||
app.use(bodyParser.json()); | ||
app.post('/', bot.webhookCallback) | ||
app.post('/', bot.webhookCallback); | ||
app.listen(process.env.PORT) | ||
app.listen(process.env.PORT); | ||
``` | ||
@@ -81,2 +81,45 @@ | ||
## Error handling | ||
```js | ||
// bad | ||
bot.command('/start', (ctx) => { | ||
ctx.reply('Hello, world!'); | ||
}); | ||
// not bad | ||
bot.command('/start', async (ctx) => { | ||
try { | ||
await ctx.reply('Hello, world!'); | ||
} catch (e) { | ||
console.error(err); | ||
} | ||
}); | ||
// good | ||
bot.use(async (ctx, next) => { | ||
try { | ||
await next(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
bot.command('/start', async (ctx) => { | ||
await ctx.reply('Hello, world!'); | ||
}); | ||
``` | ||
```js | ||
// bad | ||
bot.startPolling(); | ||
// good | ||
bot.startPolling((err) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
}); | ||
``` | ||
## Methods | ||
@@ -102,3 +145,3 @@ | ||
// Simple usage | ||
const bot = new VkBot(process.env.TOKEN) | ||
const bot = new VkBot(process.env.TOKEN); | ||
@@ -115,3 +158,3 @@ // Advanced usage | ||
confirmation: process.env.CONFIRMATION, // confirmation string | ||
}) | ||
}); | ||
``` | ||
@@ -126,3 +169,3 @@ | ||
user_ids: 1, | ||
}) | ||
}); | ||
``` | ||
@@ -136,6 +179,6 @@ | ||
bot.use((ctx, next) => { | ||
ctx.message.timestamp = new Date().getTime() | ||
next() | ||
}) | ||
ctx.message.timestamp = new Date().getTime(); | ||
return next(); | ||
}); | ||
``` | ||
@@ -149,4 +192,4 @@ | ||
bot.command('start', (ctx) => { | ||
ctx.reply('Hello!') | ||
}) | ||
ctx.reply('Hello!'); | ||
}); | ||
``` | ||
@@ -160,4 +203,4 @@ | ||
bot.event('message_edit', (ctx) => { | ||
ctx.reply('Your message was editted') | ||
}) | ||
ctx.reply('Your message was editted'); | ||
}); | ||
``` | ||
@@ -171,4 +214,4 @@ | ||
bot.on((ctx) => { | ||
ctx.reply('No commands for you.') | ||
}) | ||
ctx.reply('No commands for you.'); | ||
}); | ||
``` | ||
@@ -182,6 +225,6 @@ | ||
// Simple usage | ||
bot.sendMessage(145003487, 'Hello!', 'photo1_1') | ||
bot.sendMessage(145003487, 'Hello!', 'photo1_1'); | ||
// Multiple recipients | ||
bot.sendMessage([145003487, 145003488], 'Hello!', 'photo1_1') | ||
bot.sendMessage([145003487, 145003488], 'Hello!', 'photo1_1'); | ||
@@ -193,3 +236,3 @@ // Advanced usage | ||
lng: 30.315868, | ||
}) | ||
}); | ||
``` | ||
@@ -202,5 +245,7 @@ | ||
```js | ||
bot.startPolling(() => { | ||
console.log('Bot started.') | ||
}) | ||
bot.startPolling((err) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
}); | ||
``` | ||
@@ -214,6 +259,6 @@ | ||
// express | ||
bot.webhookCallback(req, res, next) | ||
bot.webhookCallback(req, res, next); | ||
// koa | ||
bot.webhookCallback(ctx, next) | ||
bot.webhookCallback(ctx, next); | ||
``` | ||
@@ -226,3 +271,3 @@ | ||
```js | ||
bot.stop() | ||
bot.stop(); | ||
``` | ||
@@ -235,3 +280,3 @@ | ||
```js | ||
bot.start() | ||
bot.start(); | ||
``` | ||
@@ -258,4 +303,4 @@ | ||
bot.command('start', (ctx) => { | ||
ctx.reply('Hello!') | ||
}) | ||
ctx.reply('Hello!'); | ||
}); | ||
``` | ||
@@ -279,4 +324,4 @@ | ||
]) | ||
.oneTime() | ||
) | ||
.oneTime(), | ||
); | ||
``` | ||
@@ -315,3 +360,3 @@ | ||
]), | ||
) | ||
); | ||
``` | ||
@@ -356,3 +401,3 @@ | ||
'two', | ||
'three' | ||
'three', | ||
]); | ||
@@ -398,18 +443,18 @@ ``` | ||
```javascript | ||
const VkBot = require('node-vk-bot-api') | ||
const Session = require('node-vk-bot-api/lib/session') | ||
const VkBot = require('node-vk-bot-api'); | ||
const Session = require('node-vk-bot-api/lib/session'); | ||
const bot = new VkBot(process.env.TOKEN) | ||
const session = new Session() | ||
const bot = new VkBot(process.env.TOKEN); | ||
const session = new Session(); | ||
bot.use(session.middleware()) | ||
bot.use(session.middleware()); | ||
bot.on((ctx) => { | ||
ctx.session.counter = ctx.session.counter || 0 | ||
ctx.session.counter++ | ||
ctx.session.counter = ctx.session.counter || 0; | ||
ctx.session.counter++; | ||
ctx.reply(`You wrote ${ctx.session.counter} messages.`) | ||
}) | ||
ctx.reply(`You wrote ${ctx.session.counter} messages.`); | ||
}); | ||
bot.startPolling() | ||
bot.startPolling(); | ||
``` | ||
@@ -438,37 +483,37 @@ | ||
```javascript | ||
const VkBot = require('node-vk-bot-api') | ||
const Scene = require('node-vk-bot-api/lib/scene') | ||
const Session = require('node-vk-bot-api/lib/session') | ||
const Stage = require('node-vk-bot-api/lib/stage') | ||
const VkBot = require('node-vk-bot-api'); | ||
const Scene = require('node-vk-bot-api/lib/scene'); | ||
const Session = require('node-vk-bot-api/lib/session'); | ||
const Stage = require('node-vk-bot-api/lib/stage'); | ||
const bot = new VkBot(process.env.TOKEN) | ||
const bot = new VkBot(process.env.TOKEN); | ||
const scene = new Scene('meet', | ||
(ctx) => { | ||
ctx.scene.next() | ||
ctx.reply('How old are you?') | ||
ctx.scene.next(); | ||
ctx.reply('How old are you?'); | ||
}, | ||
(ctx) => { | ||
ctx.session.age = +ctx.message.text | ||
ctx.session.age = +ctx.message.text; | ||
ctx.scene.next() | ||
ctx.reply('What is your name?') | ||
ctx.scene.next(); | ||
ctx.reply('What is your name?'); | ||
}, | ||
(ctx) => { | ||
ctx.session.name = ctx.message.text | ||
ctx.session.name = ctx.message.text; | ||
ctx.scene.leave() | ||
ctx.reply(`Nice to meet you, ${ctx.session.name} (${ctx.session.age} years old)`) | ||
ctx.scene.leave(); | ||
ctx.reply(`Nice to meet you, ${ctx.session.name} (${ctx.session.age} years old)`); | ||
}, | ||
) | ||
const session = new Session() | ||
const stage = new Stage(scene) | ||
); | ||
const session = new Session(); | ||
const stage = new Stage(scene); | ||
bot.use(session.middleware()) | ||
bot.use(stage.middleware()) | ||
bot.use(session.middleware()); | ||
bot.use(stage.middleware()); | ||
bot.command('/meet', (ctx) => { | ||
ctx.scene.enter('meet') | ||
}) | ||
ctx.scene.enter('meet'); | ||
}); | ||
bot.startPolling() | ||
bot.startPolling(); | ||
``` | ||
@@ -475,0 +520,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
44982
1135
523
14