Skip to content

Instantly share code, notes, and snippets.

@jazzedge
Last active August 2, 2017 06:59
Show Gist options
  • Save jazzedge/b71190c2299d50e7aefa1d79bd06197f to your computer and use it in GitHub Desktop.
Save jazzedge/b71190c2299d50e7aefa1d79bd06197f to your computer and use it in GitHub Desktop.
Bot - Loop to fill a form
// https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-loops/app.js
//This Bot demonstrates how to use session.replaceDialog() to create a simple
// loop that dyanmically populates a form.
// Add Q&A dialog
bot.dialog('q&aDialog', [
function (session, args) {
// Save previous state (create on first call)
session.dialogData.index = args ? args.index : 0;
session.dialogData.form = args ? args.form : {};
// Prompt user for next field
builder.Prompts.text(session, questions[session.dialogData.index].prompt);
},
function (session, results) {
// Save users reply
var field = questions[session.dialogData.index++].field;
session.dialogData.form[field] = results.response;
// Check for end of form
if (session.dialogData.index >= questions.length) {
// Return completed form
session.endDialogWithResult({ response: session.dialogData.form });
} else {
// Next field
session.replaceDialog('q&aDialog', session.dialogData);
}
}
]);
var questions = [
{ field: 'name', prompt: "What's your name?" },
{ field: 'age', prompt: "How old are you?" },
{ field: 'state', prompt: "What state are you in?" }
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment