回應使用者的訴求
// The dialog stack is cleared and this dialog is invoked when the user enters 'help'.
bot.dialog('help', function (session, args, next) {
session.endDialog("This is a bot that can help you make a dinner reservation. <br/>Please say 'next' to continue");
})
.triggerAction({
matches: /^help$/i,
});
bot.dialog('help', function (session, args, next) {
session.endDialog("This is a bot that can help you make a dinner reservation. <br/>Please say 'next' to continue");
})
.triggerAction({
matches: /^help$/i,
onSelectAction: (session, args, next) => {
// Add the help dialog to the dialog stack
// (override the default behavior of replacing the stack)
session.beginDialog(args.action, args);
}
});
這兩個範例都可以全域呼叫,
差別在於 onSelectAction 不會終止原本的dialog流程,結束之後,還是會回到原本的dialog.
// Dialog to ask for number of people in the party
bot.dialog('askForPartySize', [
function (session) {
builder.Prompts.text(session, "How many people are in your party?");
},
function (session, results) {
session.endDialogWithResult(results);
}
])
.beginDialogAction('partySizeHelpAction', 'partySizeHelp', { matches: /^help$/i });
// Context Help dialog for party size
bot.dialog('partySizeHelp', function(session, args, next) {
var msg = "Party size help: Our restaurant can support party sizes up to 150 members.";
session.endDialog(msg);
})
改變dialog主題
也因為前述,triggerAction不加onSelectAction,可以清空目前的dialog對話邏緝,故可以改變主題
bot.dialog('orderDinner', [
//...waterfall steps...
// Last step
function(session, results){
if(results.response){
session.dialogData.room = results.response;
var msg = `Thank you. Your order will be delivered to room #${session.dialogData.room}`;
session.endConversation(msg);
}
}
]);
bot.dialog('dinnerOrder', [
//...waterfall steps...
])
.endConversationAction(
"endOrderDinner", "Ok. Goodbye.",
{
matches: /^cancel$|^goodbye$/i,
confirmPrompt: "This will cancel your order. Are you sure?"
}
);
呼叫 endConversation 或是 endConversationAction 都可以結束這次的對話。