# botstream

> stream-based bot micro framework

Latest version **0.1.2** (published 2015-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install botstream
pnpm add botstream
yarn add botstream
bun add botstream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2015-05-05 |
| First published | 2015-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Ryo Murayama |
| Maintainers | hitsujiwool |
| Keywords | chat, bot, stream |

## Links

- npm: https://www.npmjs.com/package/botstream
- Repository: https://github.com/hitsujiwool/botstream
- Homepage: https://github.com/hitsujiwool/botstream#readme
- Issues: https://github.com/hitsujiwool/botstream/issues
- npm.io page: https://npm.io/package/botstream

## Dependencies (1)

- [event-stream](https://npm.io/package/event-stream.md) ^3.3.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2015-05-05
- 0.1.1 — 2015-05-02

## README

# botstream

stream-based bot micro framework

![botstream.gif](https://cldup.com/gDtCRAqaNR.gif)

## Example

### 1. Define Message Parser (from slack)

Parse [slack outgoing webhooks](https://api.slack.com/outgoing-webhooks) POST payload and convert it to an event object with properties `user` and `input`.

```javascript
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.

```javascript
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.

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

### 4. Register Actions

Integrate all the functions into a botstream application.

```javascript
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.

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

---
_Source: https://npm.io/package/botstream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
