Alexa-skills-kit-sdk-for-nodejs: Add way to Log all Request & Response JSON used/generated by SDK

Created on 20 Apr 2018  路  6Comments  路  Source: alexa/alexa-skills-kit-sdk-for-nodejs

Expected Behavior

Previously in SDK v1 i was using below code, so i can log all of request & response JSON data to CloudWatch.

exports.handler = function (event, context, callback) {
    console.log("\n" + "******************* REQUEST **********************");
    console.log("\n" + JSON.stringify(event, null, 2));

    var origCallback = callback;
    callback = function (error, response) {
        console.log("\n" + "******************* RESPONSE  **********************");
        console.log("\n" + JSON.stringify(response, null, 2));
        return origCallback(error, response);
    }


    var alexa = Alexa.handler(event, context, callback);
    ...
}

Current Behavior

After SDK v2, Where should i place so i can log those JSON data.

Possible Solution

Add Any method in BaseSkillFactory, so we can log those easily.

exports.handler = skillBuilder
  .addRequestHandlers(...)
  .addErrorHandlers(ErrorHandler)
  .enableLogs(true)
  .lambda();

Context

If Logs of JSON is avilable then it would be easy as a developer to determine what's going wrong with Request/Response format.

Your Environment

  • ASK SDK for Node.js used: 2.0
  • Operating System and version: Windows 10 v1703

Node.js and NPM Info

Most helpful comment

Thanks for the feedback @mehtanilay10 and happy to help 馃槃

All 6 comments

Just create request and response interceptors

Hi @mehtanilay10,

.lambda() is one way of creating the lambda handler. SDK v2 offers similar ways for you to manually invoke like v1. See this link. In the code snippet in this link, the event contains request json and skill.invoke() returns a Promise of response json.

Regards

I have tried solution given by @Amazon-tianrenz , but it works for request only. I was unable to log response data.
@rmtuckerphx I am not aware of interceptors, and search for it but not find any useful content. Can you explain how to do that?

For response interceptors you can call addResponseInterceptors(...) from BaseSkillFactory
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/2.0.x/ask-sdk-core/lib/skill/factory/BaseSkillFactory.ts#L117

In a response interceptor, you're passed the response object, so you can log whatever fields/data you want from it 馃槃

See the request & response interceptors docs for more information

Thanks, @brendanclement for Quick response and Documentation link.
I have used below snippet for loging all request & response data and it works as expected. It was realy better then v1 SDK.

Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        ...
    )
    .addResponseInterceptors(function(requestEnvelope, response){
        console.log("\n" + "******************* REQUEST ENVELOPE **********************");
        console.log("\n" + JSON.stringify(requestEnvelope, null, 4));
        console.log("\n" + "******************* RESPONSE  **********************");
        console.log("\n" + JSON.stringify(response, null, 4));
    })
    .addErrorHandlers(ErrorHandler)
    .create();

Thanks for the feedback @mehtanilay10 and happy to help 馃槃

Was this page helpful?
0 / 5 - 0 ratings