Skip to content

An experimental package for handling conversational form with Bottender.

License

Notifications You must be signed in to change notification settings

bottenderjs/bottender-form

Repository files navigation

bottender-form

npm Build Status License: MIT

An experimental package for handling conversational form with Bottender.

Installation

npm install bottender-form

Usage

const { middleware } = require('bottender');
const form = require('bottender-form');

const handleForm = form({
  name: 'user',
  shouldStart: context => context.event.text === '/form',
  steps: [
    {
      question: "What's your name?",
      stateKey: 'user.name',
    },
    {
      question: 'How old are you?',
      validation: text => /\d+/.test(text),
      map: numstr => +numstr,
      stateKey: 'user.age',
    },
  ],
});

bot.onEvent(
  middleware([
    handleForm,
    async context => {
      if (context.event.isMessage) {
        await context.sendText(`user: ${JSON.stringify(context.state.user)}`);
      }
    },
  ])
);

Options

name

text

shouldStart

Default: () => false.

shouldStop

Default: () => false.

retryMessage

Default: Validation failed. Please try again..

retryTimes

Default: 3.

didFinish

Default: () => {}.

Step

question

text | async context => void

stateKey

text

validation

text => boolean | async text => boolean

map

text => any

License

MIT © Yoctol

About

An experimental package for handling conversational form with Bottender.

Resources

License

Stars

Watchers

Forks

Packages

No packages published