API for VK bots, based on Long Poll.
$ npm i node-vk-bot-api
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()
- constructor(options)
- .use(callback)
- .command(command, callback)
- .hears(command, callback)
- .on(callback)
- .listen()
Parameter | Type | Required |
---|---|---|
token | string | yes |
Create bot.
const bot = new API(process.env.TOKEN)
Parameter | Type | Required |
---|---|---|
callback | function | yes |
Add middleware.
bot.use(ctx => ctx.date = new Date())
bot.on(({ date }) => {
// Fri Nov 24 2017 16:00:21 GMT+0300 (MSK)
})
Parameter | Type | Required |
---|---|---|
command | string | yes |
callback | function | yes |
Add command w/ strict match.
bot.command('start', ({ reply }) => reply('This is start!'))
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!'))
Parameter | Type | Required |
---|---|---|
callback | function | yes |
Add reserved callback.
bot.on(({ reply }) => {
reply('What?')
})
Start listen.
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) => {
// with shortcut from context
ctx.reply('Hi, this is start!')
// function from context
ctx.sendMessage(ctx.peer_id, 'Hi, this is start!')
// simple usage
bot.reply(ctx.peer_id, 'Hi, this is start!')
})
MIT.