npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slack-quick-bots

v9.0.0

Published

A slack bot solution for enterprise.

Downloads

145

Readme

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 schema
});
coolBot.start();

Simple bot

Step 1

git clone https://github.com/usubram/slack-quick-bots-reference.git
npm install && node index.js <botToken> // pass your bot token.

Step 2

No more steps. You have successfully started your bot.

Sample reference app

Sample app

Pagerduty bot

Pagerduty bot

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]
  }]
}

Schedule command

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

schedule firstCommand params1 params2 (* * * * *)

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.

Response type

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',
    }],
  }],

Custom webhooks

Supports setting up custom webhooks. Below schema sets up a http server to serve webhook request. You should also add webHook: true at bot level schema 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: ['slackUserId'] // firstCommand work only for slack user with id 'slackUserId'.
         }
       }
     }],
     blockDirectMessage: false, // block direct message to the bot.
  }

Threaded Replies

  {
     bot: [{
       botCommand: {
         firstCommand: {
           allowedUsers: ['slackUserId'] // firstCommand work only for slack user with id 'slackUserId'.
         }
       }
     }],
     repliesInThread: true, // All Replies will be in threads instead of channel directly.
  }

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) 2018 Umashankar Subramanian
Licensed under the MIT license.