Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-vk-bot

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-vk-bot

Create and control VK bots easily.

  • 0.2.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Codacy Badge CircleCI

VK BOTS

Create and control VK bots easily. Works with both profile and group tokens!

npm install --save node-vk-bot

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

Example

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

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

const bot = new Bot({
  token: 'YOUR TOKEN',
  prefix: /^Bot[\s,]/
}).start()

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

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

Table of contents

Getting Started

To get updates from the server, we use polling.

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. First of all, you have to import the library.

  2. Then I create a bot instance, with my token.

    get yourself one:

    https://oauth.vk.com/authorize?client_id= YOUR APP ID &scope=photos,messages,offline&display=touch&response_type=token
    
  3. By calling bot.start() the bot starts polling updates from the server.

  4. 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.

The API is simple as possible, still, if you have any suggestions about simplifying the API, please fill an issue.


Bot

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

new Bot({
  token: '5a9bdc30ea18ab4a685a8f773642ba0d', // don't even try to use this token
  prefix: /^Bot[\s,]/,
  prefixOnlyInChats: true,
  chats: [
    1,
    2e9 + 12
  ],
  api: {
    v: 5.62, // must be >= 5.38
    lang: 'ru'
  }
})
ParameterTypeRequired
tokenStringYes
prefixRexExpNo
prefixOnlyInChatsBooleanNo
chatsArrayNo
apiObjectNo

If prefix is set, the bot will work only with messages with prefix match. (if prefixOnlyInChats is true, then prefix will be checked only for messages from group chats)
If chats is set, the bot will work only with messages from these chats

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


Methods

start

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

See poll.js for more info.


get

Listens on specific message matching the RegExp pattern.

bot.get(/Hello/i, msg => {
  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 })

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


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[7].from === 1) {
    console.log('Got an message from Pavel Durov!');
  }
})

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, // message id
  peer_id: number, // message's chat peer_id
  date: number, // time (in Unixtime format)
  title: string, // chat title 
  body: string, // message text
  user_id: number, // sender's ID 
  attachments: Object // vk.com/dev/using_longpoll - see the attachments section
}

Keywords

FAQs

Package last updated on 27 Feb 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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc