An experimental package for handling conversational form with Bottender.
npm install bottender-form
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)}`);
}
},
])
);
text
Default: () => false
.
Default: () => false
.
Default: Validation failed. Please try again.
.
Default: 3
.
Default: () => {}
.
text | async context => void
text
text => boolean | async text => boolean
text => any
MIT © Yoctol