-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextwork-banker-bot.yaml
411 lines (386 loc) · 16.5 KB
/
nextwork-banker-bot.yaml
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: |-
Sample Lex V2 Banker Bot using CloudFormation Custom Resource
Parameters:
SarAppArn:
Description: >-
ARN of SAR Lex CloudFormation Custom Resource application
Type: String
Default: arn:aws:serverlessrepo:us-east-1:777566285978:applications/lex-v2-cfn-cr
SarAppVersion:
Description: >-
Semantic version of SAR Lex CloudFormation Custom Resource application
Type: String
Default: 0.3.0
Globals:
Function:
Runtime: python3.12
Resources:
# this deploys the Custom Resource as a nested CloudFormation stack
LexV2CfnCr:
Type: AWS::Serverless::Application
Properties:
Location:
ApplicationId: !Ref SarAppArn
SemanticVersion: !Ref SarAppVersion
Parameters:
# Custom Resource Lambda log level
LogLevel: DEBUG
LexBot:
Type: Custom::LexBot
Properties:
ServiceToken: !GetAtt LexV2CfnCr.Outputs.LexV2CfnCrFunctionArn
botName: !Sub "${AWS::StackName}-BankerBot"
dataPrivacy:
childDirected: False
description: Example Banker bot to demonstrate Lex V2 capabilities
idleSessionTTLInSeconds: 300
roleArn: !GetAtt LexV2CfnCr.Outputs.LexServiceLinkedRole
CR.botLocales:
- localeId: en_US
nluIntentConfidenceThreshold: 0.40
voiceSettings:
voiceId: Ivy
CR.slotTypes:
- slotTypeName: accountType
valueSelectionSetting:
resolutionStrategy: TopResolution
slotTypeValues:
- sampleValue:
value: Checking
- sampleValue:
value: Savings
- sampleValue:
value: Credit
synonyms:
- value: credit card
- value: visa
- value: mastercard
- value: amex
- value: american express
CR.intents:
- intentName: FallbackIntent
description: Default fallback intent when no other intent matches
intentClosingSetting:
closingResponse:
messageGroups:
- message:
plainTextMessage:
value: >-
Sorry I am having trouble understanding.
Can you describe what you'd like to do in a few
words? I can help you find your account balance,
transfer funds and make a payment.
- intentName: Welcome
description: Welcome intent
sampleUtterances:
- utterance: Hi
- utterance: Hello
- utterance: I need help
- utterance: Can you help me?
intentClosingSetting:
closingResponse:
messageGroups:
- message:
plainTextMessage:
value: >-
Hi! I'm BB, the Banking Bot. How can I help you
today?
- intentName: CheckBalance
description:
Intent to check the balance in the specified account type
sampleUtterances:
- utterance: What’s the balance in my account ?
- utterance: Check my account balance
- utterance: What’s the balance in my {accountType} account ?
- utterance: How much do I have in {accountType} ?
- utterance: I want to check the balance
- utterance: Can you help me with account balance ?
- utterance: Balance in {accountType}
fulfillmentCodeHook:
enabled: True
outputContexts:
- name: contextCheckBalance
timeToLiveInSeconds: 90
turnsToLive: 5
CR.slots:
- slotName: accountType
CR.slotTypeName: accountType
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value:
For which account would you like your balance?
maxRetries: 2
- slotName: dateOfBirth
CR.slotTypeName: AMAZON.Date
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value: >-
For verification purposes, what is your date of
birth?
maxRetries: 2
- intentName: FollowupCheckBalance
description: >-
Intent to allow a follow-up balance check request without
authentication
sampleUtterances:
- utterance: How about my {accountType} account
- utterance: What about {accountType}
- utterance: And in {accountType} ?
fulfillmentCodeHook:
enabled: True
inputContexts:
- name: contextCheckBalance
CR.slots:
- slotName: accountType
CR.slotTypeName: accountType
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value:
For which account would you like your balance?
maxRetries: 2
- slotName: dateOfBirth
CR.slotTypeName: AMAZON.Date
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value: >-
For verification purposes, what is your date of
birth?
maxRetries: 2
defaultValueSpecification:
defaultValueList:
- defaultValue: '#contextCheckBalance.dateOfBirth'
- intentName: TransferFunds
description: Help user transfer funds between bank accounts
sampleUtterances:
- utterance: I want to transfer funds
- utterance: Can I make a transfer?
- utterance: I want to make a transfer
- utterance: >-
I'd like to transfer {transferAmount} from
{sourceAccountType} to {targetAccountType}
- utterance: >-
Can I transfer {transferAmount} to my {targetAccountType}
- utterance: Would you be able to help me with a transfer?
- utterance: Need to make a transfer
fulfillmentCodeHook:
enabled: False
intentConfirmationSetting:
declinationResponse:
messageGroups:
- message:
plainTextMessage:
value: The transfer has been cancelled
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value: >-
Got it. So we are transferring {transferAmount} from
{sourceAccountType} to {targetAccountType}.
Can I go ahead with the transfer?
maxRetries: 2
intentClosingSetting:
closingResponse:
messageGroups:
- message:
plainTextMessage:
value: >-
The transfer is complete. {transferAmount} should
now be available in your {targetAccountType}
account.
CR.slots:
- slotName: sourceAccountType
CR.slotTypeName: accountType
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value:
Which account would you like to transfer from?
maxRetries: 2
- slotName: targetAccountType
CR.slotTypeName: accountType
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value: Which account are you transferring to?
maxRetries: 2
- slotName: transferAmount
CR.slotTypeName: AMAZON.Number
valueElicitationSetting:
slotConstraint: Required
promptSpecification:
messageGroups:
- message:
plainTextMessage:
value: How much money would you like to transfer?
maxRetries: 2
LexBotVersion:
# Bot versions are deleted by the Bot on Stack Deletions
DeletionPolicy: Retain
# Version number changes between updates which cause a CloudFormation
# delete event since the version number is the physical resource ID.
# The following policies prevents deletion events
UpdateReplacePolicy: Retain
Type: Custom::LexBotVersion
Properties:
ServiceToken: !GetAtt LexV2CfnCr.Outputs.LexV2CfnCrFunctionArn
botId: !Ref LexBot
# botVersionLocaleSpecification is derived from the bot locales
CR.botLocaleIds: !GetAtt LexBot.botLocaleIds
CR.lastUpdatedDateTime: !GetAtt LexBot.lastUpdatedDateTime
LexBotAlias:
# Alias is deleted by the Bot on Stack Deletions
DeletionPolicy: Retain
Type: Custom::LexBotAlias
Properties:
ServiceToken: !GetAtt LexV2CfnCr.Outputs.LexV2CfnCrFunctionArn
botId: !Ref LexBot
botAliasName: live
botVersion: !Ref LexBotVersion
botAliasLocaleSettings:
en_US:
enabled: True
# Lambda Code Hook
codeHookSpecification:
lambdaCodeHook:
lambdaARN: !GetAtt LexBotFunction.Arn
codeHookInterfaceVersion: "1.0"
LexBotFunction:
Type: AWS::Serverless::Function
Properties:
Description:
!Sub "Lex Fulfillment Function for bot: ${AWS::StackName}-BankerBot"
Handler: lambda_function.lambda_handler
MemorySize: 128
Timeout: 3
InlineCode: |-
import random
import decimal
def random_num():
return(decimal.Decimal(random.randrange(1000, 50000))/100)
def get_slots(intent_request):
return intent_request['sessionState']['intent']['slots']
def get_slot(intent_request, slotName):
slots = get_slots(intent_request)
if slots is not None and slotName in slots and slots[slotName] is not None:
return slots[slotName]['value']['interpretedValue']
else:
return None
def get_session_attributes(intent_request):
sessionState = intent_request['sessionState']
if 'sessionAttributes' in sessionState:
return sessionState['sessionAttributes']
return {}
def elicit_intent(intent_request, session_attributes, message):
return {
'sessionState': {
'dialogAction': {
'type': 'ElicitIntent'
},
'sessionAttributes': session_attributes
},
'messages': [ message ] if message != None else None,
'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None
}
def close(intent_request, session_attributes, fulfillment_state, message):
intent_request['sessionState']['intent']['state'] = fulfillment_state
return {
'sessionState': {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Close'
},
'intent': intent_request['sessionState']['intent']
},
'messages': [message],
'sessionId': intent_request['sessionId'],
'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None
}
def CheckBalance(intent_request):
session_attributes = get_session_attributes(intent_request)
slots = get_slots(intent_request)
account = get_slot(intent_request, 'accountType')
#The account balance in this case is a random number
#Here is where you could query a system to get this information
balance = str(random_num())
text = "Thank you. The balance on your "+account+" account is $"+balance+" dollars."
message = {
'contentType': 'PlainText',
'content': text
}
fulfillment_state = "Fulfilled"
return close(intent_request, session_attributes, fulfillment_state, message)
def FollowupCheckBalance(intent_request):
session_attributes = get_session_attributes(intent_request)
slots = get_slots(intent_request)
account = get_slot(intent_request, 'accountType')
#The account balance in this case is a random number
#Here is where you could query a system to get this information
balance = str(random_num())
text = "Thank you. The balance on your "+account+" account is $"+balance+" dollars."
message = {
'contentType': 'PlainText',
'content': text
}
fulfillment_state = "Fulfilled"
return close(intent_request, session_attributes, fulfillment_state, message)
def dispatch(intent_request):
intent_name = intent_request['sessionState']['intent']['name']
response = None
# Dispatch to your bot's intent handlers
if intent_name == 'CheckBalance':
return CheckBalance(intent_request)
elif intent_name == 'FollowupCheckBalance':
return FollowupCheckBalance(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
def lambda_handler(event, context):
response = dispatch(event)
return response
# Add resource policy to allow the Lex Bot Alias to invoke it
LexBotFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt LexBotFunction.Arn
Principal: lexv2.amazonaws.com
SourceArn:
!Sub "arn:${AWS::Partition}:lex:${AWS::Region}:${AWS::AccountId}:\
bot-alias/*"
Outputs:
LexBotId:
Description: Lex Bot ID
Value: !Ref LexBot
LexBotLocaleIds:
Description: Lex Bot Locale IDs
Value: !Join [",", !GetAtt LexBot.botLocaleIds]
LexBotLatestVersion:
Description: Latest Lex Bot Version ID
Value: !Ref LexBotVersion
LexBotAliasId:
Description: Lex Bot Alias ID
Value: !Ref LexBotAlias