/
mainDialog.js
83 lines (68 loc) · 3.13 KB
/
mainDialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { MessageFactory, InputHints } = require('botbuilder');
const { ComponentDialog, DialogSet, DialogTurnStatus, TextPrompt, WaterfallDialog } = require('botbuilder-dialogs');
const MAIN_WATERFALL_DIALOG = 'mainWaterfallDialog';
class MainDialog extends ComponentDialog {
constructor(beverageDialog, foodDialog, intentDialog) {
super('MainDialog');
// Define the main dialog and its related components.
this.addDialog(new TextPrompt('TextPrompt'))
.addDialog(intentDialog)
.addDialog(beverageDialog)
.addDialog(foodDialog)
.addDialog(new WaterfallDialog(MAIN_WATERFALL_DIALOG, [
this.intentStep.bind(this),
this.mainStep.bind(this),
this.finalStep.bind(this)
]));
this.initialDialogId = MAIN_WATERFALL_DIALOG;
}
/**
* The run method handles the incoming activity (in the form of a TurnContext) and passes it through the dialog system.
* If no dialog is active, it will start the default dialog.
* @param {*} turnContext
* @param {*} accessor
*/
async run(turnContext, accessor) {
const dialogSet = new DialogSet(accessor);
dialogSet.add(this);
const dialogContext = await dialogSet.createContext(turnContext);
const results = await dialogContext.continueDialog();
if (results.status === DialogTurnStatus.empty) {
await dialogContext.beginDialog(this.id);
}
}
async intentStep(stepContext) {
const intentDetails = {};
return await stepContext.beginDialog('intentDialog', intentDetails)
}
async mainStep(stepContext) {
const intentDetails = stepContext.options;
intentDetails.intent = stepContext.result
switch(intentDetails.intent.intent) {
case "food":
return await stepContext.beginDialog('foodDialog', intentDetails)
case "beverage":
return await stepContext.beginDialog('beverageDialog', intentDetails)
default:
console.log(JSON.stringify(intentDetails))
}
return await stepContext.next();
}
async beverageStep(stepContext) {
const beverageDetails = {};
return await stepContext.beginDialog('beverageDialog', beverageDetails)
}
async finalStep(stepContext) {
// If the child dialog ("bookingDialog") was cancelled or the user failed to confirm, the Result here will be null.
if (stepContext.result) {
const result = stepContext.result;
const msg = `I have chosen ${ JSON.stringify(result) }`;
await stepContext.context.sendActivity(msg, msg, InputHints.IgnoringInput);
}
// Restart the main dialog with a different message the second time around
return await stepContext.replaceDialog(this.initialDialogId, { restartMsg: 'What else can I do for you?' });
}
}
module.exports.MainDialog = MainDialog;