Botframework Prompt dialogs until user finishes

你离开我真会死。 提交于 2019-12-06 14:28:22

What you're looking for is session.replaceDialog(); there is an example labeled 'basics-loops' on the GitHub repo for the SDK. To loop through prompts, one has to create a small dialog with the desired prompts and have the dialog restart automatically via session.replaceDialog() or session.beginDialog().

I've built a chatbot that receives key-value pairs in the scenario you specified above. The code excerpt below is the final step in my 'Loop' dialog.

function (session, results) {
    var value = results.response ? results.response : null,
        key = session.dialogData.key;
    var pairs = session.userData.kVPairs;
    var newPair = {};
    newPair[key] = value;
    if (key && value) {
        session.userData.kVPairs.push(newPair);
        console.log(pairs[pairs.length - 1]);
    }
    session.send('latest key-value pair added, { %s : %s }', key, value);
    session.replaceDialog('Loop');
} 

session.replaceDialog('Loop') is incorporated at the end of this waterfall step and takes the Id of the new dialog. The method can also take optional arguments to pass to the new dialog.

Note: While not applicable here, the difference between replaceDialog and beginDialog/endDialog is semi-obvious, when you use beginDialog, the new dialog is added to the stack. When you end that child dialog, you will be returned to the original/parent dialog. replaceDialog will end the current dialog and begin the new one.

You may use replacedialog to loop the user:

bot.dialog("/getUserKeys", [
   function (session, args, next) {
       session.dialogData.keys = args && args.keys ? args.keys : [];
       builder.Prompts.text(session, "Another key to put?");
   },
   function (session, results, next) {
       if (results.response === "none") {
           session.endDialogWithResult({response: { keys: session.DialogData.keys }});
           return;
       }
       session.dialogData.keys[session.dialogData.keys.length] = results.response;
       session.replaceDialog("/getUserKeys", { keys: session.DialogData.keys });
   }
]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!