forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
86 lines (76 loc) · 2.83 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
'use strict';
const builder = require('botbuilder');
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const bot = new builder.UniversalBot(connector, [
(session, args, next) => {
const card = new builder.ThumbnailCard(session);
card.buttons([
new builder.CardAction(session).title('Add a number').value('Add').type('imBack'),
new builder.CardAction(session).title('Get help').value('Help').type('imBack'),
]).text(`What would you like to do?`);
const message = new builder.Message(session);
message.addAttachment(card);
session.send(`Hi there! I'm the calculator bot! I can add numbers for you.`);
// we can end the conversation here
// the buttons will provide the appropriate message
session.endConversation(message);
},
]);
bot.dialog('AddNumber', [
(session, args, next) => {
let message = null;
if(!session.privateConversationData.runningTotal) {
message = `Give me the first number.`;
session.privateConversationData.runningTotal = 0;
} else {
message = `Give me the next number, or say **total** to display the total.`;
}
builder.Prompts.number(session, message, {maxRetries: 3});
},
(session, results, next) => {
if(results.response) {
session.privateConversationData.runningTotal += results.response;
session.replaceDialog('AddNumber');
} else {
session.endConversation(`Sorry, I don't understand. Let's start over.`);
}
},
])
.triggerAction({matches: /^add$/i})
.cancelAction('CancelAddNumber', 'Operation cancelled', {
matches: /^cancel$/,
onSelectAction: (session, args) => {
session.endConversation(`Operation cancelled.`);
},
confirmPrompt: `Are you sure you wish to cancel?`
})
.beginDialogAction('Total', 'Total', { matches: /^total$/})
.beginDialogAction('HelpAddNumber', 'Help', { matches: /^help$/, dialogArgs: {action: 'AddNumber'} });
bot.dialog('Total', [
(session, results, next) => {
session.endConversation(`The total is ${session.privateConversationData.runningTotal}`);
},
]);
bot.dialog('Help', [
(session, args, next) => {
let message = '';
switch(args.action) {
case 'AddNumber':
message = 'You can either type the next number, or use **total** to get the total.';
break;
default:
message = 'You can type **add** to add numbers.';
break;
}
session.endDialog(message);
}
]).triggerAction({
matches: /^help/i,
onSelectAction: (session, args) => {
session.beginDialog(args.action, args);
}
});
module.exports = bot;