node-vk-bot-api
🤖 VK bot framework for Node.js, based on Bots Long Poll API and Callback API.
Install
$ npm i node-vk-bot-api -S
Usage
const VkBot = const bot = processenvTOKEN bot bot
Webhooks
const express = const bodyParser = const VkBot = const app = const bot = token: processenvTOKEN confirmation: processenvCONFIRMATION bot app app app
Examples
There's a few simple examples.
Community support
Any questions you can ask in the telegram chat. [russian/english]
Tests
$ npm test
Methods
- constructor(settings)
- .use(middleware)
- .command(triggers, ...middlewares)
- .event(triggers, ...middlewares)
- .on(...middlewares)
- .sendMessage(userId, message, attachment, keyboard, sticker)
- .startPolling([callback])
- .webhookCallback(...args)
constructor(settings)
Create bot.
// Simple usageconst bot = processenvTOKEN // Advanced usageconst bot = token: processenvTOKEN group_id: processenvGROUP_ID execute_timeout: processenvEXECUTE_TIMEOUT // in ms (50 by default) polling_timeout: processenvPOLLING_TIMEOUT // in secs (25 by default) // webhooks options only secret: processenvSECRET // secret key (optional) confirmation: processenvCONFIRMATION // confirmation string
.use(middleware)
Add simple middleware.
bot
.command(triggers, ...middlewares)
Add middlewares with triggers for message_new
event.
bot
.event(triggers, ...middlewares)
Add middlewares with triggers for selected events.
botevent'message_edit' { ctx}
.on(...middlewares)
Add reserved middlewares without triggers.
bot
.sendMessage(userId, message, attachment, keyboard, sticker)
Send message to user.
// Simple usagebot // Advanced usagebot
.startPolling([callback])
Start polling with optional callback.
bot
.webhookCallback(...args)
Get webhook callback.
// expressbot // koabot
Context Methods
.reply(message, attachment, markup, sticker)
Helper method for reply to the current user.
bot
Markup
Keyboards
Markup.keyboard(buttons, options)
: Create keyboardMarkup.button(label, color, payload)
: Create custom buttonMarkup.oneTime()
: Set oneTime to keyboard
Simple usage
ctx
Advanced usage
ctx
.keyboard(buttons, options)
Create keyboard with optional settings.
/* Each string has maximum 2 columns. | one | two | | three | four | | five | six | */ Markup;
/* By default, columns count for each string is 4. | one | two | three | */ Markup;
.button(label, color, payload)
Create custom button.
Markup;
.oneTime()
Helper method for create one time keyboard.
Markup ;
Sessions
Store anything for current user in local (or redis) memory.
Usage
const VkBot = const Session = const bot = processenvTOKENconst session = bot bot bot
API
Options
key
: Context property name (default:session
)getSessionKey
: Getter for session key
getSessionKey(ctx)
Default const getSessionKey = { const userId = ctxmessagefrom_id || ctxmessageuser_id; return `:`;};
Stage
Scene manager.
const VkBot = const Scene = const Session = const Stage = const bot = processenvTOKENconst scene = 'meet' { ctxscenenext ctx } { ctxsessionage = +ctxmessagetext ctxscenenext ctx } { ctxsessionname = ctxmessagetext ctxscene ctx }const session = const stage = scene botbot bot bot
API
Stage
constructor(...scenes)
: Register scenes
Scene
constructor(name, ...middlewares)
: Create scene.command(triggers, ...middlewares)
: Create commands for scene
Context
ctxscene // Enter in scenectxscene // Leave from scenectxscenenext // Go to the next step in scenectxscenestep // Getter for step in scenectxscenestep= // Setter for step in scene
License
MIT.