-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_funtion.py
229 lines (180 loc) · 6.94 KB
/
lambda_funtion.py
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
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import boto3
import io
import os
import json
s3 = boto3.client("s3")
s3_bucket = "lex-luthor-stor"
s3_key = "profile_data.json"
### Functionality Helper Functions ###
def parse_float(n):
"""
Securely converts a non-numeric value to float.
"""
try:
return float(n)
except ValueError:
return float("nan")
def build_validation_result(is_valid, violated_slot, message_content):
"""
Defines an internal validation message structured as a python dictionary.
"""
if message_content is None:
return {"isValid": is_valid, "violatedSlot": violated_slot}
return {
"isValid": is_valid,
"violatedSlot": violated_slot,
"message": {"contentType": "PlainText", "content": message_content},
}
def validate_data(response, intent_request):
"""
Validates the data provided by the user.
"""
# Validate the amount, it should be > 0
for rep in response:
if rep is not None:
rep = parse_float(
rep
) # Since parameters are strings it's important to cast values
if 1 > rep and rep > 4:
return build_validation_result(
False,
"rep",
"Your response should be a number between 1 and 4, "
"please provide a correct response.",
)
# A True results is returned if age or amount are valid
return build_validation_result(True, None, None)
## Dialog Actions Helper Functions ###
def get_slots(intent_request):
"""
Fetch all the slots and their values from the current intent.
"""
return intent_request["currentIntent"]["slots"]
def elicit_slot(session_attributes, intent_name, slots, slot_to_elicit, message):
"""
Defines an elicit slot type response.
"""
return {
"sessionAttributes": session_attributes,
"dialogAction": {
"type": "ElicitSlot",
"intentName": intent_name,
"slots": slots,
"slotToElicit": slot_to_elicit,
"message": message,
},
}
def delegate(session_attributes, slots):
"""
Defines a delegate slot type response.
"""
return {
"sessionAttributes": session_attributes,
"dialogAction": {"type": "Delegate", "slots": slots},
}
def close(session_attributes, fulfillment_state, message):
"""
Defines a close slot type response.
"""
response = {
"sessionAttributes": session_attributes,
"dialogAction": {
"type": "Close",
"fulfillmentState": fulfillment_state,
"message": message,
},
}
return response
### Fuction to build client's profile
def profile_constructor(intent_request):
# Gets the invocation source, for Lex dialogs "DialogCodeHook" is expected.
source = intent_request["invocationSource"]
if source == "DialogCodeHook":
# This code performs basic validation on the supplied input slots.
# Gets all the slots
slots = get_slots(intent_request)
# Validates user's input using the validate_data function
validation_result = validate_data(slots, intent_request)
# If the data provided by the user is not valid,
# the elicitSlot dialog action is used to re-prompt for the first violation detected.
if not validation_result["isValid"]:
slots[validation_result["violatedSlot"]] = None # Cleans invalid slot
# Returns an elicitSlot dialog to request new data for the invalid slot
return elicit_slot(
intent_request["sessionAttributes"],
intent_request["currentIntent"]["name"],
slots,
validation_result["violatedSlot"],
validation_result["message"],
)
# Fetch current session attributes
output_session_attributes = intent_request["sessionAttributes"]
# Once all slots are valid, a delegate dialog is returned to Lex to choose the next course of action.
return delegate(output_session_attributes, get_slots(intent_request))
# Retrieve input values from the Lambda event
name = get_slots(intent_request) ["name"]
investment_amount = get_slots(intent_request) ["investment_amount"]
date_of_birth = get_slots(intent_request) ["birthday"]
retirement_age = get_slots(intent_request) ["retirement"]
#Aggregate Risk Profile slots
risk_total = 0
for slot_name, slot_value in get_slots(intent_request).items():
if slot_name.startswith('qu_') and slot_value is not None:
risk_total += float(slot_value)
# Calculate the current age
current_date = datetime.now()
birth_date = datetime.strptime(date_of_birth, "%Y-%m-%d")
current_age = current_date.year - birth_date.year
if (current_date.month, current_date.day) < (birth_date.month, birth_date.day):
current_age -= 1
# Calculate the years until retirement and retirement date
years_until_retirement = int(retirement_age) - current_age
retirement_date = current_date + timedelta(days=years_until_retirement * 365)
# Write values to S3 bucket
profile_data = {
"name": name,
"investment_amount": int(investment_amount),
"current_age": current_age,
"years_until_retirement": years_until_retirement,
"risk_total": risk_total,
"retirement_date": retirement_date.strftime("%Y-%m-%d")
}
s3.put_object(
Body=json.dumps(profile_data),
Bucket=s3_bucket,
Key=s3_key
)
return close(
intent_request["sessionAttributes"],
"Fulfilled",
{
"contentType": "PlainText",
"content": """Thank you for your information;
you are currently {} and have {} years until you retire.
Your risk profile score is {} and
retirement date is {}.
This information has been uploaded to your profile.
We will email you your recommended portfolio and projected returns shortly.
""".format(current_age, years_until_retirement, risk_total, retirement_date.strftime("%Y-%m-%d"))
}
)
### Intents Dispatcher #####
def dispatch(intent_request):
"""
Called when the user specifies an intent for this bot.
"""
# Get the name of the current intent
intent_name = intent_request["currentIntent"]["name"]
# Dispatch to bot's intent handlers
if intent_name == "SuggestPortfolio":
return profile_constructor(intent_request)
raise Exception("Intent with name " + intent_name + " not supported")
### Main Handler ###
def lambda_handler(event, context):
"""
Route the incoming request based on intent.
The JSON body of the request is provided in the event slot.
"""
return dispatch(event)