node-vk-bot-api
API for VK bots, based on Long Poll.
Install
$ npm i node-vk-bot-api
Example
const API = require('node-vk-bot-api')
const bot = new API(process.env.TOKEN)
bot.command('start', ({ reply }) => reply('This is start!'))
bot.hears(/(car|tesla)/, ({ reply }) => reply('I love Tesla!'))
bot.on(({ reply }) => reply('What?'))
bot.listen()
Methods
constructor(options)
Parameter | Type | Required |
---|
token | string | yes |
Create bot.
const bot = new API(process.env.TOKEN)
.use(callback)
Parameter | Type | Required |
---|
callback | function | yes |
Add middleware.
bot.use(ctx => ctx.date = new Date())
bot.on(({ date }) => {
})
.command(command, callback)
Parameter | Type | Required |
---|
command | string | yes |
callback | function | yes |
Add command w/ strict match.
bot.command('start', ({ reply }) => reply('This is start!'))
.hears(command, callback)
Parameter | Type | Required |
---|
command | string/regexp | yes |
callback | function | yes |
Add command w/ match like RegEx.
bot.hears(/(car|tesla)/, ({ reply }) => reply('I love Tesla!'))
.on(callback)
Parameter | Type | Required |
---|
callback | function | yes |
Add reserved callback.
bot.on(({ reply }) => {
reply('What?')
})
.listen()
Start listen.
Context Methods
.reply(peer_id, message, attachment, callback)
Parameter | Type | Requried |
---|
user_id | number or array | yes |
message | string | yes (no, if setten attachment) |
attachment | string | yes (no, if setten message) |
callback | function | no |
Send a message to user.
bot.command('start', (ctx) => {
ctx.reply('Hi, this is start!')
ctx.sendMessage(ctx.peer_id, 'Hi, this is start!')
bot.reply(ctx.peer_id, 'Hi, this is start!')
})
License
MIT.