Socket
Socket
Sign inDemoInstall

botact

Package Overview
Dependencies
Maintainers
1
Versions
111
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

botact

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


Version published
Weekly downloads
32
increased by10.34%
Maintainers
1
Weekly downloads
 
Created
Source

botact botact botact botact botact

botact.js

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

Table of content

Install

$ npm i botact

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)

Botact API

Methods

Core

Actions

Options

Upload helpers


Botact API: Core

constructor(settings)

Create bot.

Botact Flow:
Turn settings.redis to true, if you will use Botact Flow.
For detailed redis config see this

Definition:

constructor (settings: {
  confirmation: string;   // required
  token: string;          // required
  group_id?: number;

  // Flow Settings
  flowTimeout?: number;   // Document expire time, in seconds
  redis?: boolean;        // false by default
  redisConfig?: object;   // {} by default
})

Usage:

const { Botact } = require('botact')

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

.api(method, settings)

Call API method (https://vk.com/dev/methods).

Definition:

async api (
  method: string,        // required 
  options?: object,      // api call parameters
): Promise<any>;         // Promise with response/error

Usage:

const user_data = await bot.api('users.get', {
  user_ids: 1
})

.execute(method, settings, callback)

Call API by execute.

Definition:

async execute (
  method: string,        // required 
  options?: object,      // api call  parameters
  callback?: function    
): Promise<any>;         // Promise with response/error

Usage:

bot.execute('users.get', {
  user_ids: 1
}, (body) => {
  // {
  //   response: [{
  //     id: 1,
  //     first_name: 'Павел',
  //     last_name: 'Дуров'
  //   }]
  // }
})

.reply(user_id, message, attachment)

Sends message to user

Definition:

async reply (
  user_id: number, 
  message: string,      // required, if attachment not setten 
  attachment: string    // required, if message not setten 
): Promise<any>         // Promise with response/error

Usage:

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)

Start listen Express server.

Definition:

listen (
  req: any,     // Express request, required
  res: any      // Express response, required
)

Usage:

bot.listen(req, res)

Botact API: Actions

.before(callback)

Add callback before bot will start.

Definition:

before (
  callback: function
)

Usage:

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

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

.command(command, callback)

Add command w/ strict match.

Definition:

command (
  command: string | string[], 
  callback: function
): Botact

Usage:

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

.event(event, callback)

Add event handler .

Definition:

event (
  event: string | string[], 
  callback: function
): Botact;

Usage:

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

.hears(command, callback)

Add command w/ match like RegEx.

Definition:

hears (
  hear: string | RegExp | (string | RegExp)[], 
  callback: function
): Botact;

Usage:

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

.on(type, callback)

Add reserved callback.

Definition:

on (
  type: string, 
  callback: function
): Botact;

OR

on (
  callback: function
): Botact;

Usage:

bot.on(({ reply }) => reply('What?'))
bot.on('audio', ({ reply }) => reply('Great music!'))

.use(callback)

Add middleware.

Definition:

use (
  callback: function
): Botact

Usage:

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

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

Botact API: Options

[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)

Delete keys settings.

Definition:

deleteOptions (
  keys: string[]
): Botact

Usage:

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

Botact API: Upload helpers

.uploadCover(file, settings)

Upload and save cover. See detailed settings here.

Definition:

async uploadCover (
  filepath: string,    // Path to file with cover
  settings?: object
): Promise<any>        // Promise with response/error

Usage:

await bot.uploadCover('./cover.jpg', { crop_x2: 1590 })
// {
//   images: [
//     { 
//       url: "URL",
//       width: 1920,
//       height: 1080 
//     },
//     [Object],
//     [Object],
//     [Object],
//     [Object]
//   ]
// }

.uploadDocument(file, peer_id, type)

Uploads document to peer.

Definition:

async uploadDocument (
  filepath: string,               // Path to file
  peer_id: number, 
  type: 'doc' | 'audio_message'   // 'doc' by default
): Promise<any>;                  // Promise with response/error

Usage:

await bot.uploadDocument('./book.pdf', 1234)
// { 
//   response:
//     [{ 
//       id: 1234,
//       owner_id: 1234,
//       title: "",
//       ... 
//     }]
// }

.uploadPhoto(file, peer_id)

Uploads photo to peer.

Definition:

async uploadPhoto (
  filepath: string,   // Path to picture
  peer_id: number
): Promise<any>       // Promise with response/error

Usage:

await bot.uploadPhoto('./picture.png', 1234)
// {
//   id: 1234,
//   album_id: 1234,
//   owner_id: 1234,
//   ...
// }

Botact Flow API

Usage

$ redis-server

Methods

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)

Botact Flow API: Methods

.addScene(name, ...callbacks)

Add scene.

Definition:

addScene (
  name: string, 
  ...args: function[]
): Botact;

Usage:

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)

Enter scene.

Definition:

async joinScene (
  ctx: object, 
  scene: string, 
  session?: object,      // {} by default 
  step?: number,         // 0 by default
  instantly?: boolean    // true by default
): Promise<Botact>;

Usage:

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

.nextScene(ctx, body)

Navigate scene.

Definition:

async nextScene (
  ctx: object, 
  session?: object,      // {} by default 
): Promise<Botact>;

Usage:

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

.leaveScene(ctx)

Leave scene.

Definition:

async leaveScene(
  ctx: object
): Promise<Botact>;

Usage:

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

TypeScript

Botact includes TypeScript definitions.

Tests

$ npm test

Donate 💰

Thank you for donations.

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

License

MIT.

Keywords

FAQs

Package last updated on 28 Mar 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