mkfoster / lex-dynamodb Goto Github PK
View Code? Open in Web Editor NEWExample of how a serverless app can handle Lex requests and log intent and/or session data to dynamodb.
License: MIT License
Example of how a serverless app can handle Lex requests and log intent and/or session data to dynamodb.
License: MIT License
Hi Mark,
This looks really useful for my project but I'm struggling to get it working as I'm not clear from the readme what lines in the index.js need updating and in what format.
I've updated lines 6, 8 and 31 with my info but when I invoke the Lambda from my bot I get:
"An error has occurred: Invalid Lambda Response: Received error response from Lambda: Handled"
Updated Lambda index.js below if that helps.
Any help would be appreciated.
'use strict';
const aws = require('aws-sdk');
const uuidv4 = require('uuid/v4');
const dynamodb = new aws.DynamoDB();
// Update the Table name below
const tableName = process.env.LexLog;
//const tableName = 'LexLog';
const primaryKey = process.env.LexLogId;
//const primaryKey = 'LexLogId';
/**
* This function expects Lex events and logs intents and
* session variables to DynamoDB.
*
* Incoming events are routed based on intent.
*/
exports.handler = (event, context, callback) => {
try {
// By default, treat the user request as coming from the America/New_York time zone.
process.env.TZ = 'America/New_York';
console.log(`event.bot.name=${event.bot.name}`);
// Output the event in a JSON format we can easily copy and paste
// from the Cloudwatch logs to facilitate debugging.
console.log(JSON.stringify(event, null, 4));
/**
* Uncomment this if statement and populate with your Lex bot name, alias and / or version as
* a sanity check to prevent invoking this Lambda function from an undesired source.
*/
if (event.bot.name !== 'DearDiary') {
callback('Invalid Bot Name');
}
// Get the intent name. Intent names should match
// what you see in the AWS Lex Console
const intentName = event.currentIntent.name;
// Dispatch to the correct intent handler
if (intentName === 'Ping') {
return ping(event, callback);
} else if (intentName === 'LogIntent') {
return logIntent(event, callback);
}
// Handle unknown intents
throw new Error(`Intent with name ${intentName} not supported`);
} catch (err) {
callback(err);
}
};
/**
* This provides a quick way to check connectivity
* between Lex and this function.
*/
function ping(event, callback) {
let callbackObj = {
dialogAction: {
type: 'Close',
fulfillmentState: "Fulfilled",
message: {
contentType: "PlainText",
content: "Pong!"
}
}
};
callback(null, callbackObj);
}
/**
* This is to hanlde the "LogIntent" intent.
* Any intent phrases from LogIntent that Lex matches will be
* logged here.
*
* @param {} event
* @param {*} callback
*/
function logIntent(event, callback) {
try {
let docClient = new aws.DynamoDB.DocumentClient({region: process.env.AWS_REGION});
// Load the data we need to save from the input event into an object
let item = {
"currentIntent": event.currentIntent,
"inputTranscript": event.inputTranscript,
"requestAttributes": event.requestAttributes,
"sessionAttributes": event.sessionAttributes
};
item[primaryKey] = uuidv4();
let params = {
TableName: tableName,
Item: item
};
// Note the data object we are sending to DynamoDB
console.log(JSON.stringify(params, null, 4));
console.log('Putting to DynamoDB');
docClient.put(params, function(err, data) {
if(err) {
console.log('Error occurred on DynamoDB put.');
console.log(err);
callback(err);
} else {
let callbackObj = {
dialogAction: {
type: 'Close',
fulfillmentState: "Fulfilled",
message: {
contentType: "PlainText",
content: "Logged!"
}
}
};
// Save the response we are sending back to Lex in
// the Cloudwatch logs.
console.log('Logged!');
console.log(JSON.stringify(callbackObj, null, 4));
callback(null, callbackObj);
}
});
} catch (err) {
callback(err);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.