Created
June 25, 2020 14:36
-
-
Save thiagoavadore/0bc61c1902d8808278176bdd5e8176a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def schedule_demo(intent_request): | |
date = intent_request['currentIntent']['slots']['Date'] | |
appointment_time = intent_request['currentIntent']['slots']['Time'] | |
source = intent_request['invocationSource'] | |
caller_id = get_call_id(intent_request['sessionAttributes']) | |
output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {} | |
timeslot_map = json.loads(try_except_key_error(lambda: output_session_attributes['timeslotMap']) or '{}') | |
if source == 'DialogCodeHook': | |
slots = intent_request['currentIntent']['slots'] | |
validation_result = validate_demo_appointment(date, appointment_time) | |
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'], date, timeslot_map) | |
) | |
) | |
if not date: | |
return elicit_slot( | |
output_session_attributes, | |
intent_request['currentIntent']['name'], | |
intent_request['currentIntent']['slots'], | |
'Date', | |
{'contentType': 'PlainText', | |
'content': 'When would you like to schedule your Demo?'}, | |
build_response_card( | |
'Specify Date', | |
'When would you like to schedule your Demo?', | |
build_options('Date', date, None) | |
) | |
) | |
if date: | |
demo_availabilities = try_except_key_error(lambda: timeslot_map[date]) | |
if demo_availabilities is None: | |
demo_availabilities = get_availabilities_mock(date) | |
timeslot_map[date] = demo_availabilities | |
output_session_attributes['timeslotMap'] = json.dumps(timeslot_map) | |
appointment_type_availabilities = get_availabilities_for_duration(30, demo_availabilities) | |
if len(appointment_type_availabilities) == 0: | |
slots['Date'] = None | |
slots['Time'] = None | |
return elicit_slot( | |
output_session_attributes, | |
intent_request['currentIntent']['name'], | |
slots, | |
'Date', | |
{'contentType': 'PlainText', | |
'content': 'We do not have any availability on that date, is there another day which works for you?'}, | |
build_response_card( | |
'Specify Date', | |
'What day works best for you?', | |
build_options('Date', date, timeslot_map) | |
) | |
) | |
message_content = 'What time on {} works for you? '.format(date) | |
if appointment_time: | |
output_session_attributes['formattedTime'] = build_time_output_string(appointment_time) | |
if is_available(appointment_time, demo_availabilities): | |
return delegate(output_session_attributes, slots) | |
message_content = 'Sorry, but the time slot you requested is not available. ' | |
if len(appointment_type_availabilities) == 1: | |
slots['Time'] = appointment_type_availabilities[0] | |
return confirm_intent( | |
output_session_attributes, | |
intent_request['currentIntent']['name'], | |
slots, | |
{ | |
'contentType': 'PlainText', | |
'content': '{}{} is our only availability, does that work for you?'.format | |
(message_content, build_time_output_string(appointment_type_availabilities[0])) | |
}, | |
build_response_card( | |
'Confirm Appointment', | |
'Is {} on {} okay?'.format(build_time_output_string(appointment_type_availabilities[0]), date), | |
[{'text': 'yes', 'value': 'yes'}, {'text': 'no', 'value': 'no'}] | |
) | |
) | |
available_time_string = build_available_time_string(appointment_type_availabilities) | |
return elicit_slot( | |
output_session_attributes, | |
intent_request['currentIntent']['name'], | |
slots, | |
'Time', | |
{'contentType': 'PlainText', 'content': '{}{}'.format(message_content, available_time_string)}, | |
build_response_card( | |
'Specify Time', | |
'What time works best for you?', | |
build_options('Time', date, timeslot_map) | |
) | |
) | |
return delegate(output_session_attributes, slots) | |
timeslot_availabilities = timeslot_map[date] | |
if timeslot_availabilities: | |
timeslot_availabilities.remove(appointment_time) | |
timeslot_map[date] = timeslot_availabilities | |
output_session_attributes['timeslotMap'] = json.dumps(timeslot_map) | |
if caller_id is not '': | |
response = table.query( | |
KeyConditionExpression=Key('phoneNumber').eq(caller_id) | |
) | |
if len(response['Items']) > 0: | |
prev_appointment = response['Items'][0]['appointment'] | |
multiple_appointment_time = prev_appointment + " and another MLOps Darius demo on " + appointment_time + ' ' + date | |
table.update_item( | |
Key={ | |
'phoneNumber': caller_id, | |
}, | |
UpdateExpression='SET appointment = :val1', | |
ExpressionAttributeValues={ | |
':val1': multiple_appointment_time | |
} | |
) | |
else: | |
table.put_item( | |
Item={ | |
'phoneNumber': caller_id, | |
'appointment': 'Darius MLOps demo on ' + appointment_time + ' ' + date | |
} | |
) | |
sms_message = "Thank you for booking your Darius MLOps demo with us. We will see you at {} on {}".format( | |
build_time_output_string(appointment_time), date) | |
send_sms(caller_id, sms_message) | |
else: | |
table.put_item( | |
Item={ | |
'phoneNumber': str(uuid.uuid4()), | |
'appointment': 'Darius MLOps demo on ' + appointment_time + ' ' + date | |
} | |
) | |
else: | |
logger.debug('Availabilities for {} were null at fulfillment time. ' | |
'This should have been initialized if this function was configured as the dialog code hook'.format( | |
date)) | |
return close( | |
output_session_attributes, | |
'Fulfilled', | |
{ | |
'contentType': 'PlainText', | |
'content': 'Okay, I have booked your appointment. We will see you at {} on {}'.format( | |
build_time_output_string(appointment_time), date) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment