# bottender-xstate

> Finite State Machines in Bottender using xstate

Latest version **0.5.1** (published 2019-10-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install bottender-xstate
pnpm add bottender-xstate
yarn add bottender-xstate
bun add bottender-xstate
```

## 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.5.1 |
| Published | 2019-10-07 |
| First published | 2018-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.6 |
| Dependencies | 3 |
| Unpacked size | 20.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | calvinlin, chentsulin |
| Keywords | bot, bottender, state-machine, xstate |

## Links

- npm: https://www.npmjs.com/package/bottender-xstate
- npm.io page: https://npm.io/package/bottender-xstate

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [xstate](https://npm.io/package/xstate.md) 4.0.1
- [warning](https://npm.io/package/warning.md) ^4.0.2

## Recent versions

- 0.5.1 (latest) — 2019-10-07
- 0.4.1-events (next) — 2018-09-13
- 0.5.0 — 2018-11-19
- 0.4.1 — 2018-10-11
- 0.4.0 — 2018-09-04
- 0.3.1 — 2018-08-19
- 0.3.0 — 2018-08-17
- 0.2.0 — 2018-08-16
- 0.1.0 — 2018-08-02
- 0.0.4 — 2018-07-21
- 0.0.3 — 2018-07-21
- 0.0.2 — 2018-07-20
- 0.0.1 — 2018-07-17

## README

# bottender-xstate

[![CircleCI](https://circleci.com/gh/Yoctol/bottender-xstate.svg?style=shield&circle-token=df773eab701966e6af5b8b57a8969764c3d351f8)](https://circleci.com/gh/Yoctol/bottender-xstate)
[![npm](https://img.shields.io/npm/v/bottender-xstate.svg?style=flat-square)](https://www.npmjs.com/package/bottender-xstate)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> Finite State Machines in [Bottender](https://github.com/Yoctol/bottender) using [xstate](https://github.com/davidkpiano/xstate)

## Installation

```sh
npm install bottender-xstate
```

## API Reference

| Param                   | Type            | Description                                                                    |
| ----------------------- | --------------- | ------------------------------------------------------------------------------ |
| config                  | `XstateConfig`  | Config to be passed to xstate.                                                 |
| mapContextToXstateEvent | `Function`      | Mapper for create xstate event from context.                                   |
| actions                 | `Object`        | Map of named actions.                                                          |
| guards                  | `Object`        | Map of named guards.                                                           |
| events                  | `Array<String>` | All events handled by the machine. (required when using `* (catch-all)` event) |
| onEvent                 | `Function`      | Callback to be called when trigger event.                                      |
| onAction                | `Function`      | Callback to be called when trigger action.                                     |

```js
const bottenderXstate = require('bottender-xstate');

const config = {
  id: 'light',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: { target: 'yellow' },
      },
      onEntry: 'enterGreen',
      onExit: 'leaveGreen',
    },
    yellow: {
      on: {
        TIMER: { target: 'red' },
      },
      onEntry: 'enterYellow',
      onExit: 'leaveYellow',
    },
    red: {
      on: {
        TIMER: {
          target: 'green',
          actions: ['fromRedToGreen'],
        },
      },
      onEntry: 'enterRed',
      onExit: 'leaveRed',
    },
  },
};

const mapContextToXstateEvent = () => 'TIMER';

const actions = {
  enterGreen: context => context.sendText('enter green'),
  enterYellow: context => context.sendText('enter yellow'),
  enterRed: context => context.sendText('enter red'),
  leaveGreen: context => context.sendText('leave green'),
  leaveYellow: context => context.sendText('leave yellow'),
  leaveRed: context => context.sendText('leave red'),
  fromRedToGreen: context => context.sendText('from red to green'),
};

bot.onEvent(
  bottenderXstate({
    config,
    mapContextToXstateEvent,
    actions,
  })
);
```

You can find more examples in the
[examples](https://github.com/Yoctol/bottender-xstate/tree/master/examples) folder.

## License

MIT © [Yoctol](https://github.com/Yoctol/bottender-xstate)

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