function logIntent()

in LambdaFunction2/index.js [73:163]


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.uuid();

        console.log('intent name =======>'+event.currentIntent.name);
        console.log('servicename=======>'+event.currentIntent.slots.servicename);
        console.log('current intent=========>'+event.currentIntent);
        console.log('inputTranscript=========>'+event.inputTranscript);
        console.log('requestAttributes=========>'+event.requestAttributes);
        console.log('sessionAttributes=========>'+event.sessionAttributes);
        // Note the data object we are sending to DynamoDB
        console.log(JSON.stringify(params, null, 4));

        if(event.currentIntent.name=="GetServiceInfo"){
            console.log("======> Query");
            var params = {
                TableName : tableName,
                ProjectionExpression:"LexLogId, servicevalue",
                KeyConditionExpression: "LexLogId = :name",
                ExpressionAttributeValues: {
                    ":name": event.currentIntent.slots.servicename
                }
            };
            console.log('=========>'+params);
            docClient.query(params, function(err, data) {
                if (err) {
                    console.log("Unable to query. Error:", JSON.stringify(err, null, 2));
                } else {
                    console.log("Query succeeded.");
                    data.Items.forEach(function(item) {
                        console.log(" -", item.LexLogId + ": " + item.servicevalue);
                        let callbackObj = {
                            dialogAction: {
                                type: 'Close',
                                fulfillmentState: "Fulfilled",
                                message: {
                                    contentType: "PlainText",
                                    content: item.servicevalue
                                }
                            }
                        };
                        // 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);
                    });
                }
            });
        }
        else{
            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);
    }
}