Socket
Socket
Sign inDemoInstall

node-vk-bot

Package Overview
Dependencies
51
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-vk-bot

Create and control VK bots easily.


Version published
Maintainers
1
Created

Readme

Source

Build Status

README на русском

VK BOTS

  • Create and control VK bots easily.
  • Uses LongPoll or Callback API to get new messages
npm install --save node-vk-bot

If you are cloning this repository, remember to run npm install to install dependencies.

Example

// TypeScript
import { Bot } from 'node-vk-bot'

// ES5
const { Bot } = require('node-vk-bot')

const bot = new Bot({
  token: 'YOUR TOKEN',
  group_id: 123456
}).start()

bot.get(/Hi|Hello|Hey/i, message => {
  const options =  { forward_messages: message.id }

  bot.send('Hello!', message.peer_id, options)
})

More examples (how to use webhooks, upload pictures, ...)

Bots created with this library

if you want your bot to be in this list, just make a pull request

Table of contents

Getting Started

In the example above you can see a super simple VK Bot. This bot will answer our greetings, that's all.

Let's explain the code, it's pretty simple.

  1. Then I create a bot instance with my group's token.
  2. By calling bot.start() the bot starts polling updates from the server.
  3. Then I simply listen on messages which pass the RegExp test, when I get such message, Then I send a new message with text 'Hello' to that chat with a forwarded message.

Bot

The class used to create new bots, it takes a single argument, an options object.

new Bot({
  token: 'TOKEN',
  group_id: 123456
  api: {
    v: 5.80, // >= 5.80
    lang: 'ru'
  }
})
ParameterTypeRequired
tokenStringYes
group_idNumberYes
apiObjectNo

api is object with API settings: version and language. (both strings) (Read more)


Methods

start

Starts polling updates from API. Emits an update event after getting updates with the response from server. Update examples.


get

Listens on specific message matching the RegExp pattern.

bot.get(/Hello/i, (msg, exec) => {
  console.log(msg)
})

The argument passed to callback is a Message object and result of pattern.exec(text).


send

Sends message.

bot.send('text', peer_id, params)

uploadPhoto

Upload a photo.

The only parameter is an absolute path to picture. Returns a Promise that resolves with a photo object

bot.uploadPhoto('~/kittens.png').then(photo => {
  console.log(photo)
})

api

Access VK API.

bot.api('users.get', { user_ids: 1 })
  .then(res => console.log(res[0].first_name)) // Pavel

When using execute method, this function returns full response object. (Because there may be errors and responses in same object).


processUpdate

Process an update from Callback API. Example of usage may be found in examples folder


stop

Stops the bot from listening on updates.

bot.stop()

Events

update

The update event is emitted whenever there is a response from LongPoll.

bot.on('update', update => {
  if (update.from_id === 1) {
    console.log('Got a message from Pavel Durov!');
  }
})

voice

The voice event is emitted whenever there is a new voice message. (emits Message object)


sticker

The sticker event is emitted whenever there is a new incoming sticker. (emits Message object)


poll-error

The poll-error event is emitted whenever there is an error occurred in LongPoll.

bot.on('poll-error', error => {
  console.error('error occurred on a working with the Long Poll server ' +
    `(${util.inspect(error)})`)
})

command-notfound

This event is emitted whenever there's no .get() listeners matching

bot.on('command-notfound', msg => {
  bot.send('What?', msg.peer_id)
})

The Message Object

interface Message {
  id: number,
  peer_id: number,
  date: number,
  text: string,
  from_id: number,
  attachments: any,
  important: boolean,
  conversation_message_id: number,
  fwd_messages: any
}

// Reference: https://vk.com/dev/objects/message

Keywords

FAQs

Last updated on 19 Jun 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc