Socket
Socket
Sign inDemoInstall

botact

Package Overview
Dependencies
156
Maintainers
1
Versions
111
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    botact

A framework for creating VK bots on callback API.


Version published
Weekly downloads
14
decreased by-22.22%
Maintainers
1
Install size
11.8 MB
Created
Weekly downloads
 

Readme

Source

botact botact botact botact botact

botact.js

Botact enables developers to focus on writing reusable application logic instead of spending time building infrastructure.

Donate 💰

Thank you for donations.

  • Bitcoin: 1C26xXoA42Ufz5cNNPhAJY8Ykqh2QB966L
  • Ethereum: 0x331FeA1a0b0E9E66A647e964cF4eBE1D2E721579
  • Qiwi: 79522232254

Install

$ npm i botact

Tests

$ npm test

Usage

const bodyParser = require('body-parser')
const express = require('express')
const { Botact } = require('botact')

const app = express()
const bot = new Botact({
  confirmation: process.env.CONFIRMATION,
  token: process.env.TOKEN
})

bot.command('start', ({ reply }) => reply('This is start!'))
bot.hears(/(car|tesla)/, ({ reply }) => reply('I love Tesla!'))
bot.event('group_join', ({ reply }) => reply('Thanks!'))
bot.on(({ reply }) => reply('What?'))

app.use(bodyParser.json())
app.post('/', bot.listen)
app.listen(process.env.PORT)

Methods

constructor(options)

ParameterTypeRequried
optionsobjectyes

Create bot.

const { Botact } = require('botact')

const bot = new Botact({
  confirmation: process.env.CONFIRMATION,
  token: process.env.TOKEN
})

[getter] options

Get options.

bot.options
// {
//   confirmation: '12345',
//   token: 'abcde...'
// }

[setter] options

Set options.

bot.options = { foo: 'bar' }
// {
//   confirmation: '12345',
//   token: 'abcde...',
//   foo: 'bar'
// }

.deleteOptions(settings)

ParameterTypeRequried
settingsarrayyes

Delete keys settings.

bot.deleteOptions([ 'token', 'confirmation' ])
// {
//   foo: 'bar'
// }

.before(callback)

ParameterTypeRequried
callbackfunctionyes

Add callback before bot will start.

bot.before(() => new Date())

bot.on(({ inital }) => {
  // Fri Nov 24 2017 16:00:21 GMT+0300 (MSK)
})

.use(callback)

ParameterTypeRequried
callbackfunctionyes

Add middleware.

bot.use((ctx) => ctx.date = new Date())

bot.on(({ date }) => {
  // Fri Nov 24 2017 16:00:21 GMT+0300 (MSK)
})

.execute(method, settings, token, callback)

ParameterTypeRequried
methodstringyes
settingsobjectyes
tokenstringyes
callbackfunctionno

Call API by execute.

bot.execute('users.get', {
  user_ids: 1
}, this.settings.token, (body) => {
  // {
  //   id: 1,
  //   first_name: 'Pavel',
  //   last_name: 'Durov'
  // }
})

.command(command, callback)

ParameterTypeRequried
commandstringyes
callbackfunctionyes

Add command w/ strict match.

bot.command('start', ({ reply }) => {
  reply('This is start!')
})

.hears(command, callback)

ParameterTypeRequried
commandstring/regexpyes
callbackfunctionyes

Add command w/ match like RegEx.

bot.hears(/(car|tesla)/, ({ reply }) => {
  reply('I love Tesla!')
})

.on(callback)

ParameterTypeRequried
callbackfunctionyes

Add reserved callback.

bot.on(({ reply }) => {
  reply('What?')
})

.event(event, callback)

ParameterTypeRequried
eventstringyes
callbackfunctionno

Add event.

bot.event('group_join', ({ reply }) => {
  reply('Thanks!')
})

.uploadDocument(file)

ParameterTypeRequried
filestringyes

Upload document.

await bot.uploadDocument(path.join(__dirname, 'files', 'book.pdf'))
// {
//   id: 445225557
//   owner_id: 145003487,
//   title: 'book.pdf',
//   ...
// }

.uploadPhoto(file)

ParameterTypeRequried
filestringyes

Upload photo.

await bot.uploadPhoto(path.join(__dirname, 'files', 'girl.png')) // { id: 456246067, ... }
// {
//   id: 456246067,
//   album_id: -14,
//   owner_id: 145003487,
//   ...
// }

.uploadAndSaveCoverPhoto(file)

ParameterTypeRequried
filestringyes

Upload and save cover.

await bot.uploadAndSaveCoverPhoto('./cover.jpg')
// {
//   response: {
//     images: [ [Object], [Object], [Object], [Object], [Object] ]
//   }
// }

.reply(userId, message, attachment, callback)

ParameterTypeRequried
userIdnumber or arrayyes
messagestringyes (no, if setten attachment)
attachmentstringyes (no, if setten message)
callbackfunctionno
bot.command('start', (ctx) => {
  // with shortcut from context
  ctx.reply('Hi, this is start!')
  // function from context
  ctx.sendMessage(ctx.user_id, 'Hi, this is start!')
  // simple usage
  bot.reply(ctx.user_id, 'Hi, this is start!')
  // to multiple users
  bot.reply([ ctx.user_id, 1 ], 'Hi, this is start!')
})

.listen(req, res)

ParameterTypeRequried
reqobjectyes
resobjectyes

Start listen.

bot.listen(req, res)

Scenes

Usage

$ redis-server

Example

const bodyParser = require('body-parser')
const express = require('express')
const { Botact } = require('botact')

const app = express()
const bot = new Botact({
  confirmation: process.env.CONFIRMATION,
  token: process.env.TOKEN,
  flowTimeout: 20, // document will be deleted after 20 secs
  redisConfig: {
    host: '127.0.0.1', // default host for redis
    port: 8080 // custom port for redis
  },
})

bot.addScene('wizard',
  ({ reply, scene: { next } }) => {
    next()
    reply('Write me something!')
   },
  ({ reply, body, scene: { leave } }) => {
    leave()
    reply(`You wrote: ${body}`)
  }
)

bot.command('join', ({ scene: { join } }) => join('wizard'))

app.use(bodyParser.json())
app.post('/', bot.listen)
app.listen(process.env.PORT)

.addScene(name, ...callbacks)

ParameterTypeRequried
namestringyes
callbacksfunctionminumum one

Add scene.

bot.addScene('wizard',
  ({ reply, scene: { next } }) => {
    next()
    reply('Write me something!')
  },
  ({ reply, body, scene: { leave } }) => {
    leave()
    reply(`You wrote: ${body}`)
  }
)

.joinScene(ctx, scene, session, step, now)

ParameterTypeRequriedDefault
ctxobjectyesnone
scenestringyesnone
sessionobjectno{}
stepnumberno0
nownumbernotrue

Enter scene.

bot.command('join', (ctx) => {
  // with shortcut without additional settings
  ctx.scene.join('wizard')
  // simple usage with additional settings
  bot.joinScene(ctx, 'wizard', { foo: 'bar' })
})

.leaveScene(ctx)

ParameterTypeRequried
ctxobjectyes

Leave scene.

bot.addScene('wizard',
  (ctx) => {
    // with shortcut
    ctx.scene.leave()
    // simple usage
    bot.leaveScene(ctx)
  }
)

.nextScene(ctx, body)

ParameterTypeRequried
ctxobjectyes
sessionobectno

Navigate scene.

bot.addScene('wizard',
  (ctx) => {
    // with shortcut without additional settings
    ctx.scene.next({ foo: 'bar' })
    // simple usage with additional settings
    bot.nextScene(ctx, { foo: 'bar' })
  }
)

License

MIT.

Keywords

FAQs

Last updated on 01 Dec 2017

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