forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
139 lines (121 loc) · 5.15 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
'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! I'm a sample bot showing how multiple dialogs work.`);
session.send(`Let's start the first dialog, which will ask you your name.`);
// 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('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 age = session.privateConversationData.age = results.response;
const name = session.privateConversationData.name;
session.endConversation(`Hello ${name}. You are ${age}`);
} 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('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 || age < 13 || age > 90) {
// 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 });
}
}
]);