Skip to content

aws-samples/amazon-lex-v2-helper

amazon-lex-helper

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

Quick Install

pip install amazon-lex-helper

Example

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:

  1. allow only reservations for London and Bristol cities
  2. 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)

AWS Lambda usage

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.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.