forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.js
146 lines (128 loc) · 5.36 KB
/
address.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
var builder = require('botbuilder');
var locationDialog = require('botbuilder-location');
var lib = new builder.Library('address');
// Register BotBuilder-Location dialog
lib.library(locationDialog.createLibrary(process.env.BING_MAPS_KEY));
// Main request address dialog, invokes BotBuilder-Location
lib.dialog('/', [
function (session, args) {
// Ask for address
args = args || {};
var promptMessage = args.promptMessage || 'default_address_prompt';
session.dialogData.promptMessage = promptMessage;
// Use botbuilder-location dialog for address request
var options = {
prompt: promptMessage,
useNativeControl: true,
reverseGeocode: true,
skipConfirmationAsk: true,
requiredFields:
locationDialog.LocationRequiredFields.streetAddress |
locationDialog.LocationRequiredFields.locality |
locationDialog.LocationRequiredFields.country
};
locationDialog.getLocation(session, options);
},
function (session, results) {
if (results.response) {
// Return selected address to previous dialog in stack
var place = results.response;
var address = locationDialog.getFormattedAddressFromPlace(place, ', ');
session.endDialogWithResult({
address: address
});
} else {
// No address resolved, restart
session.replaceDialog('/', { promptMessage: session.dialogData.promptMessage });
}
}]);
// Request Billing Address
// Prompt/Save selected address. Uses previous dialog to request and validate address.
var UseSavedInfoChoices = {
Home: 'home_address',
Work: 'work_address',
NotThisTime: 'not_this_time'
};
lib.dialog('billing', [
function (session, args, next) {
var selection = session.message.text;
var saved = session.userData.billingAddresses = session.userData.billingAddresses || {};
if (hasAddresses(saved)) {
// Saved data found, check for selection
if (selection && saved[selection]) {
// Retrieve selection
var savedAddress = saved[selection];
session.dialogData.billingAddress = savedAddress;
next();
} else if (selection.toLowerCase() === session.gettext(UseSavedInfoChoices.NotThisTime).toLowerCase()) {
// Ask for data
next();
} else {
// No selection, prompt which saved address to use
session.send('select_billing_address');
var message = new builder.Message(session)
.attachmentLayout(builder.AttachmentLayout.carousel);
var homeAddress = saved[session.gettext(UseSavedInfoChoices.Home)];
var workAddress = saved[session.gettext(UseSavedInfoChoices.Work)];
if (homeAddress) message.addAttachment(createAddressCard(session, session.gettext(UseSavedInfoChoices.Home), homeAddress));
if (workAddress) message.addAttachment(createAddressCard(session, session.gettext(UseSavedInfoChoices.Work), workAddress));
message.addAttachment(createAddressCard(session, session.gettext(UseSavedInfoChoices.NotThisTime), 'add_new_address'));
session.send(message);
}
} else {
// No saved addresses data
next();
}
},
function (session, args, next) {
if (session.dialogData.billingAddress) {
// Address selected in previous step, skip
return next();
}
// Ask for address
session.beginDialog('/',
{
promptMessage: session.gettext('ask_billing_address')
});
},
function (session, args, next) {
if (session.dialogData.billingAddress) {
return next();
}
// Retrieve address from previous dialog
session.dialogData.billingAddress = args.address;
// Ask to save address
builder.Prompts.choice(session, 'ask_save_address', [
session.gettext(UseSavedInfoChoices.Home),
session.gettext(UseSavedInfoChoices.Work),
session.gettext(UseSavedInfoChoices.NotThisTime)
]);
},
function (session, args, next) {
var billingAddress = session.dialogData.billingAddress;
if (args.response && args.response.entity !== session.gettext(UseSavedInfoChoices.NotThisTime)) {
// Save address
session.userData.billingAddresses = session.userData.billingAddresses || {};
session.userData.billingAddresses[args.response.entity] = billingAddress;
}
// Return address
session.endDialogWithResult({ billingAddress: billingAddress });
}
]);
// Helpers
function hasAddresses(addresses) {
return addresses[UseSavedInfoChoices.Home] || addresses[UseSavedInfoChoices.Work];
}
function createAddressCard(session, buttonTitle, address) {
return new builder.HeroCard(session)
.title(buttonTitle)
.subtitle(address)
.buttons([
builder.CardAction.imBack(session, buttonTitle, buttonTitle)
]);
}
module.exports.UseSavedInfoChoices = UseSavedInfoChoices;
// Export createLibrary() function
module.exports.createLibrary = function () {
return lib.clone();
};