🤖 VK bot framework for Node.js, based on Bots Long Poll API and Callback API
npm install node-vk-bot-api-new-keybord
!node-vk-bot-api
!node-vk-bot-api
🤖 VK bot framework for Node.js, based on Bots Long Poll API and Callback API.
``sh`
$ npm i node-vk-bot-api -S
`javascript
const VkBot = require('node-vk-bot-api')
const bot = new VkBot(process.env.TOKEN)
bot.command('/start', (ctx) => {
ctx.reply('Hello!')
})
bot.startPolling()
`
`javascript
const express = require('express')
const bodyParser = require('body-parser')
const VkBot = require('node-vk-bot-api')
const app = express()
const bot = new VkBot({
token: process.env.TOKEN,
confirmation: process.env.CONFIRMATION,
})
bot.on((ctx) => {
ctx.reply('Hello!')
})
app.use(bodyParser.json())
app.post('/', bot.webhookCallback)
app.listen(process.env.PORT)
`
There's a few simple examples.
Any questions you can ask in the telegram chat. [russian/english]
`sh`
$ npm test
* constructor(settings)
* .use(middleware)
* .command(triggers, ...middlewares)
* .event(triggers, ...middlewares)
* .on(...middlewares)
* .sendMessage(userId, message, attachment, keyboard, sticker)
* [.startPolling([callback])](#startpollingcallback)
* .webhookCallback(...args)
Create bot.
`javascript
// Simple usage
const bot = new VkBot(process.env.TOKEN)
// Advanced usage
const bot = new VkBot({
token: process.env.TOKEN,
group_id: process.env.GROUP_ID,
execute_timeout: process.env.EXECUTE_TIMEOUT, // in ms (50 by default)
polling_timeout: process.env.POLLING_TIMEOUT, // in secs (25 by default)
// webhooks options only
secret: process.env.SECRET, // secret key (optional)
confirmation: process.env.CONFIRMATION, // confirmation string
})
`
Add simple middleware.
`javascript`
bot.use((ctx, next) => {
ctx.message.timestamp = new Date().getTime()
next()
})
Add middlewares with triggers for message_new event.
`javascript`
bot.command('start', (ctx) => {
ctx.reply('Hello!')
})
Add middlewares with triggers for selected events.
`javascript`
bot.event('message_edit', (ctx) => {
ctx.reply('Your message was editted')
})
Add reserved middlewares without triggers.
`javascript`
bot.on((ctx) => {
ctx.reply('No commands for you.')
})
Send message to user.
`javascript
// Simple usage
bot.sendMessage(145003487, 'Hello!', 'photo1_1')
// Advanced usage
bot.sendMessage(145003487, {
message: 'Hello!',
lat: 59.939095,
lng: 30.315868,
})
`
Start polling with optional callback.
`js`
bot.startPolling(() => {
console.log('Bot started.')
})
Get webhook callback.
`js
// express
bot.webhookCallback(req, res, next)
// koa
bot.webhookCallback(ctx, next)
`
* .reply(message, attachment, markup, sticker)
Helper method for reply to the current user.
`javascript`
bot.command('start', (ctx) => {
ctx.reply('Hello!')
})
* Markup.keyboard(buttons, options): Create keyboardMarkup.button(label, color, payload)
* : Create custom buttonMarkup.oneTime()
* : Set oneTime to keyboard
#### Simple usage
`js`
ctx.reply('Select your sport', null, Markup
.keyboard([
'Football',
'Basketball',
])
.oneTime()
)
#### Advanced usage
`js`
ctx.reply('How are you doing?', null, Markup
.keyboard([
[
Markup.button('Normally', 'primary'),
],
[
Markup.button('Fine', 'positive'),
Markup.button('Bad', 'negative'),
],
])
)
Create keyboard with optional settings.
`js
/*
Each string has maximum 2 columns.
| one | two |
| three | four |
| five | six |
*/
Markup.keyboard([
'one',
'two',
'three',
'four',
'five',
'six',
], { columns: 2 });
`
`js
/*
By default, columns count for each string is 4.
| one | two | three |
*/
Markup.keyboard([
'one',
'two',
'three'
]);
`
Create custom button.
`js`
Markup.button('Start', 'positive', {
foo: 'bar',
});
Helper method for create one time keyboard.
`js`
Markup
.keyboard(['Start', 'Help'])
.oneTime();
Store anything for current user in local (or redis) memory.
`javascript
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()
bot.use(session.middleware())
bot.on((ctx) => {
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter++
ctx.reply(You wrote ${ctx.session.counter} messages.)
})
bot.startPolling()
`
#### Options
* key: Context property name (default: session)getSessionKey
* : Getter for session key
##### Default getSessionKey(ctx)
`js
const getSessionKey = (ctx) => {
const userId = ctx.message.from_id || ctx.message.user_id;
return ${userId}:${userId};``
};
Scene manager.
`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 bot = new VkBot(process.env.TOKEN)
const scene = new Scene('meet',
(ctx) => {
ctx.scene.next()
ctx.reply('How old are you?')
},
(ctx) => {
ctx.session.age = +ctx.message.text
ctx.scene.next()
ctx.reply('What is your name?')
},
(ctx) => {
ctx.session.name = ctx.message.text
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)
bot.use(session.middleware())
bot.use(stage.middleware())
bot.command('/meet', (ctx) => {
ctx.scene.enter('meet')
})
bot.startPolling()
`
#### Stage
* constructor(...scenes): Register scenes
#### Scene
* constructor(name, ...middlewares): Create scene.command(triggers, ...middlewares)
* : Create commands for scene
#### Context
`js``
ctx.scene.enter(name, [step]) // Enter in scene
ctx.scene.leave() // Leave from scene
ctx.scene.next() // Go to the next step in scene
ctx.scene.step // Getter for step in scene
ctx.scene.step= // Setter for step in scene
MIT.