This repository contains a list of helper classes to handle Amazon Lex V2 responses and create custom requests. More specifically, it provides the following functionality:
- LexEvent: Amazon Lex event class
- LexResponse: builder to create Amazon Lex responses
- LexEventDispatcher: utility class to define your intent handlers
- IntentHandler: base class providing basic intent functionality
pip install amazon-lex-helper
LexEventDispatcher class provides an observer approach to register intent handlers which scales better and is more modular than just a simple loop, as in the examples:
For example, let's add extra validation to the Book Hotel example .
First step is to create our own BookHotel class to handle the intent. Notice the intent name (BookHotel) must match the intent name defined in Amazon Lex.
from amazon_lex_helper import LexEventDispatcher
from BookHotelIntent import BookHotelIntent
def lambda_handler(event, context):
lexEventDispatcher = LexEventDispatcher()
lexEventDispatcher.subscribe(
BookHotelIntent("BookHotel")
)
return lexEventDispatcher.dispatch(event)
BookHotelIntent class adds some extra behaviour to the intent handling.
In this case we will:
- allow only reservations for London and Bristol cities
- if the city is Bristol, we will set number of nights (Nights)to 5.
from amazon_lex_helper import IntentHandler, LexEvent, LexResponse
class BookHotelIntent (IntentHandler):
def process_request(self, req: LexEvent):
if req.slot_exists("Location"):
location = req.get_slot_interpreted_value("Location")
if location not in ["London", "Bristol"]:
return LexResponse.elicit_slot(req, "Location", message="Sorry, location can only be London or Bristol")
if location == "Bristol":
return LexResponse.delegate(req, "Nights", "5")
return LexResponse.delegate(req)
You can clone this repo and execute ./create_layer.sh script, which will create a .zip file inside /layer folder.
That zip can be then used to create a layer for your AWS Lambda function.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.