Last active
May 29, 2019 17:46
-
-
Save alexcasalboni/150682c8c3a9e180303a30083f27aa62 to your computer and use it in GitHub Desktop.
Amazon Lex fulfillment function - Lambda handler (Python)
This file contains hidden or 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
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