Build a serverless chatbot (voice and text) with Amazon Lex and integrate with Facebook
What is Amazon Lex?
Amazon Lex is an AWS service for building conversational interfaces for applications using voice and text.Amazon Lex provides the deep functionality and flexibility of natural language understanding (NLU) and automatic speech recognition (ASR) so you can build highly engaging user experiences with lifelike, conversational interactions, and create new categories of products.
What is AWS Lambda?
AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code.
Steps:
1. Create a Lambda Function.
First, create a Lambda function which fulfils a pizza order. You specify this function in your Amazon Lex bot, which you create in the next section.
'use strict';
// Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
// --------------- Events -----------------------
function dispatch(intentRequest, callback) {
console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);
const sessionAttributes = intentRequest.sessionAttributes;
const slots = intentRequest.currentIntent.slots;
const crust = slots.crust;
const size = slots.size;
const PizzaKind = slots.PizzaKind;
callback(close(sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${PizzaKind} pizza on ${crust} crust`}));
}
// --------------- Main handler -----------------------
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
try {
dispatch(event,
(response) => {
callback(null, response);
});
} catch (err) {
callback(err);
}
};
{
"messageVersion": "1.0",
"invocationSource": "FulfillmentCodeHook",
"userId": "user-1",
"sessionAttributes": {},
"bot": {
"name": "PizzaOrderingApp",
"alias": "$LATEST",
"version": "$LATEST"
},
"outputDialogMode": "Text",
"currentIntent": {
"name": "OrderPizza",
"slots": {
"size": "large",
"pizzaKind": "meat",
"crust": "thin"
},
"confirmationStatus": "None"
}
}
2. Create a Bot.
Now, create the OrderPizza intent , an action that the user wants to perform, with the minimum information needed. You add slot types for the intent and then configure the intent later.
To create slot types
Recommended by LinkedIn
After Adding the values it should look like this.
Configure the OrderPizza intent to fulfill a user's request to order a pizza.
Under Select utterances Type the following strings. The curly braces {} enclose slot names.
Build and test the bot
Now we will Integrate this Chat bot to our Facebook page.
THANK YOU