bottender-form
v0.2.1
Published
An experimental package for handling conversational form with Bottender.
Downloads
4
Readme
bottender-form
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