forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
270 lines (250 loc) · 9.96 KB
/
app.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*-----------------------------------------------------------------------------
Roller is a dice rolling skill that's been optimized for speech.
-----------------------------------------------------------------------------*/
var restify = require('restify');
var builder = require('botbuilder');
var ssml = require('./ssml');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
/**
* Create your bot with a function to receive messages from the user.
* - This function will be called anytime the users utterance isn't
* recognized.
*/
var bot = new builder.UniversalBot(connector, function (session) {
// Just redirect to our 'HelpDialog'.
session.replaceDialog('HelpDialog');
});
/**
* This dialog sets up a custom game for the bot to play. It will
* ask the user how many sides they want the dice to have and then
* how many should be rolled. Once it's built up the game structure
* it will pass it to a seperate 'PlayGameDialog'.
*
* We've added a triggerAction() to this dialog that lets a user say
* something like "I'd like to roll some dice" to start the dialog.
* We're using a RegEx to match the users input but we could just as
* easily use a LUIS intent.
*/
bot.dialog('CreateGameDialog', [
function (session) {
// Initialize game structure.
// - dialogData gives us temporary storage of this data in between
// turns with the user.
var game = session.dialogData.game = {
type: 'custom',
sides: null,
count: null,
turns: 0
};
/**
* Ask for the number of sides.
*
* You can pass an array of choices to be matched. These will be shown as a
* numbered list by default. This list will be shown as a numbered list which
* is what we want since we have so many options.
*
* - value is what you want returned via 'results.response.entity' when selected.
* - action lets you customize the displayed label and for buttons what get sent when clicked.
* - synonyms let you specify alternate things to recognize for a choice.
*/
var choices = [
{ value: '4', action: { title: '4 Sides' }, synonyms: 'four|for|4 sided|4 sides' },
{ value: '6', action: { title: '6 Sides' }, synonyms: 'six|sex|6 sided|6 sides' },
{ value: '8', action: { title: '8 Sides' }, synonyms: 'eight|8 sided|8 sides' },
{ value: '10', action: { title: '10 Sides' }, synonyms: 'ten|10 sided|10 sides' },
{ value: '12', action: { title: '12 Sides' }, synonyms: 'twelve|12 sided|12 sides' },
{ value: '20', action: { title: '20 Sides' }, synonyms: 'twenty|20 sided|20 sides' },
];
builder.Prompts.choice(session, 'choose_sides', choices, {
speak: speak(session, 'choose_sides_ssml')
});
},
function (session, results) {
// Store users input
// - The response comes back as a find result with index & entity value matched.
var game = session.dialogData.game;
game.sides = Number(results.response.entity);
/**
* Ask for number of dice.
*
* - We can use gettext() to format a string using a template stored in our
* localized prompts file.
* - The number prompt lets us pass additional options to say we only want
* integers back and what's the min & max value that's allowed.
*/
var prompt = session.gettext('choose_count', game.sides);
builder.Prompts.number(session, prompt, {
speak: speak(session, 'choose_count_ssml'),
minValue: 1,
maxValue: 100,
integerOnly: true
});
},
function (session, results) {
// Store users input
// - The response is already a number.
var game = session.dialogData.game;
game.count = results.response;
/**
* Play the game we just created.
*
* We can use replaceDialog() to end the current dialog and start a new
* one in its place. We can pass arguments to dialogs so we'll pass the
* 'PlayGameDialog' the game we created.
*/
session.replaceDialog('PlayGameDialog', { game: game });
}
]).triggerAction({ matches: [
/(roll|role|throw|shoot).*(dice|die|dye|bones)/i,
/new game/i
]});
/**
* This dialog is our main game loop. We'll store the game structure in
* session.conversationData so that should the user say "roll again" we
* can just re-roll the same set of dice again.
*/
bot.dialog('PlayGameDialog', function (session, args) {
// Get current or new game structure.
var game = args.game || session.conversationData.game;
if (game) {
// Generate rolls
var total = 0;
var rolls = [];
for (var i = 0; i < game.count; i++) {
var roll = Math.floor(Math.random() * game.sides) + 1;
if (roll > game.sides) {
// Accounts for 1 in a million chance random() generated a 1.0
roll = game.sides;
}
total += roll;
rolls.push(roll);
}
// Format roll results
var results = '';
var multiLine = rolls.length > 5;
for (var i = 0; i < rolls.length; i++) {
if (i > 0) {
results += ' . ';
}
results += rolls[i];
}
// Render results using a card
var card = new builder.HeroCard(session)
.subtitle(game.count > 1 ? 'card_subtitle_plural' : 'card_subtitle_singular', game)
.buttons([
builder.CardAction.imBack(session, 'roll again', 'Roll Again'),
builder.CardAction.imBack(session, 'new game', 'New Game')
]);
if (multiLine) {
//card.title('card_title').text('\n\n' + results + '\n\n');
card.text(results);
} else {
card.title(results);
}
var msg = new builder.Message(session).addAttachment(card);
// Determine bots reaction for speech purposes
var reaction = 'normal';
var min = game.count;
var max = game.count * game.sides;
var score = total/max;
if (score == 1.0) {
reaction = 'best';
} else if (score == 0) {
reaction = 'worst';
} else if (score <= 0.3) {
reaction = 'bad';
} else if (score >= 0.8) {
reaction = 'good';
}
// Check for special craps rolls
if (game.type == 'craps') {
switch (total) {
case 2:
case 3:
case 12:
reaction = 'craps_lose';
break;
case 7:
reaction = 'craps_seven';
break;
case 11:
reaction = 'craps_eleven';
break;
default:
reaction = 'craps_retry';
break;
}
}
// Build up spoken response
var spoken = '';
if (game.turn == 0) {
spoken += session.gettext('start_' + game.type + '_game_ssml') + ' ';
}
spoken += session.gettext(reaction + '_roll_reaction_ssml');
msg.speak(ssml.speak(spoken));
// Incrment number of turns and store game to roll again
game.turn++;
session.conversationData.game = game;
/**
* Send card and bots reaction to user.
*/
msg.inputHint(builder.InputHint.acceptingInput);
session.send(msg).endDialog();
} else {
// User started session with "roll again" so let's just send them to
// the 'CreateGameDialog'
session.replaceDialog('CreateGameDialog');
}
}).triggerAction({ matches: /(roll|role|throw|shoot) again/i });
/**
* Listen for the user to ask to play craps.
*
* While you can use a triggerAction() to start a dialog, you sometimes just want
* to either send a message when a user says something or start an existing dialog
* with some arguments. You can use a cusomAction() to recognize something the user
* says without tampering with the dialog stack. In our case what we want to do is
* call 'PlayGameDialog' with a pre-defined game structure.
*/
bot.customAction({
matches: /(play|start).*(craps)/i,
onSelectAction: function (session, args, next) {
// The user could be in another dialog so clear the dialog stack first
// to make sure we end that task.
session.clearDialogStack().beginDialog('PlayGameDialog', {
game: { type: 'craps', sides: 6, count: 2, turn: 0 }
})
}
});
/**
* Every bot should have a help dialog. Ours will use a card with some buttons
* to educate the user with the options available to them.
*/
bot.dialog('HelpDialog', function (session) {
var card = new builder.HeroCard(session)
.title('help_title')
.buttons([
builder.CardAction.imBack(session, 'roll some dice', 'Roll Dice'),
builder.CardAction.imBack(session, 'play craps', 'Play Craps')
]);
var msg = new builder.Message(session)
.speak(speak(session, 'help_ssml'))
.addAttachment(card)
.inputHint(builder.InputHint.acceptingInput);
session.send(msg).endDialog();
}).triggerAction({ matches: /help/i });
/** Helper function to wrap SSML stored in the prompts file with <speak/> tag. */
function speak(session, prompt) {
var localized = session.gettext(prompt);
return ssml.speak(localized);
}