-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
199 lines (174 loc) · 7.34 KB
/
bot.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
const builder = require('botbuilder');
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// In a bot, a conversation can hold a collection of dialogs.
// Each dialog is designed to be a self-contained unit that can
// perform an action that might take multiple steps, such as collecting
// information from a user or performing an action on her behalf.
const bot = module.exports = new builder.UniversalBot(connector, [
// this section becomes the root dialog
// If a conversation hasn't been started, and the message
// sent by the user doesn't match a pattern, the
// conversation will start here
(session, args, next) => {
session.send(`Hi there!`);
session.send(`Let's get to know you.`);
// Launch the getName dialog using beginDialog
// When beginDialog completes, control will be passed
// to the next function in the waterfall
session.beginDialog('getName');
},
(session, results, next) => {
// executed when getName dialog completes
// results parameter contains the object passed into endDialogWithResults
// check for a response
if (results.response) {
const name = session.privateConversationData.name = results.response;
// When calling another dialog, you can pass arguments in the second parameter
session.beginDialog('getGender', { name: name });
} else {
// no valid response received - End the conversation
session.endConversation(`Sorry, I didn't understand the response. Let's start over.`);
}
},
(session, results, next) => {
// executed when getName dialog completes
// results parameter contains the object passed into endDialogWithResults
// check for a response
if (results.response) {
const name = session.privateConversationData.name;
const gender = session.privateConversationData.gender = results.response;
// When calling another dialog, you can pass arguments in the second parameter
session.beginDialog('getAge', { name: name });
} else {
// no valid response received - End the conversation
session.endConversation(`Sorry, I didn't understand the response. Let's start over.`);
}
},
(session, results, next) => {
// executed when getAge dialog completes
// results parameter contains the object passed into endDialogWithResults
// check for a response
if (results.response) {
const gender = session.privateConversationData.gender;
const name = session.privateConversationData.name;
const age = session.privateConversationData.age = results.response;
session.endConversation(`Hello ${name}. You are ${gender}. Your age is ${age} years.`);
} else {
// no valid response received - End the conversation
session.endConversation(`Sorry, I didn't understand the response. Let's start over.`);
}
},
]);
bot.dialog('getName', [
(session, args, next) => {
// store reprompt flag
if(args) {
session.dialogData.isReprompt = args.isReprompt;
}
// prompt user
builder.Prompts.text(session, 'What is your name?');
},
(session, results, next) => {
const name = results.response;
if (!name || name.trim().length < 3) {
// Bad response. Logic for single re-prompt
if (session.dialogData.isReprompt) {
// Re-prompt ocurred
// Send back empty string
session.endDialogWithResult({ response: '' });
} else {
// Set the flag
session.send('Sorry, name must be at least 3 characters.');
// Call replaceDialog to start the dialog over
// This will replace the active dialog on the stack
// Send a flag to ensure we only reprompt once
session.replaceDialog('getName', { isReprompt: true });
}
} else {
// Valid name received
// Return control to calling dialog
// Pass the name in the response property of results
session.endDialogWithResult({ response: name.trim() });
}
}
]);
bot.dialog('getGender', [
(session, args, next) => {
let name = session.dialogData.name = 'User';
if (args) {
// store reprompt flag
session.dialogData.isReprompt = args.isReprompt;
// retrieve name
name = session.dialogData.name = args.name;
}
// prompt user
builder.Prompts.text(session, `What is your gender, ${name}?`);
},
(session, results, next) => {
const gender = results.response;
// Basic validation - did we get a response?
if (!gender) {
// Bad response. Logic for single re-prompt
if (session.dialogData.isReprompt) {
// Re-prompt ocurred
// Send back empty string
session.endDialogWithResult({ response: '' });
} else {
// Set the flag
session.dialogData.didReprompt = true;
session.send(`Sorry, please type male or female!`);
// Call replaceDialog to start the dialog over
// This will replace the active dialog on the stack
session.replaceDialog('getGender',
{ name: session.dialogData.name, isReprompt: true });
}
} else {
// Valid city received
// Return control to calling dialog
// Pass the city in the response property of results
session.endDialogWithResult({ response: gender });
}
}
]);
bot.dialog('getAge', [
(session, args, next) => {
let name = session.dialogData.name = 'User';
if (args) {
// store reprompt flag
session.dialogData.isReprompt = args.isReprompt;
// retrieve name
name = session.dialogData.name = args.name;
}
// prompt user
builder.Prompts.number(session, `How old are you, ${name}?`);
},
(session, results, next) => {
const age = results.response;
// Basic validation - did we get a response?
if (!age) {
// Bad response. Logic for single re-prompt
if (session.dialogData.isReprompt) {
// Re-prompt ocurred
// Send back empty string
session.endDialogWithResult({ response: '' });
} else {
// Set the flag
session.dialogData.didReprompt = true;
session.send(`Sorry, that doesn't look right.`);
// Call replaceDialog to start the dialog over
// This will replace the active dialog on the stack
session.replaceDialog('getAge',
{ name: session.dialogData.name, isReprompt: true });
}
} else {
// Valid city received
// Return control to calling dialog
// Pass the city in the response property of results
session.endDialogWithResult({ response: age });
}
}
]);