Amazon Lex Integration
Learn how to integrate AWS Lambda with Amazon Lex.
What is Amazon Lex?
Amazon Lex is a service provided by AWS that allows developers to build conversational interfaces and chatbots using natural language understanding (NLU) and automatic speech recognition (ASR) capabilities. Essentially, Amazon Lex makes it easier to create applications that can engage in text and voice-based interactions with users.
An AWS Lambda function can be configured to process some of the steps in the Lex workflow. For example, a Lex service can post events to it with summaries of the user’s intention. The function can then deliver responses based on the intent.
Integrating AWS Lambda with Lex
The following playground demonstrates how an AWS Lambda function can be integrated with Lex.
using System.Text.Json; using System.Text.Json.Serialization; using Amazon.Lambda.Core; using Amazon.Lambda.LexEvents; namespace LambdaApp; public abstract class AbstractIntentProcessor : IIntentProcessor { internal const string CURRENT_RESERVATION_PRICE_SESSION_ATTRIBUTE = "currentReservationPrice"; internal const string CURRENT_RESERVATION_SESSION_ATTRIBUTE = "currentReservation"; internal const string LAST_CONFIRMED_RESERVATION_SESSION_ATTRIBUTE = "lastConfirmedReservation"; internal const string MESSAGE_CONTENT_TYPE = "PlainText"; public abstract LexResponse Process(LexEvent lexEvent, ILambdaContext context); protected string SerializeReservation(Reservation reservation) { return JsonSerializer.Serialize(reservation, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); } protected Reservation DeserializeReservation(string json) { return JsonSerializer.Deserialize<Reservation>(json) ?? new Reservation(); } protected LexResponse Close(IDictionary<string, string> sessionAttributes, string fulfillmentState, LexResponse.LexMessage message) { return new LexResponse { SessionAttributes = sessionAttributes, DialogAction = new LexResponse.LexDialogAction { Type = "Close", FulfillmentState = fulfillmentState, Message = message } }; } protected LexResponse Delegate(IDictionary<string, string> sessionAttributes, IDictionary<string, string> slots) { return new LexResponse { SessionAttributes = sessionAttributes, DialogAction = new LexResponse.LexDialogAction { Type = "Delegate", Slots = slots } }; } protected LexResponse ElicitSlot(IDictionary<string, string> sessionAttributes, string intentName, IDictionary<string, string?> slots, string? slotToElicit, LexResponse.LexMessage? message) { return new LexResponse { SessionAttributes = sessionAttributes, DialogAction = new LexResponse.LexDialogAction { Type = "ElicitSlot", IntentName = intentName, Slots = slots, SlotToElicit = slotToElicit, Message = message } }; } protected LexResponse ConfirmIntent(IDictionary<string, string> sessionAttributes, string intentName, IDictionary<string, string?> slots, LexResponse.LexMessage? message) { return new LexResponse { SessionAttributes = sessionAttributes, DialogAction = new LexResponse.LexDialogAction { Type = "ConfirmIntent", IntentName = intentName, Slots = slots, Message = message } }; } }
In this application, we check the type of Lex event. Depending on the type, it processes the event and delivers a response. The application supports three intent types:
Booking a hotel
Booking a car
Ordering flowers
Here’s a diagram of the ...