Code Monkey home page Code Monkey logo

lex-dynamodb's People

Contributors

dependabot[bot] avatar mkfoster avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

joshy91

lex-dynamodb's Issues

Help with readme, index.js and Lambda Response error.

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);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.