Socket
Socket
Sign inDemoInstall

node-vk-bot-api

Package Overview
Dependencies
1
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-vk-bot-api


Version published
Maintainers
1
Created

Readme

Source

node-vk-bot-api node-vk-bot-api

node-vk-bot-api

🤖 VK bot framework for Node.js, based on Bots Long Poll API.

Install

$ npm i node-vk-bot-api@2 -S # bots longpoll api
$ npm i node-vk-bot-api@1 -S # user longpoll api

Usage

const VkBot = require('node-vk-bot-api')

const bot = new VkBot({
  token: process.env.TOKEN,
  group_id: process.env.GROUP_ID
})

bot.command('/start', (ctx) => {
  ctx.reply('Hello!')
})

bot.startPolling()

Examples

There's a few simple examples.

Methods

constructor(settings)

Create bot.

const bot = new VkBot({
  token: process.env.TOKEN,
  group_id: process.env.GROUP_ID
})

.use(middleware)

Add simple middleware.

bot.use((ctx, next) => {
  ctx.message.timestamp = new Date().getTime()
  
  next()
})

.command(triggers, ...middlewares)

Add middlewares with triggers for message_new event.

bot.command('start', (ctx) => {
  ctx.reply('Hello!')
})

.event(triggers, ...middlewares)

Add middlewares with triggers for selected events.

bot.event('message_edit', (ctx) => {
  ctx.reply('Your message was editted')
})

.on(...middlewares)

Add reserved middlewares without triggers.

bot.on((ctx) => {
  ctx.reply('No commands for you.')
})

.sendMessage(userId, message, attachment, keyboard, sticker)

Send message to user.

// Simple usage
bot.sendMessage(145003487, 'Hello!', 'photo1_1')

// Advanced usage
bot.sendMessage(145003487, {
  message: 'Hello!',
  lat: 59.939095,
  lng: 30.315868
})

.startPolling([timeout])

Start polling with given timeout (25 by default).

bot.startPolling()

Context Methods

.reply(message, attachment, keyboard, sticker)

Helper method for reply to the current user.

bot.command('start', (ctx) => {
  ctx.reply('Hello!')
})

Markup

Add keyboard in message.

const VkBot = require('node-vk-bot-api')
const Markup = require('node-vk-bot-api/lib/markup')

const bot = new VkBot({
  token: process.env.TOKEN,
  group_id: process.env.GROUP_ID,
})

bot.command('/sport', (ctx) => {
  ctx.reply('Select your sport', null, Markup
    .keyboard([
      'Football',
      'Basketball',
    ])
    .oneTime())
})

bot.command('/mood', (ctx) => {
  ctx.reply('How are you doing?', null, Markup
    .keyboard([
      [
        Markup.button('Normally', 'primary'),
      ],
      [
        Markup.button('Fine', 'positive'),
        Markup.button('Bad', 'negative'),
      ],
    ]))
})

Sessions

Store anything for current user in local (or redis) memory.

Usage

const VkBot = require('node-vk-bot-api')
const Session = require('node-vk-bot-api/lib/session')

const bot = new VkBot({
  token: process.env.TOKEN,
  group_id: process.env.GROUP_ID,
})
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()

API

Options
  • key: Context property name (default: session)
  • getSessionKey: Getter for session key
Default getSessionKey(ctx)
const getSessionKey = (ctx) => {
 return `${ctx.message.from_id}:${ctx.message.from_id}` 
}

Stage

Scene manager.

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({
  token: process.env.TOKEN,
  group_id: process.env.GROUP_ID,
})
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()

API

Stage
  • constructor(...scenes): Register scenes
  • enter(name): Enter in scene
  • leave(): Leave from scene
  • next(): Go to the next step in scene
Scene
  • constructor(name, ...middlewares): Create scene

License

MIT.

Keywords

FAQs

Last updated on 03 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc