-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.py
339 lines (270 loc) · 10.4 KB
/
lambda.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
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
import os
import logging
import json
import boto3
import time
import dateutil.parser
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# -- Helpers For Response Builders --
# -- ElicitSlot — Informs Amazon Lex that the user is expected to provide a slot value in the response.
def elicit_slot(session_attributes, intent_name, slots, slot_to_elicit, message):
return{
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'ElicitSlot',
'intentName': intent_name,
'slots': slots,
'slotToElicit': slot_to_elicit,
'message': message
}
}
# -- ConfirmIntent — Informs Amazon Lex that the user is expected to give a yes or no answer to confirm or deny the current intent.
def confirm_intent(session_attributes, intent_name, slots, message):
return {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'ConfirmIntent',
'intentName': intent_name,
'slots': slots,
'message': message
}
}
# -- Close — Informs Amazon Lex not to expect a response from the user. For example, "Your pizza order has been placed" does not require a response.
def close(session_attributes, fulfillment_state, message):
response = {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Close',
'fulfillmentState': fulfillment_state,
'message': message
}
}
return response
# -- Delegate — Directs Amazon Lex to choose the next course of action based on the bot configuration.
def delegate(session_attrubutes, slots):
return {
'sessionAttributes': session_attrubutes,
'dialogAction': {
'type': 'Delegate',
'slots': slots
}
}
# --- Misc Helper Functions ---
def safe_int(n):
"""
Safely converting n value to integer.
"""
if n is not None:
return int(n)
#console.log('Error')
return n
def isvalid_date(date):
"""
Safely checking parsed date to be True.
"""
try:
dateutil.parser.parse(date)
return True
except ValueError:
return False
def build_validation_result(is_valid, violated_slot, message_content):
"""
Making result for Validation
"""
return {
'isValid': is_valid,
'violatedSlot': violated_slot,
'message': {
'contentType': 'PlainText',
'content': message_content
}
}
def isvalid_qtype(qtype):
"""
Validation for user created Slot 'Quote Type' - qtype.
"""
qtype = ['liability insurance', 'collision coverage',
'comprehensive coverage', 'personal injury protection',
'underinsured motorist protection']
return qtype.lower() in car_type
def isvalid_add(street, city):
"""
Validation for user inputed address and street information.
"""
auth_id = "14324411-3afb-6b5f-8ba0-79b4c7a26694"
auth_token = "rkJ6XCrMNo5JbuVAkmqp"
credentials = StaticCredentials(auth_id, auth_token)
client = ClientBuilder(credentials).build_us_street_api_client()
lookup = Lookup()
lookup.street = street
look.city = city
lookup.state = "MD"
try:
client.send_lookup(lookup)
except exceptions.SmartyException as err:
print(err)
return
result = lookup.result
if not result:
print("No candidates. This means the address is not valid.")
return False
first_candidate = result[0]
print("Address is valid. (There is at least one candidate)\n")
print("ZIP Code: " + first_candidate.components.zipcode)
print("County: " + first_candidate.metadata.county_name)
print("Latitude: {}".format(first_candidate.metadata.latitude))
print("Longitude: {}".format(first_candidate.metadata.longitude))
qwe = first_candidate.components.zipcode
if qwe is not None:
return True
else:
return False
def build_validation_result(isvalid, violated_slot, message_content):
"""
Function for intermediate validation building and processing.
"""
return {
'isValid': isvalid,
'violatedSlot': violated_slot,
'message': {'contentType': 'PlainText', 'content': message_content}
}
def validate_values(qtype, caddress, cname, clast, city):
"""
Function for intermediate validation building and processing.
"""
if qtype and not isvalid_qtype(qtype):
return build_validation_result(False, 'QType', 'I did not recognize that, can you please provide me with a valid insurance type?')
if caddress and city:
if isvalid_add('caddress', 'city') is False:
return build_validation_result(False, 'CAddress', 'This is an invalid Address, Kindly input correct Address')
return build_validation_result(True, None, None)
def validate_quote(slots):
"""
Function for intermediate validation for Slots and Lex Contents.
"""
caddres = intent_request['currentIntent']['slots']['CAddress']
cname = intent_request['currentIntent']['slots']['CName']
clast = intent_request['currentIntent']['slots']['CLast']
qtype = intent_request['currentIntent']['slots']['QType']
city = intent_request['currentIntent']['slots']['City']
source = intent_request['invocationSource']
output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {
}
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slots.
slots = intent_request['currentIntent']['slots']
validation_result = validate_values(qtype, caddress, cname, clast, city)
if not validation_result['isValid']:
slots[validation_result['violatedSlot']] = None
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
slots,
validation_result['violatedSlot'],
validation_result['message'],
build_response_card(
'Specify {}'.format(validation_result['violatedSlot']),
validation_result['message']['content'],
build_options(validation_result['violatedSlot'],
appointment_type, date, booking_map)
)
)
if not caddress:
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
intent_request['currentIntent']['slots'],
'CAddress',
{'contentType': 'PlainText',
'content': 'Please specify a proper address for quotation evaluation'},
build_response_card(
'Specify Address', 'Please specify a proper address for quotation evaluation',
build_options('CAddress', caddress, date, None)
)
)
if qtype and not isvalid_quote_type(qtype):
return build_validation_result(
False,
'QType',
'We currently do not support {} as a valid insurance type. Can you try a different type?'.format(qtype))
def get_quote(intent_request):
"""
Function for processing quote and retrieving the user information.
"""
caddres = try_ex(lambda: slots['CAddress'])
cname = try_ex(lambda: slots['CName'])
clast = try_ex(lambda: slots['CLast'])
qtype = try_ex(lambda: slots['QType'])
city = try_ex(lambda: slots['City'])
session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {
}
# Load confirmation history and track the current reservation.
reservation = json.dumps({
'QType': qtype,
'CAddress': caddres,
'CName': cname,
'CLast': clast,
'City': city
})
session_attributes['currentReservation'] = reservation
# Validate any slots which have been specified. If any are invalid, re-elicit for their value
if intent_request['invocationSource'] == 'DialogCodeHook':
validation_result = validate_quote(intent_request['currentIntent']['slots'])
if not validation_result['isValid']:
slots = intent_request['currentIntent']['slots']
slots[validation_result['violatedSlot']] = None
return elicit_slot(
session_attributes,
intent_request['currentIntent']['name'],
slots,
validation_result['violatedSlot'],
validation_result['message']
)
def greetings(intent_request):
"""
Function for running the task list attested for the intent greetings.
"""
cname = try_ex(lambda: intent_request['currentIntent']['slots']['CName'].title())
intent = intent_request['currentIntent']['name']
slots = intent_request['currentIntent']['slots']
session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {
}
reservation = json.dumps({
'CName': cname,
})
session_attributes['currentReservation'] = reservation
logger.info("Hey Thisis is a test")
logger.info("This is the current Intent: " + slots)
logger.info("Intent Name : ")
logger.info(intent)
logger.info("Name : ")
logger.info(cname)
logger.info("Slots is :")
logger.info(slots)
return delegate(session_attributes, intent_request['currentIntent']['slots'])
def dispatch(intent_request):
"""
Called when the user specifies an intent for this bot.
"""
logger.debug('dispatch userId={}, intentName={}'.format(
intent_request['userId'], intent_request['currentIntent']['name']))
intent_name = intent_request['currentIntent']['name']
# Dispatch to your bot's intent handlers
"""
Invocation for Calling Greeting Intent.
"""
if intent_name == 'Greetings':
return greetings(intent_request)
elif intent_name == 'Info':
return get_quote(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
def lambda_handler(event, context):
"""
Route the incoming request based on intent.
The JSON body of the request is provided in the event slot.
"""
os.environ['TZ'] = 'America/New_York'
time.tzset()
logger.debug('event.bot.name={}'.format(event['bot']['name']))
return dispatch(event)