Skip to content

Instantly share code, notes, and snippets.

@alexcasalboni alexcasalboni/lex.py
Last active May 29, 2019

Embed
What would you like to do?
Amazon Lex fulfillment function - Lambda handler (Python)
from lex_utils import elicit_slot, delegate, close, ElicitAction, DelegateAction
from utils import validate_dialog, init_or_load_session, finalize_session, actually_book_the_hotel
def lambda_handler(event, context):
intent_name = event['currentIntent']['name']
if intent_name == 'BookHotel':
return book_hotel(event)
# elif (add more intents here)
else:
raise Exception('Intent with name %s not supported' % intent_name)
def book_hotel(event):
session_attributes = init_or_load_session(event)
slots = event['currentIntent']['slots']
name = event['currentIntent']['name']
try:
if event['invocationSource'] == 'DialogCodeHook':
validate_dialog(slots, session_attributes)
except ElicitAction as ea:
# request a new value
return elicit_slot(
session_attributes,
name,
slots,
ea.invalid_slot, # elicit this invalid slot
ea.message, # with this custom message
)
except DelegateAction as da:
# tell Lex to move on with the next slot
return delegate(session_attributes, slots)
# ok, we have all the slots and we can finally book the hotel!
actually_book_the_hotel(session_attributes)
finalize_session(session_attributes)
return close(
session_attributes,
'Thanks, I have placed your hotel reservation. What shall we do next?',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.