Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

botstream

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

botstream

stream-based bot micro framework

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

botstream

stream-based bot micro framework

botstream.gif

Example

1. Define Message Parser (from slack)

Parse slack outgoing webhooks POST payload and convert it to an event object with properties user and input.

var parse = require('querystring').parse;
var through = require('through');

function slackParser(options) {
  var buffer = [];
  return through(function(data) {
    buffer.push(data.toString('utf8'));
  }, function() {
    var parsed = parse(buffer.join(''));
    // omit payload from @slackbot which causes infinite meesage loop
    if (parsed.user_name !== 'slackbot') {
      this.queue({
        user: parsed.user_name,
        input: parsed.text
      });
    }
    this.queue(null);
  });
};

2. Define Actions

Here we define some rules to compose the outgoing message. Action must be defined as a function which returns a through (both readable and writable) stream.

function hello() {
  return through(function(event) {
    event.output = "hello " + event.user;
    this.queue(event);
  });
}

function bye() {
  return through(function(event) {
    event.output = "bye " + event.user;
    this.queue(event);
  });
}

3. Define Outgoing (to slack)

Convert event object to slack response format.

function slackOutgoing() {
  return through(function(event) {
    this.queue(JSON.stringify({ text: event.output }));
  });
};

4. Register Actions

Integrate all the functions into a botstream application.

var botstream = require('botstream');

var actions = botstream.select()
  .case(/hello/, hello)
  .case(/bye/, bye)
  .default();

var bot = botstream.app(slackParser)
  .register(actions)
  .register(slackOutgoing);

5. Mount to HTTP(S)

Pipe it from/to existing application. Whatever application which implements stream interface is available.

app.post('/bot', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  req.pipe(bot.stream()).pipe(res);
});

Keywords

FAQs

Package last updated on 05 May 2015

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