Git Product home page Git Product logo

slack-quick-bots's Introduction

slack-quick-bots

npm version Build Status Dependency Status

Command driven slack bot library focused to quickly rolled out api/data driven bots abstracting most common bot needs.

const SlackBot = require('slack-quick-bots');
const coolBot = new SlackBot({
  // all bot config
});
coolBot.start();

Simple bot

Clone and get started!! Sample bot

Pagerduty bot

Config schema

slack-quick-bots uses handlebars template as view layer for all bot output messages. When callback with called with data it is rendered against the template. Templates are like html. Data is injected into the template before sent to slack.

{
  bot: [{
    botCommand: {
      firstCommand: {
        commandType: 'DATA',
        template: sampleTmpl,
        data: function(input, options, callback) {
          // input.command - for command name.
          // input.params - for params in array.
          // options.user.profile.email - email in slack.
          // options.channel - channel from which the command was fired.
          callback({
            data: 'message to respond for command goes here'
          });
        }
      }
    }
    schedule: true,   // Generic schedule command for all bot command. Example command (schedule firstCommand (* * * * *)) executes firstCommand for every minute.
    botToken: args[0]
  }]
}

Command format

In channel @botname {command} {params1} {params2}

@newbot firstCommand param1 params2

DM {command} ['params1', 'params2']

firstCommand param1 params2

Schedule command

schedule commandName [params] (* * * * *)

schedule firstCommand params1 params2 (* * * * *)

Stop command

Use to stop a schedule command. A stop is automatically added to the command list if the bots contains RECURSIVE, ALERT or SCHEDULE is enabled.

stop firstCommand // if firstCommand is a RECURSIVE command.
stop schedule scheduleId // to stop schedule command.

Command type

  • DATA - Simple data query.
  • RECURSIVE - Simple data query bound to a timer.
  • Alert - Run a peak/dip algorithm on a dataset and notifies channels/users bases on the threshold set in realtime.

Library support wide range of response type. You can stream file of any type for a DATA command. The below sample is to generate a graph in realtime. Make sure you have gnuplot for graphs for time series data.

responseType: {
  type: 'png',
  ylabel: 'errors',
  xlabel: 'title errors',
  timeUnit: 'm',
  title: 'Log data',
  logscale: false,
  style: 'lines',
  exec: { encoding: 'utf16' }
}

Input validation

  validation: [{
    schema: [1, 1],
    default: [1, 1],
    help: [{
      sample: '{firstArg}',
      recommend: '1',
      error: '{{arg}} is incorrect',
    }, {
      sample: '{secondArg}',
      recommend: '1',
      error: '{{arg}} is incorrect',
    }],
  }],

Supports regex

  validation: [{
    schema: [/^(?:[1-9]\d?|100)$/, 1],
    default: [1, 1],
    help: [{
      sample: '{firstArg}',
      recommend: '1',
      error: '{{arg}} is incorrect',
    }, {
      sample: '{secondArg}',
      recommend: '1',
      error: '{{arg}} is incorrect',
    }],
  }],

Supports setting up custom webhooks. Below configuration sets up a http server to serve webhook request. You should also add webHook: true at bot level config to make webhook available for bots. Custom webhook can be used to trigger long running operation and at the completion of the operation the hookUrl can be used to notify the user who triggered the operation.

server: {
  host: 'http://custombothostname',
  port: 9090,
  webHook: true
}
data: function(input, options, callback) {
  // options.hookUrl - http://custombothostname:9090//hook/U2U9RBV8R/69b773b0-a110-47cc-987d-48756d86a5ab.
  callback({
    data: 'message to respond for command goes here'
  });
}

Access control

  {
     bot: [{
       botCommand: {
         firstCommand: {
           allowedUsers: ['slackUsername'] // firstCommand work only for slack user with id 'slackUsername'.
         }
       }
     }],
     blockDirectMessage: false, // block direct message to the bot.
  }

Testing bots

  const onMessageSpy = sinon.spy((response) => {
    setTimeout(() => {
      expect(response.message).to.equal('Hello 1');
      done();
    }, 1);
  });

  testBots.start().then((botEvt) => {

    botEvt[0].on('connect', () => {
      botEvt[0].injectMessage({
        text: 'ping 1',
        channel: 'D1234567'
      });
    });

    botEvt[0].on('message', onMessageSpy);
  });

License

Copyright (c) 2017 Umashankar Subramanian
Licensed under the MIT license.

slack-quick-bots's People

Contributors

usubram avatar

Watchers

Christopher Garcia avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.