Make Amazon Lex Smarter

Thuong To
5 min readDec 29, 2023

--

exports.handler = function(event, ctx, cb){
var
my_response = {};
if(event.currentIntent.slots.city_str){
// we have the city already awesome keep going
}else{
//we need to ask for (elicit) a city
my_response.statusCode = 200;
my_response.body = {
"dialogAction": {
"type": "ElicitSlot",
"message": {
"contentType": "PlainText",
"content": "Name the city your cat lives in, thanks"
},
"intentName": "CatWeather",
"slots": {
"city_str": null
},
"slotToElicit" : "city_str"
}
};
return cb(null, my_response.body);
}
var
city_str = event.currentIntent.slots.city_str,
AWS = require("aws-sdk"),
DDB = new AWS.DynamoDB({
apiVersion: "2012-08-10",
region: "us-east-1"
}),
lookup_name_str = city_str.toUpperCase(),
params = {
TableName: "weather",
KeyConditionExpression: "sc = :v1",
ExpressionAttributeValues: {
":v1":{
"S": lookup_name_str
}
},
ProjectionExpression: "t"
};

console.log(params);
DDB.query(params, function(err, data){
if(err){
throw err;
}

if(data.Items && data.Items[0] && data.Items[0].t){
console.log("city weather found");
console.log(data.Items[0]);
my_response.statusCode = 200;
my_response.body = {
"sessionAttributes": {
"temp_str": data.Items[0].t.N,
"city_str": event.currentIntent.slots.city_str
},
"dialogAction":{
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": data.Items[0].t.N
}
}
};
}else{
console.log("city weather not found for " + lookup_name_str);
my_response.statusCode = 200;
my_response.body = {
"dialogAction": {
"type": "ElicitSlot",
"message": {
"contentType": "PlainText",
"content": "Please try another city, we couldn't find the weather for that city"
},
"intentName": "CatWeather",
"slots": {
"city_str": null
},
"slotToElicit" : "city_str"
}
}
}
return cb(null, my_response.body);
});
};
{
"messageVersion": "1.0",
"invocationSource": "DialogCodeHook",
"userId": "1012602",
"sessionAttributes": {
},
"bot": {
"name": "WeatherCatBot",
"alias": "$LATEST",
"version": "$LATEST"
},
"outputDialogMode": "Text",
"currentIntent": {
"name": "CatWeather",
"slots": {
"city_str": "CHICAGO"
},
"confirmationStatus": "None"
}
}
{
"sessionAttributes": {
"temp_str": "42",
"city_str": "CHICAGO"
},
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": "42"
}
}
}
{
"messageVersion": "1.0",
"invocationSource": "DialogCodeHook",
"userId": "1012603",
"sessionAttributes": {
},
"bot": {
"name": "weather",
"alias": "$LATEST",
"version": "$LATEST"
},
"outputDialogMode": "Text",
"currentIntent": {
"name": "catWeather",
"slots": {
"city_str": "BANANA"
},
"confirmationStatus": "None"
}
}
{
"dialogAction": {
"type": "ElicitSlot",
"message": {
"contentType": "PlainText",
"content": "Please try another city, we couldn't find the weather for that city"
},
"intentName": "CatWeather",
"slots": {
"city_name_str": null
},
"slotToElicit": "city_str"
}
}
{
"messageVersion": "1.0",
"invocationSource": "DialogCodeHook",
"userId": "1012604",
"sessionAttributes": {
},
"bot": {
"name": "weather",
"alias": "$LATEST",
"version": "$LATEST"
},
"outputDialogMode": "Text",
"currentIntent": {
"name": "catWeather",
"slots": {
"city_str": null
},
"confirmationStatus": "None"
}
}
{
"dialogAction": {
"type": "ElicitSlot",
"message": {
"contentType": "PlainText",
"content": "Name the city your cat lives in, thanks"
},
"intentName": "CatWeather",
"slots": {
"city_str": null
},
"slotToElicit": "city_str"
}
}
  function handler(event, context, callback){      
var
MESSAGE_STR = event.message_str,
USER_ID_STR = event.user_id_str,
AWS = require("aws-sdk"),
LEXRUNTIME = {},
BOT_NAME_STR = "WeatherCatBot",
BOT_ALIAS_STR = "$LATEST",
sessionAttributes = {

},
params = {};

AWS.config.update({
region: "us-east-1"
});

LEXRUNTIME = new AWS.LexRuntime();

params = {
botAlias: BOT_ALIAS_STR,
botName: BOT_NAME_STR,
inputText: MESSAGE_STR,
userId: USER_ID_STR,
sessionAttributes: sessionAttributes
};
LEXRUNTIME.postText(params, function(error, data){
var response = {};
if(error){
console.log(error, error.stack);
response = "problem with lex";
callback(null, response);
}else{
console.log(data);
response = data;
callback(null, response);
}
});
}
exports.handler = handler;
  {
"message_str": "can my cat go out in alto?",
"user_id_str": "10126023"
}
  Response:
{
"intentName": "CatWeather",
"slots": {
"city_str": "alto"
},
"sessionAttributes": {
"city_str": "alto",
"temp_str": "47"
},
"message": "47",
"messageFormat": "PlainText",
"dialogState": "Fulfilled",
"slotToElicit": null
}
  {
"message_str": "can my cat go out in alto?",
"user_id_str": "10126023"
}
  {
"message_str": "can my cat go out?",
"user_id_str": "10126024"
}
  {
"intentName": "CatWeather",
"slots": {
"city_str": null
},
"sessionAttributes": {},
"message": "Name the city your cat lives in, thanks",
"messageFormat": "PlainText",
"dialogState": "ElicitSlot",
"slotToElicit": "city_str"
}
  {
"message_str": "DENVER",
"user_id_str": "10126024"
}
 {
"intentName": "CatWeather",
"slots": {
"city_str": "DENVER"
},
"sessionAttributes": {
"city_str": "DENVER",
"temp_str": "38"
},
"message": "38",
"messageFormat": "PlainText",
"dialogState": "Fulfilled",
"slotToElicit": null
}

Resources

Deploying an Amazon Lex Bot on a Messaging Platform

In the video, we explained how to integrate our Lex bot with Facebook messenger. As noted, in Addition to Facebook messenger, you can deploy your Lex bot on Slack and Twilio.

Examples and details about how to do this can be found in the Lex Documentation

--

--

Thuong To
Thuong To

No responses yet