forked from aws-samples/amazon-lex-connect-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyPersonalBanker_v1.js
executable file
·147 lines (129 loc) · 4.24 KB
/
myPersonalBanker_v1.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
const AWS = require('aws-sdk');
// Route the incoming request based on intent.
const accounts = [
{
type: 'current',
pinNumber: '1234',
balance: '134.12',
},
{
type: 'saving',
pinNumber: '1234',
balance: '154.12',
},
];
// Check to see if an account matches for the account type and also that the pin number for that
// account matches the array
const getAccount = (type, pinNumber) => {
const result = accounts.find(account => account.type.toLowerCase() === type.toLowerCase()
&& account.pinNumber === pinNumber);
return result;
};
const balanceIntent = (intentRequest, callback) => {
const { slots } = intentRequest.currentIntent;
const sessionAttributes = { ...intentRequest.sessionAttributes, ...slots };
const account = getAccount(slots.AccountType, slots.PinNumber);
if (!account) {
callback(elicitSlot(sessionAttributes, 'GetBalanceCheck', slots, 'PinNumber', {
contentType: 'PlainText',
content: 'No accounts have been found with this pin number please re-enter your pin number',
}));
} else {
callback(close(sessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: `You have £${account.balance} in your account`,
}));
}
};
const balanceIntentError = (intentRequest, callback) => {
const { slots } = intentRequest.currentIntent;
const sessionAttributes = { ...intentRequest.sessionAttributes, ...slots };
const account = getAccount(slots.AccountType, slots.PinNumber);
callback(close(sessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: `You have £${account.balance} in your account, is there anything can help you with?`,
}));
};
// This a simple example to demonstrate how lambda can work with the flow
const simpleResponse = (intentRequest, callback) => {
const { slots } = intentRequest.currentIntent;
const $msg = `Thank you for using the lambda function.
You submitted the following values AccountType:${slots.AccountType}
pinNumber ${slots.PinNumber}. Now it's time to make it actually do something!`;
callback(close({}, 'Fulfilled', {
contentType: 'PlainText',
content: $msg,
}));
};
// Called when the user specifies an intent for this skill.
const dispatch = (intentRequest, callback) => {
console.log(JSON.stringify(intentRequest, null, 2));
console.log(`dispatch userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);
const intentName = intentRequest.currentIntent.name;
if (intentName === 'GetBalanceCheck') {
return simpleResponse(intentRequest, callback);
// return balanceIntentError(intentRequest, callback);
// return balanceIntent(intentRequest, callback);
}
return {};
};
const loggingCallback = (response, originalCallback) => {
console.log('lambda response:\n', JSON.stringify(response, null, 2));
originalCallback(null, response);
};
// The handler function is the one that gets called by lambda as it is invoked
exports.handler = (event, context, callback) => {
try {
console.log(`event.bot.name=${event.bot.name}`);
dispatch(event, response => loggingCallback(response, callback));
} catch (err) {
callback(err);
}
};
// --------------- Helpers that build all of the responses -----------------------
// continue dialog with the customer, expressing that the user will select another intent after
// she hears this response
const nextIntent = (sessionAttributes, message) => {
console.log(`nextIntent: ${JSON.stringify(message)}`);
return {
sessionAttributes,
dialogAction: {
type: 'ElicitIntent',
message,
},
};
};
const elicitSlot = (sessionAttributes, intentName, slots, slotToElicit, message) => ({
sessionAttributes,
dialogAction: {
type: 'ElicitSlot',
intentName,
slots,
slotToElicit,
message,
},
});
const confirmIntent = (sessionAttributes, intentName, slots, message) => ({
sessionAttributes,
dialogAction: {
type: 'ConfirmIntent',
intentName,
slots,
message,
},
});
const close = (sessionAttributes, fulfillmentState, message) => ({
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
});
const delegate = (sessionAttributes, slots) => ({
sessionAttributes,
dialogAction: {
type: 'Delegate',
slots,
},
});