node-vk-bot-api
Advanced tools
Comparing version 2.4.0 to 2.4.1
{ | ||
"extends": "@bifot/eslint-config", | ||
"rules": { | ||
"semi": ["error", "never"] | ||
"no-shadow": "off", | ||
"no-underscore-dangle": "off", | ||
"import/no-unresolved": "off", | ||
"no-plusplus": "off", | ||
"func-names": "off", | ||
"consistent-return": "off", | ||
"prefer-destructuring": "off", | ||
"max-len": ["error", { "code": 120 }], | ||
"camelcase": ["error", { | ||
"properties": "never", | ||
"ignoreDestructuring": true | ||
}] | ||
} | ||
} |
@@ -1,5 +0,5 @@ | ||
const VkBot = require('../lib') | ||
const Markup = require('../lib/markup') | ||
const VkBot = require('../lib'); | ||
const Markup = require('../lib/markup'); | ||
const bot = new VkBot(process.env.TOKEN) | ||
const bot = new VkBot(process.env.TOKEN); | ||
@@ -12,4 +12,4 @@ bot.command('/sport', (ctx) => { | ||
]) | ||
.oneTime()) | ||
}) | ||
.oneTime()); | ||
}); | ||
@@ -26,5 +26,5 @@ bot.command('/mood', (ctx) => { | ||
], | ||
])) | ||
}) | ||
])); | ||
}); | ||
bot.startPolling() | ||
bot.startPolling(); |
@@ -1,34 +0,34 @@ | ||
const VkBot = require('../lib') | ||
const Session = require('../lib/session') | ||
const Stage = require('../lib/stage') | ||
const Scene = require('../lib/scene') | ||
const VkBot = require('../lib'); | ||
const Session = require('../lib/session'); | ||
const Stage = require('../lib/stage'); | ||
const Scene = require('../lib/scene'); | ||
const bot = new VkBot(process.env.TOKEN) | ||
const session = new Session() | ||
const bot = new VkBot(process.env.TOKEN); | ||
const session = new Session(); | ||
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)`) | ||
}) | ||
const stage = new Stage(scene) | ||
ctx.scene.leave(); | ||
ctx.reply(`Nice to meet you, ${ctx.session.name} (${ctx.session.age} years old)`); | ||
}); | ||
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(); |
@@ -1,16 +0,16 @@ | ||
const VkBot = require('../lib') | ||
const Session = require('../lib/session') | ||
const VkBot = require('../lib'); | ||
const Session = require('../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(); |
@@ -1,9 +0,9 @@ | ||
const VkBot = require('../lib') | ||
const VkBot = require('../lib'); | ||
const bot = new VkBot(process.env.TOKEN) | ||
const bot = new VkBot(process.env.TOKEN); | ||
bot.on((ctx) => { | ||
ctx.reply('Hello!') | ||
}) | ||
ctx.reply('Hello!'); | ||
}); | ||
bot.startPolling() | ||
bot.startPolling(); |
@@ -1,19 +0,19 @@ | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
const VkBot = require('../lib') | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const VkBot = require('../lib'); | ||
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); |
@@ -1,22 +0,22 @@ | ||
const Koa = require('koa') | ||
const Router = require('koa-router') | ||
const bodyParser = require('koa-bodyparser') | ||
const VkBot = require('../lib') | ||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
const bodyParser = require('koa-bodyparser'); | ||
const VkBot = require('../lib'); | ||
const app = new Koa() | ||
const router = new Router() | ||
const app = new Koa(); | ||
const router = new Router(); | ||
const bot = new VkBot({ | ||
token: process.env.TOKEN, | ||
confirmation: process.env.CONFIRMATION, | ||
}) | ||
}); | ||
bot.on((ctx) => { | ||
ctx.reply('Hello!') | ||
}) | ||
ctx.reply('Hello!'); | ||
}); | ||
router.post('/', bot.webhookCallback) | ||
router.post('/', bot.webhookCallback); | ||
app.use(bodyParser) | ||
app.use(router.routes()) | ||
app.use(bodyParser); | ||
app.use(router.routes()); | ||
app.listen(process.env.PORT) | ||
app.listen(process.env.PORT); |
@@ -1,3 +0,3 @@ | ||
const axios = require('axios') | ||
const { stringify } = require('querystring') | ||
const axios = require('axios'); | ||
const { stringify } = require('querystring'); | ||
@@ -9,12 +9,12 @@ module.exports = async function (method, settings = {}) { | ||
...settings, | ||
})) | ||
})); | ||
if (data.error) { | ||
throw JSON.stringify(data) | ||
throw JSON.stringify(data); | ||
} | ||
return data | ||
return data; | ||
} catch (err) { | ||
throw (typeof err === 'object' ? JSON.stringify(err) : err) | ||
throw (typeof err === 'object' ? JSON.stringify(err) : err); | ||
} | ||
} | ||
}; |
class Context { | ||
constructor({ type, object: update }, bot) { | ||
this.message = { ...update, type } | ||
this.bot = bot | ||
this.message = { ...update, type }; | ||
this.bot = bot; | ||
} | ||
reply(...args) { | ||
this.bot.sendMessage(this.message.peer_id || this.message.user_id, ...args) | ||
this.bot.sendMessage(this.message.peer_id || this.message.user_id, ...args); | ||
} | ||
} | ||
module.exports = Context | ||
module.exports = Context; |
@@ -1,4 +0,4 @@ | ||
const methods = require('./methods') | ||
const api = require('./api') | ||
const { callExecute } = require('./utils') | ||
const methods = require('./methods'); | ||
const api = require('./api'); | ||
const { callExecute } = require('./utils'); | ||
@@ -8,54 +8,54 @@ class VkBot { | ||
if (!settings) { | ||
throw new Error('You must pass token into settings') | ||
throw new Error('You must pass token into settings'); | ||
} else if (typeof settings === 'object' && !settings.token) { | ||
throw new Error('You must set token param in settings') | ||
throw new Error('You must set token param in settings'); | ||
} | ||
this.longPollParams = null | ||
this.middlewares = [] | ||
this.methods = [] | ||
this.longPollParams = null; | ||
this.middlewares = []; | ||
this.methods = []; | ||
this.settings = Object.assign({}, { | ||
polling_timeout: 25, | ||
execute_timeout: 50, | ||
}, typeof settings === 'object' ? settings : { token: settings }) | ||
}, typeof settings === 'object' ? settings : { token: settings }); | ||
Object.entries({ ...methods, api, callExecute }).forEach(([key, method]) => { | ||
this[key] = method.bind(this) | ||
}) | ||
this[key] = method.bind(this); | ||
}); | ||
setInterval(() => { | ||
this.callExecute(this.methods) | ||
this.methods = [] | ||
}, settings.execute_timeout || 50) | ||
this.callExecute(this.methods); | ||
this.methods = []; | ||
}, settings.execute_timeout || 50); | ||
} | ||
use(middleware) { | ||
this.use(middleware) | ||
this.use(middleware); | ||
} | ||
command(triggers, ...middlewares) { | ||
this.command(triggers, ...middlewares) | ||
this.command(triggers, ...middlewares); | ||
} | ||
event(triggers, ...middlewares) { | ||
this.command(triggers, ...middlewares) | ||
this.command(triggers, ...middlewares); | ||
} | ||
on(...middlewares) { | ||
this.command([], ...middlewares) | ||
this.command([], ...middlewares); | ||
} | ||
next(ctx, idx) { | ||
return this.next(ctx, idx) | ||
return this.next(ctx, idx); | ||
} | ||
sendMessage(userId, ...args) { | ||
this.sendMessage(userId, ...args) | ||
this.sendMessage(userId, ...args); | ||
} | ||
startPolling(callback) { | ||
return this.startPolling(callback) | ||
return this.startPolling(callback); | ||
} | ||
} | ||
module.exports = VkBot | ||
module.exports = VkBot; |
@@ -7,11 +7,11 @@ class Markup { | ||
: [buttons.map(label => Markup.button(label))], | ||
} | ||
}; | ||
return this | ||
return this; | ||
} | ||
static oneTime(value = true) { | ||
this.__keyboard.one_time = value | ||
this.__keyboard.one_time = value; | ||
return this | ||
return this; | ||
} | ||
@@ -21,3 +21,3 @@ | ||
if (typeof label === 'object') { | ||
return label | ||
return label; | ||
} | ||
@@ -32,10 +32,10 @@ | ||
color, | ||
} | ||
}; | ||
} | ||
static toJSON() { | ||
return JSON.stringify(this.__keyboard) | ||
return JSON.stringify(this.__keyboard); | ||
} | ||
} | ||
module.exports = Markup | ||
module.exports = Markup; |
@@ -1,9 +0,9 @@ | ||
const toArray = require('../utils/toArray') | ||
const toArray = require('../utils/toArray'); | ||
module.exports = function (_triggers, ...middlewares) { | ||
const triggers = toArray(_triggers) | ||
.map(item => (item instanceof RegExp ? item : item.toLowerCase())) | ||
.map(item => (item instanceof RegExp ? item : item.toLowerCase())); | ||
middlewares.forEach((fn) => { | ||
const idx = this.middlewares.length | ||
const idx = this.middlewares.length; | ||
@@ -13,6 +13,6 @@ this.middlewares.push({ | ||
triggers, | ||
}) | ||
}) | ||
}); | ||
}); | ||
return this | ||
} | ||
return this; | ||
}; |
@@ -8,5 +8,5 @@ module.exports = function (method, settings, callback = () => {}) { | ||
callback, | ||
}) | ||
}); | ||
return this | ||
} | ||
return this; | ||
}; |
@@ -5,5 +5,5 @@ module.exports = async function () { | ||
access_token: this.settings.token, | ||
}) | ||
}); | ||
this.settings.group_id = response[0].id | ||
this.settings.group_id = response[0].id; | ||
} | ||
@@ -15,11 +15,11 @@ | ||
}).catch((err) => { | ||
const { error } = JSON.parse(err) | ||
const { error } = JSON.parse(err); | ||
if (error.error_code === 15) { | ||
console.error(err) | ||
process.exit(1) | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
}) | ||
}); | ||
return response | ||
} | ||
return response; | ||
}; |
@@ -1,9 +0,9 @@ | ||
const sendMessage = require('./sendMessage') | ||
const startPolling = require('./startPolling') | ||
const getLongPollParams = require('./getLongPollParams') | ||
const use = require('./use') | ||
const command = require('./command') | ||
const next = require('./next') | ||
const execute = require('./execute') | ||
const webhookCallback = require('./webhookCallback') | ||
const sendMessage = require('./sendMessage'); | ||
const startPolling = require('./startPolling'); | ||
const getLongPollParams = require('./getLongPollParams'); | ||
const use = require('./use'); | ||
const command = require('./command'); | ||
const next = require('./next'); | ||
const execute = require('./execute'); | ||
const webhookCallback = require('./webhookCallback'); | ||
@@ -19,2 +19,2 @@ module.exports = { | ||
webhookCallback, | ||
} | ||
}; |
module.exports = function (ctx, idx = -1) { | ||
if (this.middlewares.length > idx + 1) { | ||
const { fn, triggers } = this.middlewares[idx + 1] | ||
const { fn, triggers } = this.middlewares[idx + 1]; | ||
const isTriggered = (triggers || []).some( | ||
(trigger) => { | ||
if (ctx.message.type === 'message_new' && trigger !== 'message_new') { | ||
const message = (ctx.message.text || ctx.message.body || '').toLowerCase() | ||
const message = (ctx.message.text || ctx.message.body || '').toLowerCase(); | ||
if (trigger instanceof RegExp) { | ||
return trigger.test(message) | ||
return trigger.test(message); | ||
} | ||
return message.startsWith(trigger) | ||
return message.startsWith(trigger); | ||
} | ||
return ctx.message.type === trigger | ||
return ctx.message.type === trigger; | ||
}, | ||
) | ||
); | ||
if (!triggers || (!triggers.length && ctx.message.type === 'message_new') || isTriggered) { | ||
return fn(ctx) | ||
return fn(ctx); | ||
} | ||
return this.next(ctx, idx + 1) | ||
return this.next(ctx, idx + 1); | ||
} | ||
} | ||
}; |
@@ -1,5 +0,5 @@ | ||
const toArray = require('../utils/toArray') | ||
const toArray = require('../utils/toArray'); | ||
module.exports = function (userId, ...args) { | ||
const [message, attachment, keyboard, sticker] = args | ||
const [message, attachment, keyboard, sticker] = args; | ||
@@ -21,3 +21,3 @@ this.execute( | ||
), | ||
) | ||
} | ||
); | ||
}; |
@@ -1,3 +0,3 @@ | ||
const axios = require('axios') | ||
const Context = require('../context') | ||
const axios = require('axios'); | ||
const Context = require('../context'); | ||
@@ -7,7 +7,7 @@ module.exports = async function (callback, ts) { | ||
if (!this.longPollParams) { | ||
this.longPollParams = await this.getLongPollParams() | ||
this.longPollParams = await this.getLongPollParams(); | ||
} | ||
if (typeof callback === 'function') { | ||
callback() | ||
callback(); | ||
} | ||
@@ -22,3 +22,3 @@ | ||
}, | ||
}) | ||
}); | ||
@@ -28,22 +28,24 @@ if (body.failed) { | ||
case 1: | ||
return this.startPolling(null, body.ts) | ||
return this.startPolling(null, body.ts); | ||
case 2: | ||
case 3: | ||
this.longPollParams = null | ||
return this.startPolling() | ||
this.longPollParams = null; | ||
return this.startPolling(); | ||
default: | ||
console.error(`Listening Error: ${JSON.stringify(body)}`) | ||
console.error(`Listening Error: ${JSON.stringify(body)}`); | ||
this.longPollParams = null | ||
return this.startPolling() | ||
this.longPollParams = null; | ||
return this.startPolling(); | ||
} | ||
} | ||
this.startPolling(null, body.ts) | ||
this.startPolling(null, body.ts); | ||
body.updates.forEach(update => this.next(new Context(update, this))) | ||
body.updates.forEach(update => this.next(new Context(update, this))); | ||
} catch (err) { | ||
this.longPollParams = null | ||
this.startPolling() | ||
this.longPollParams = null; | ||
this.startPolling(); | ||
} | ||
} | ||
}; |
module.exports = function (middleware) { | ||
const idx = this.middlewares.length | ||
const idx = this.middlewares.length; | ||
this.middlewares.push({ | ||
fn: ctx => middleware(ctx, () => this.next(ctx, idx)), | ||
}) | ||
}); | ||
return this | ||
} | ||
return this; | ||
}; |
@@ -1,8 +0,8 @@ | ||
const Request = require('../request') | ||
const Context = require('../context') | ||
const Request = require('../request'); | ||
const Context = require('../context'); | ||
const CONFIRMATION_TYPE = 'confirmation' | ||
const CONFIRMATION_TYPE = 'confirmation'; | ||
module.exports = function (...args) { | ||
const request = new Request(...args) | ||
const request = new Request(...args); | ||
@@ -14,14 +14,14 @@ if ( | ||
) { | ||
request.body = 'error' | ||
request.body = 'error'; | ||
return | ||
return; | ||
} | ||
if (request.body.type !== CONFIRMATION_TYPE) { | ||
request.body = 'ok' | ||
request.body = 'ok'; | ||
return this.next(new Context(request.body, this)) | ||
return this.next(new Context(request.body, this)); | ||
} | ||
request.body = this.settings.confirmation.toString() | ||
} | ||
request.body = this.settings.confirmation.toString(); | ||
}; |
class Request { | ||
constructor(...args) { | ||
if (args.length === 3) { | ||
this.request = args[0] // req [express] | ||
this.response = args[1] // res [express] | ||
this.request = args[0]; // req [express] | ||
this.response = args[1]; // res [express] | ||
} else { | ||
this.request = args[0].req // req [koa] | ||
this.response = args[0].res // res [koa] | ||
this.request = args[0].req; // req [koa] | ||
this.response = args[0].res; // res [koa] | ||
} | ||
@@ -13,10 +13,10 @@ } | ||
get body() { | ||
return this.request.body | ||
return this.request.body; | ||
} | ||
set body(body) { | ||
this.response.send(body) | ||
this.response.send(body); | ||
} | ||
} | ||
module.exports = Request | ||
module.exports = Request; |
@@ -1,7 +0,7 @@ | ||
const toArray = require('./utils/toArray') | ||
const toArray = require('./utils/toArray'); | ||
class Scene { | ||
constructor(name, ...middlewares) { | ||
this.name = name | ||
this.middlewares = middlewares.map(fn => ({ fn })) | ||
this.name = name; | ||
this.middlewares = middlewares.map(fn => ({ fn })); | ||
} | ||
@@ -11,3 +11,3 @@ | ||
const triggers = toArray(_triggers) | ||
.map(item => (item instanceof RegExp ? item : item.toLowerCase())) | ||
.map(item => (item instanceof RegExp ? item : item.toLowerCase())); | ||
@@ -19,6 +19,6 @@ this.middlewares.push( | ||
})), | ||
) | ||
); | ||
} | ||
} | ||
module.exports = Scene | ||
module.exports = Scene; |
@@ -7,3 +7,3 @@ class Session { | ||
getSessionKey: ctx => `${ctx.message.from_id}:${ctx.message.from_id}`, | ||
}, settings) | ||
}, settings); | ||
} | ||
@@ -13,4 +13,4 @@ | ||
return (ctx, next) => { | ||
const key = this.getSessionKey(ctx) | ||
let session = this.store.get(key) || {} | ||
const key = this.getSessionKey(ctx); | ||
let session = this.store.get(key) || {}; | ||
@@ -20,13 +20,13 @@ Object.defineProperty(ctx, this.key, { | ||
set: (value) => { | ||
session = value | ||
session = value; | ||
}, | ||
}) | ||
}); | ||
this.store.set(key, session) | ||
this.store.set(key, session); | ||
next() | ||
} | ||
next(); | ||
}; | ||
} | ||
} | ||
module.exports = Session | ||
module.exports = Session; |
class Stage { | ||
constructor(...scenes) { | ||
this.scenes = {} | ||
this.scenes = {}; | ||
scenes.forEach(({ name, middlewares }) => { | ||
this.scenes[name] = middlewares | ||
}) | ||
this.scenes[name] = middlewares; | ||
}); | ||
} | ||
enter(ctx) { | ||
const { current, step } = ctx.session.__scene | ||
const text = (ctx.message.text || ctx.message.body).toLowerCase() | ||
const { current, step } = ctx.session.__scene; | ||
const text = (ctx.message.text || ctx.message.body).toLowerCase(); | ||
const command = this.scenes[current].find(({ triggers }) => { | ||
if (!triggers) { | ||
return false | ||
return false; | ||
} | ||
return triggers.some(trigger => (trigger instanceof RegExp ? trigger.test(text) : text.startsWith(trigger))) | ||
}) | ||
const simple = this.scenes[current][step] | ||
return triggers.some(trigger => (trigger instanceof RegExp ? trigger.test(text) : text.startsWith(trigger))); | ||
}); | ||
const simple = this.scenes[current][step]; | ||
if (!command && !simple) { | ||
return console.error(`Middleware not found for ${current} scene at ${step} step`) | ||
return console.error(`Middleware not found for ${current} scene at ${step} step`); | ||
} | ||
if (command) { | ||
return command.fn(ctx) | ||
return command.fn(ctx); | ||
} | ||
simple.fn(ctx) | ||
simple.fn(ctx); | ||
} | ||
@@ -40,26 +40,26 @@ | ||
step, | ||
} | ||
}; | ||
this.enter(ctx) | ||
this.enter(ctx); | ||
}, | ||
leave: () => { | ||
ctx.session.__scene = null | ||
ctx.session.__scene = null; | ||
}, | ||
next: () => { | ||
++ctx.session.__scene.step | ||
++ctx.session.__scene.step; | ||
}, | ||
selectStep: (index) => { | ||
ctx.session.__scene.step = index | ||
ctx.session.__scene.step = index; | ||
}, | ||
} | ||
}; | ||
if (ctx.session.__scene && ctx.message.type === 'message_new') { | ||
return this.enter(ctx) | ||
return this.enter(ctx); | ||
} | ||
next() | ||
} | ||
next(); | ||
}; | ||
} | ||
} | ||
module.exports = Stage | ||
module.exports = Stage; |
@@ -1,6 +0,6 @@ | ||
const api = require('../api') | ||
const api = require('../api'); | ||
module.exports = function (methods) { | ||
for (let i = 0, j = Math.ceil(methods.length / 25); i < j; i++) { | ||
const slicedMethods = methods.slice(i * 25, i * 25 + 25) | ||
const slicedMethods = methods.slice(i * 25, i * 25 + 25); | ||
@@ -12,7 +12,7 @@ api('execute', { | ||
.then(({ response, execute_errors = [] }) => { | ||
execute_errors.forEach(err => console.error(`Execute Error: ${JSON.stringify(err)}`)) | ||
response.forEach((body, i) => slicedMethods[i].callback(body)) | ||
execute_errors.forEach(err => console.error(`Execute Error: ${JSON.stringify(err)}`)); | ||
response.forEach((body, i) => slicedMethods[i].callback(body)); | ||
}) | ||
.catch(err => console.error(err)) | ||
.catch(err => console.error(err)); | ||
} | ||
} | ||
}; |
@@ -1,5 +0,5 @@ | ||
const callExecute = require('./callExecute') | ||
const callExecute = require('./callExecute'); | ||
module.exports = { | ||
callExecute, | ||
} | ||
}; |
@@ -1,1 +0,1 @@ | ||
module.exports = value => (Array.isArray(value) ? value : [value]) | ||
module.exports = value => (Array.isArray(value) ? value : [value]); |
{ | ||
"name": "node-vk-bot-api", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"description": "🤖 VK bot framework for Node.js, based on Bots Long Poll API and Callback API", | ||
@@ -54,3 +54,3 @@ "main": "lib/index.js", | ||
"chai": "^4.2.0", | ||
"eslint": "^4.19.1", | ||
"eslint": "^5.9.0", | ||
"eslint-config-airbnb-base": "^13.0.0", | ||
@@ -57,0 +57,0 @@ "eslint-plugin-import": "^2.13.0", |
const sendRequestViaKoa = (ctx, middleware) => { | ||
middleware(ctx, () => {}) | ||
middleware(ctx, () => {}); | ||
return ctx | ||
} | ||
return ctx; | ||
}; | ||
const sendRequestViaExpress = (ctx, middleware) => { | ||
middleware(ctx.req, ctx.res, () => {}) | ||
middleware(ctx.req, ctx.res, () => {}); | ||
return ctx | ||
} | ||
return ctx; | ||
}; | ||
@@ -18,10 +18,10 @@ module.exports.sendRequest = (type, body, middleware) => { | ||
send: (body) => { | ||
ctx.res.body = body | ||
ctx.res.body = body; | ||
}, | ||
}, | ||
} | ||
}; | ||
return type === 'koa' | ||
? sendRequestViaKoa(ctx, middleware) | ||
: sendRequestViaExpress(ctx, middleware) | ||
} | ||
: sendRequestViaExpress(ctx, middleware); | ||
}; |
@@ -1,12 +0,12 @@ | ||
const { expect } = require('chai') | ||
const VkBot = require('../lib') | ||
const Scene = require('../lib/scene') | ||
const Stage = require('../lib/stage') | ||
const Session = require('../lib/session') | ||
const { expect } = require('chai'); | ||
const VkBot = require('../lib'); | ||
const Scene = require('../lib/scene'); | ||
const Stage = require('../lib/stage'); | ||
const Session = require('../lib/session'); | ||
const bot = new VkBot('TOKEN') | ||
const bot = new VkBot('TOKEN'); | ||
describe('scenes', () => { | ||
describe('unit', () => { | ||
let scene | ||
let scene; | ||
@@ -16,41 +16,41 @@ it('should create scene', () => { | ||
(ctx) => { | ||
ctx.scene.next() | ||
ctx.reply('How are you?') | ||
ctx.scene.next(); | ||
ctx.reply('How are you?'); | ||
}, | ||
(ctx) => { | ||
ctx.scene.leave() | ||
ctx.reply('OK, bye') | ||
}) | ||
ctx.scene.leave(); | ||
ctx.reply('OK, bye'); | ||
}); | ||
expect(scene).to.be.an('object').to.have.all.keys(['name', 'middlewares']) | ||
expect(scene.name).to.be.a('string').to.be.equal('simple') | ||
expect(scene.middlewares).to.be.an('array').to.have.lengthOf(2) | ||
expect(scene).to.be.an('object').to.have.all.keys(['name', 'middlewares']); | ||
expect(scene.name).to.be.a('string').to.be.equal('simple'); | ||
expect(scene.middlewares).to.be.an('array').to.have.lengthOf(2); | ||
scene.middlewares.forEach((middleware) => { | ||
expect(middleware).to.be.an('object').to.have.all.keys(['fn']) | ||
expect(middleware.fn).to.be.a('function') | ||
}) | ||
expect(middleware).to.be.an('object').to.have.all.keys(['fn']); | ||
expect(middleware.fn).to.be.a('function'); | ||
}); | ||
expect(scene.command).to.be.a('function') | ||
}) | ||
expect(scene.command).to.be.a('function'); | ||
}); | ||
it('should create stage', () => { | ||
const stage = new Stage(scene) | ||
const stage = new Stage(scene); | ||
expect(stage).to.be.an('object').to.have.all.keys(['scenes']) | ||
expect(Object.keys(stage.scenes)).to.be.an('array').to.have.lengthOf(1).to.include('simple') | ||
expect(stage.enter).to.be.a('function') | ||
expect(stage.middleware).to.be.a('function') | ||
}) | ||
expect(stage).to.be.an('object').to.have.all.keys(['scenes']); | ||
expect(Object.keys(stage.scenes)).to.be.an('array').to.have.lengthOf(1).to.include('simple'); | ||
expect(stage.enter).to.be.a('function'); | ||
expect(stage.middleware).to.be.a('function'); | ||
}); | ||
it('should create session', () => { | ||
const session = new Session() | ||
const session = new Session(); | ||
expect(session).to.be.an('object').to.have.all.keys(['store', 'key', 'getSessionKey']) | ||
expect(session.store).to.be.a('map') | ||
expect(session.key).to.be.a('string').to.be.equal('session') | ||
expect(session.getSessionKey).to.be.a('function') | ||
expect(session.getSessionKey({ message: { from_id: 'uid' } })).to.be.a('string').to.be.equal('uid:uid') | ||
expect(session.middleware).to.be.a('function') | ||
}) | ||
expect(session).to.be.an('object').to.have.all.keys(['store', 'key', 'getSessionKey']); | ||
expect(session.store).to.be.a('map'); | ||
expect(session.key).to.be.a('string').to.be.equal('session'); | ||
expect(session.getSessionKey).to.be.a('function'); | ||
expect(session.getSessionKey({ message: { from_id: 'uid' } })).to.be.a('string').to.be.equal('uid:uid'); | ||
expect(session.middleware).to.be.a('function'); | ||
}); | ||
@@ -61,46 +61,46 @@ it('should create session with custom settings', () => { | ||
getSessionKey: ctx => `secure:${ctx.message.text}`, | ||
}) | ||
}); | ||
expect(session).to.be.an('object').to.have.all.keys(['store', 'key', 'getSessionKey']) | ||
expect(session.store).to.be.a('map') | ||
expect(session.key).to.be.a('string').to.be.equal('my_super_session') | ||
expect(session.getSessionKey).to.be.a('function') | ||
expect(session.getSessionKey({ message: { text: 'text' } })).to.be.a('string').to.be.equal('secure:text') | ||
expect(session.middleware).to.be.a('function') | ||
}) | ||
}) | ||
expect(session).to.be.an('object').to.have.all.keys(['store', 'key', 'getSessionKey']); | ||
expect(session.store).to.be.a('map'); | ||
expect(session.key).to.be.a('string').to.be.equal('my_super_session'); | ||
expect(session.getSessionKey).to.be.a('function'); | ||
expect(session.getSessionKey({ message: { text: 'text' } })).to.be.a('string').to.be.equal('secure:text'); | ||
expect(session.middleware).to.be.a('function'); | ||
}); | ||
}); | ||
describe('e2e', () => { | ||
const session = new Session() | ||
const session = new Session(); | ||
const testSceneContext = (ctx, current, step) => { | ||
expect(ctx).to.be.an('object') | ||
expect(ctx.scene).to.be.an('object').to.have.all.keys(['enter', 'leave', 'next', 'selectStep']) | ||
expect(ctx.scene.enter).to.be.a('function') | ||
expect(ctx.scene.leave).to.be.a('function') | ||
expect(ctx.scene.next).to.be.a('function') | ||
expect(ctx.scene.selectStep).to.be.a('function') | ||
expect(ctx).to.be.an('object'); | ||
expect(ctx.scene).to.be.an('object').to.have.all.keys(['enter', 'leave', 'next', 'selectStep']); | ||
expect(ctx.scene.enter).to.be.a('function'); | ||
expect(ctx.scene.leave).to.be.a('function'); | ||
expect(ctx.scene.next).to.be.a('function'); | ||
expect(ctx.scene.selectStep).to.be.a('function'); | ||
if (current === null) { | ||
expect(ctx.session.__scene).to.be.equal(null) | ||
expect(ctx.session.__scene).to.be.equal(null); | ||
} | ||
if (current !== undefined && current !== null && step !== undefined) { | ||
expect(ctx.session.__scene).to.be.an('object').to.have.all.keys(['current', 'step']) | ||
expect(ctx.session.__scene.current).to.be.a('string').to.be.equal(current) | ||
expect(ctx.session.__scene.step).to.be.a('number').to.be.equal(step) | ||
expect(ctx.session.__scene).to.be.an('object').to.have.all.keys(['current', 'step']); | ||
expect(ctx.session.__scene.current).to.be.a('string').to.be.equal(current); | ||
expect(ctx.session.__scene.step).to.be.a('number').to.be.equal(step); | ||
} | ||
} | ||
}; | ||
bot.use(session.middleware()) | ||
bot.use(session.middleware()); | ||
it('should set value in session', (done) => { | ||
bot.on((ctx) => { | ||
ctx.session.foo = 'bar' | ||
ctx.session.foo = 'bar'; | ||
expect(ctx.session).to.be.an('object') | ||
expect(ctx.session.foo).to.be.a('string').to.be.equal('bar') | ||
expect(ctx.session).to.be.an('object'); | ||
expect(ctx.session.foo).to.be.a('string').to.be.equal('bar'); | ||
done() | ||
}) | ||
done(); | ||
}); | ||
@@ -112,13 +112,13 @@ bot.next({ | ||
}, | ||
}) | ||
}) | ||
}); | ||
}); | ||
it('should run scene', (done) => { | ||
bot.middlewares = [] | ||
bot.middlewares = []; | ||
const scene = new Scene('test', | ||
(ctx) => { | ||
testSceneContext(ctx, 'test', 0) | ||
testSceneContext(ctx, 'test', 0); | ||
ctx.scene.next() | ||
ctx.scene.next(); | ||
@@ -130,23 +130,23 @@ bot.next({ | ||
}, | ||
}) | ||
}); | ||
}, | ||
(ctx) => { | ||
testSceneContext(ctx, 'test', 1) | ||
testSceneContext(ctx, 'test', 1); | ||
ctx.scene.leave() | ||
ctx.scene.leave(); | ||
testSceneContext(ctx, null) | ||
testSceneContext(ctx, null); | ||
done() | ||
}) | ||
const stage = new Stage(scene) | ||
done(); | ||
}); | ||
const stage = new Stage(scene); | ||
bot.use(session.middleware()) | ||
bot.use(stage.middleware()) | ||
bot.use(session.middleware()); | ||
bot.use(stage.middleware()); | ||
bot.on((ctx) => { | ||
testSceneContext(ctx) | ||
testSceneContext(ctx); | ||
ctx.scene.enter('test') | ||
}) | ||
ctx.scene.enter('test'); | ||
}); | ||
@@ -158,13 +158,13 @@ bot.next({ | ||
}, | ||
}) | ||
}) | ||
}); | ||
}); | ||
it('should trigger command in scene', (done) => { | ||
bot.middlewares = [] | ||
bot.middlewares = []; | ||
const scene = new Scene('test', | ||
(ctx) => { | ||
testSceneContext(ctx, 'test', 0) | ||
testSceneContext(ctx, 'test', 0); | ||
ctx.scene.next() | ||
ctx.scene.next(); | ||
@@ -176,29 +176,29 @@ bot.next({ | ||
}, | ||
}) | ||
}); | ||
}, | ||
(ctx) => { | ||
ctx.scene.leave() | ||
}) | ||
ctx.scene.leave(); | ||
}); | ||
scene.command('/cancel', (ctx) => { | ||
testSceneContext(ctx, 'test', 1) | ||
testSceneContext(ctx, 'test', 1); | ||
ctx.scene.leave() | ||
ctx.scene.leave(); | ||
testSceneContext(ctx, null) | ||
testSceneContext(ctx, null); | ||
done() | ||
}) | ||
done(); | ||
}); | ||
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.on((ctx) => { | ||
testSceneContext(ctx) | ||
testSceneContext(ctx); | ||
ctx.scene.enter('test') | ||
}) | ||
ctx.scene.enter('test'); | ||
}); | ||
@@ -210,5 +210,5 @@ bot.next({ | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,4 +0,4 @@ | ||
const { expect } = require('chai') | ||
const VkBot = require('../lib') | ||
const { sendRequest } = require('./config') | ||
const { expect } = require('chai'); | ||
const VkBot = require('../lib'); | ||
const { sendRequest } = require('./config'); | ||
@@ -8,3 +8,3 @@ const bot = new VkBot({ | ||
confirmation: 'CONFIRMATION', | ||
}) | ||
}); | ||
@@ -14,5 +14,5 @@ describe('webhooks', () => { | ||
it('should return webhook callback', () => { | ||
expect(bot.webhookCallback).to.be.a('function') | ||
}) | ||
}) | ||
expect(bot.webhookCallback).to.be.a('function'); | ||
}); | ||
}); | ||
@@ -27,6 +27,6 @@ describe('e2e', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.equal(bot.settings.confirmation) | ||
}) | ||
expect(res.body).to.be.equal(bot.settings.confirmation); | ||
}); | ||
@@ -40,6 +40,6 @@ it('should send confirmation for koa request', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.equal(bot.settings.confirmation) | ||
}) | ||
expect(res.body).to.be.equal(bot.settings.confirmation); | ||
}); | ||
@@ -53,6 +53,6 @@ it('should send ok for express request', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.equal('ok') | ||
}) | ||
expect(res.body).to.be.equal('ok'); | ||
}); | ||
@@ -66,10 +66,10 @@ it('should send ok for koa request', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.equal('ok') | ||
}) | ||
expect(res.body).to.be.equal('ok'); | ||
}); | ||
it('should send not ok for express request with invalid secret key', () => { | ||
// setup bot via secret key | ||
bot.settings.secret = 'SECRET' | ||
bot.settings.secret = 'SECRET'; | ||
@@ -82,6 +82,6 @@ const { res } = sendRequest( | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.not.equal('ok') | ||
}) | ||
expect(res.body).to.be.not.equal('ok'); | ||
}); | ||
@@ -95,6 +95,6 @@ it('should send not ok for koa request with invalid secret key', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.not.equal('ok') | ||
}) | ||
expect(res.body).to.be.not.equal('ok'); | ||
}); | ||
@@ -109,6 +109,6 @@ it('should send ok for express request with valid secret key', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.equal('ok') | ||
}) | ||
expect(res.body).to.be.equal('ok'); | ||
}); | ||
@@ -123,7 +123,7 @@ it('should send ok for koa request with valid secret key', () => { | ||
bot.webhookCallback, | ||
) | ||
); | ||
expect(res.body).to.be.equal('ok') | ||
}) | ||
}) | ||
}) | ||
expect(res.body).to.be.equal('ok'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
32574
804
11