AWS Lex helper classes and functions to build responses
- ElicitSlot - Informs Amazon Lex that the user is expected to provide a slot value in the response
- ElicitIntent - Informs Amazon Lex that the user is expected to respond with an utterance that includes an intent
- ConfirmIntent - Informs Amazon Lex that the user is expected to give a yes or no answer to confirm or deny the current intent
- Close - Informs Amazon Lex not to expect a response from the user
- Delegate - Directs Amazon Lex to choose the next course of action based on the bot configuration
View more details on Lex Documentation
- PlainTextMessage
- ResponseCard
- ResponseCardAttachment
- AttachmentButton
- ResponseCardAttachment
Your lambda functions need to response in the following format
{
"sessionAttributes": {
"key1": "value1",
"key2": "value2"
...
},
"dialogAction": {
"type": "ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close",
Full structure based on the type field. See below for details.
}
}
Using this module, you can easily generate the above response like below.
- Generate the dialog action content using message components (ResponseCard,ResponseCardAttachment,AttachmentButton,PlainTextMessage)
- Generate the lambda response using the message component generated in step 1
npm install aws-lex-helpers --save
const AWSLexHelpers = require('aws-lex-helpers');
// Response generator
const Responses = AWSLexHelpers.Responses;
// Message components
const {ResponseCard,ResponseCardAttachment,AttachmentButton,PlainTextMessage,Constants} = AWSLexHelpers;
let plainTextMessage = new PlainTextMessage()
.setMessage('hey there this is a plain text message')
.build();
let firstAttachmentButton = new AttachmentButton()
.setText('First Button')
.setValue('first button')
.build();
let secondAttachmentButton = new AttachmentButton()
.setText('Second Button')
.setValue('second button')
.build();
let responseCardAttachment = new ResponseCardAttachment()
.setTitle('Attachment Title')
.setSubTitle('Attachment subtitle')
.setImageUrl('<IMAGE_URL>') // Hosted url of the image
.setAttachmentLinkUrl('<LINK_URL>') // URL to be opened once click on the image
.setButtons([firstAttachmentButton, secondAttachmentButton])
.build();
let responseCard = new ResponseCard()
.setGenericAttachments([responseCardAttachment]) // Response card attachments can be provided as an array
.build();
let sessionAttributes = {YOUR_SESSION_ATTRIBUTES};
let intentName = 'YOUR_INTENT_NAME';
let slots = {YOUR_SLOTS};
let slotToElicit = {YOUR_SLOTS_TO_ELICIT};
let response = Responses.elicitSlot(sessionAttributes, intentName, slots, slotToElicit, plainTextMessage,responseCard);
let response = Responses.elicitIntent(sessionAttributes, plainTextMessage, responseCard);
let response = Responses.confirmIntent(sessionAttributes, intentName, slots, plainTextMessage,responseCard);
let fulfillmentState = Constants.FULFILMENT_STATES.FULFILLED; // or FAILED
let response = Responses.close(sessionAttributes, fulfillmentState, plainTextMessage, responseCard);
let response = Responses.delegate(sessionAttributes, slots);
Return the response above from your lambda function
-
FULFILMENT_STATES
- FULFILLED
- FAILED
-
INVOCATION_SOURCE
- DIALOG_CODE_HOOK
- FULFILMENT_CODE_HOOK