forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
157 lines (131 loc) · 6.76 KB
/
bot.js
File metadata and controls
157 lines (131 loc) · 6.76 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityTypes } = require('botbuilder');
const { DialogSet, NumberPrompt, TextPrompt, WaterfallDialog } = require('botbuilder-dialogs');
const { SlotFillingDialog } = require('./slotFillingDialog');
const { SlotDetails } = require('./slotDetails');
const DIALOG_STATE_PROPERTY = 'dialogState';
class SampleBot {
/**
* SampleBot defines the core business logic of this bot.
* @param {ConversationState} conversationState A ConversationState object used to store dialog state.
*/
constructor(conversationState) {
this.conversationState = conversationState;
// Create a property used to store dialog state.
// See https://aka.ms/about-bot-state-accessors to learn more about bot state and state accessors.
this.dialogState = this.conversationState.createProperty(DIALOG_STATE_PROPERTY);
// Create a dialog set to include the dialogs used by this bot.
this.dialogs = new DialogSet(this.dialogState);
// Set up a series of questions for collecting the user's name.
const fullnameSlots = [
new SlotDetails('first', 'text', 'Please enter your first name.'),
new SlotDetails('last', 'text', 'Please enter your last name.')
];
// Set up a series of questions to collect a street address.
const addressSlots = [
new SlotDetails('street', 'text', 'Please enter your street address.'),
new SlotDetails('city', 'text', 'Please enter the city.'),
new SlotDetails('zip', 'text', 'Please enter your zipcode.')
];
// Link the questions together into a parent group that contains references
// to both the fullname and address questions defined above.
const slots = [
new SlotDetails('fullname', 'fullname'),
new SlotDetails('age', 'number', 'Please enter your age.'),
new SlotDetails('shoesize', 'shoesize', 'Please enter your shoe size.', 'You must enter a size between 0 and 16. Half sizes are acceptable.'),
new SlotDetails('address', 'address')
];
// Add the individual child dialogs and prompts used.
// Note that the built-in prompts work hand-in-hand with our custom SlotFillingDialog class
// because they are both based on the provided Dialog class.
this.dialogs.add(new SlotFillingDialog('address', addressSlots));
this.dialogs.add(new SlotFillingDialog('fullname', fullnameSlots));
this.dialogs.add(new TextPrompt('text'));
this.dialogs.add(new NumberPrompt('number'));
this.dialogs.add(new NumberPrompt('shoesize', this.shoeSizeValidator));
this.dialogs.add(new SlotFillingDialog('slot-dialog', slots));
// Finally, add a 2-step WaterfallDialog that will initiate the SlotFillingDialog,
// and then collect and display the results.
this.dialogs.add(new WaterfallDialog('root', [
this.startDialog.bind(this),
this.processResults.bind(this)
]));
}
// This is the first step of the WaterfallDialog.
// It kicks off the dialog with the multi-question SlotFillingDialog,
// then passes the aggregated results on to the next step.
async startDialog(step) {
return await step.beginDialog('slot-dialog');
}
// This is the second step of the WaterfallDialog.
// It receives the results of the SlotFillingDialog and displays them.
async processResults(step) {
// Each "slot" in the SlotFillingDialog is represented by a field in step.result.values.
// The complex that contain subfields have their own .values field containing the sub-values.
const values = step.result.values;
const fullname = values['fullname'].values;
await step.context.sendActivity(`Your name is ${ fullname['first'] } ${ fullname['last'] }.`);
await step.context.sendActivity(`You wear a size ${ values['shoesize'] } shoes.`);
const address = values['address'].values;
await step.context.sendActivity(`Your address is: ${ address['street'] }, ${ address['city'] } ${ address['zip'] }`);
return await step.endDialog();
}
// Validate that the provided shoe size is between 0 and 16, and allow half steps.
// This is used to instantiate a specialized NumberPrompt.
async shoeSizeValidator(prompt) {
if (prompt.recognized.succeeded) {
const shoesize = prompt.recognized.value;
// Shoe sizes can range from 0 to 16.
if (shoesize >= 0 && shoesize <= 16) {
// We only accept round numbers or half sizes.
if (Math.floor(shoesize) === shoesize || Math.floor(shoesize * 2) === shoesize * 2) {
// Indicate success.
return true;
}
}
}
return false;
}
/**
*
* @param {TurnContext} turnContext A TurnContext object representing an incoming message to be handled by the bot.
*/
async onTurn(turnContext) {
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
if (turnContext.activity.type === ActivityTypes.Message) {
// Create dialog context.
const dc = await this.dialogs.createContext(turnContext);
const utterance = (turnContext.activity.text || '').trim().toLowerCase();
if (utterance === 'cancel') {
if (dc.activeDialog) {
await dc.cancelAllDialogs();
await dc.context.sendActivity(`Ok... canceled.`);
} else {
await dc.context.sendActivity(`Nothing to cancel.`);
}
}
if (!dc.context.responded) {
// Continue the current dialog if one is pending.
await dc.continueDialog();
}
if (!dc.context.responded) {
// If no response has been sent, start the onboarding dialog.
await dc.beginDialog('root');
}
} else if (
turnContext.activity.type === ActivityTypes.ConversationUpdate &&
turnContext.activity.membersAdded[0].name !== 'Bot'
) {
// Send a "this is what the bot does" message.
const description = [
'This is a bot that demonstrates an alternate dialog system',
'which uses a slot filling technique to collect multiple responses from a user.',
'Say anything to continue.'
];
await turnContext.sendActivity(description.join(' '));
}
await this.conversationState.saveChanges(turnContext);
}
}
module.exports.SampleBot = SampleBot;