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

node-vk-bot-api

Package Overview
Dependencies
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-vk-bot-api

API for VK bots based on long poll with multi-dispatch send messages (~75 per second).

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
234
increased by4.93%
Maintainers
1
Weekly downloads
 
Created
Source

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

VK Bot API

Clean API for VK bots based on long poll with multi-dispatch send messages (~75 per second).

Install

$ npm install node-vk-bot-api

Example

Full example you can see here.

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

app.auth(process.env.BOT_TOKEN)

app.command('/start', (data) => {
  const uid = data.user_id

  app.sendMessage(uid, 'Hello, this is /start command!')
})

app.hears('hello', (data) => {
  const uid = data.user_id

  app.sendMessage(uid, 'Hi!')
})

app.reserve(data => {
  const uid = data.user_id
  const msg = data.msg

  app.sendMessage(uid, msg, 'wall145003487_1900')
})

app.startLongPoll()

Methods

.auth(token, opts)

ParameterTypeRequried
tokenstringyes
optsobjectno

Authting with token. Also you can set subscribers mode and bot will reply only to subscribers.

// Bot will reply to all
app.auth(process.env.BOT_TOKEN)
// Bot will reply only to subscribers.
// If user isn't subscriber, bot will send 'Bot available only for subscribers ...'
app.auth(process.env.BOT_TOKEN, {
  gid: 138165805, // group id
  msg: 'Bot available only for subscribers. Subscribe and then try again. <3'
})

.command(command, callback)

ParameterTypeRequried
commandstringyes
callbackfunctionyes

If bot get message which equal to command, then will run callback.

app.command('/start', (data) => {
  app.sendMessage(data.user_id, 'This is start command!')
})

.hears(command, callback)

ParameterTypeRequried
commandstringyes
callbackfunctionyes

If bot hears command in message from user, then will run callback (e.g. user sent 'Hello, world' and bot hears 'hello', then bot will run callback).

app.hears('hello', (data) => {
  app.sendMessage(data.user_id, 'Hi!')
})

.reserve(callback)

ParameterTypeRequried
callbackfunctionyes

If bot get message and this isn't command, then will run reserved callback.

app.reserve(data => {
  app.sendMessage(data.user_id, 'Sorry, you sent not command to bot.')
})

.sendMessage(uid, msg, attach)

ParameterTypeRequried
uidnumberyes
msgstringyes (no, if setten attach)
attachstringyes (no, if setten msg)

Send message (multi-dispatch). Also you can only one argument opts, it's must be equal to object All params for this object you can see on messages.send page.

app.sendMessage(data.user_id, 'Hello, world!')

app.sendMessage({
  user_id: data.user_id,
  message: 'Hello, function takes only one argument now. It\'s opts.',
  forward_messages: '123,431,544'
})

.replyMessage(updates)

ParameterTypeRequried
updatesarrayyes

Core function for reply message to user. In the start function calls getForwardMessage and then see is the message a command or action and calls sendMessage.

.getLastMessage(update)

ParameterTypeRequried
updateobjectyes

Get last message from forward message.

app.getLastMessage({
  "response": {
    "count": 1,
    "items": [{
      "id": 480,
      "date": 1491653021,
      "out": 0,
      "user_id": 145003487,
      "read_state": 1,
      "title": " ... ",
      "body": "",
      "fwd_messages": [{
        "user_id": -138165805,
        "date": 1491652976,
        "body": "Hello, world!"
      }]
    }]
  }
})

.getForwardMessage(update)

ParameterTypeRequried
updatearrayyes

Get message info from forward message. If function detects fwd_messages, then will call getLastMessage.

app.getForwardMessage([ 4, 487, 529, 145003487, 1491653078, ' ... ', '',  { fwd: '145003487_2214301' } ])

.startLongPoll()

Get long poll params.

app.startLongPoll()

.getLongPoll()

Start long poll.

License

MIT.

Keywords

FAQs

Package last updated on 13 Apr 2017

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