question

coreycole avatar image
coreycole asked

Intercept response objects for debugging v1 skill

I'd like to print out the entire request and response objects to better understand what intents are being invoked and what my code is building up as a response. For example, here is my main handler with my LaunchRequest:

export const handler = (
        event: Alexa.RequestBody<Alexa.Request>,
        context: Alexa.Context,
        callback: (err: any, response: any) => void): void => {
    const deviceId = event.context.System.device.deviceId;
    devicelogsService.logRequest(event, deviceId);
    alexa.registerHandlers({
        'LaunchRequest'() {
            intentControllers.launchRequest(this, deviceId)
                .then(() => {
                    console.dir(this);
                    devicelogsService.logResponse(this.response, deviceId);
                })
                .catch(err => console.error(`[Index][launchRequest]: Error = ${err}`, deviceId));
        },


As you can see, I'm trying I access `this.response`. I get some data, but not all the data. This is an example of the response that I'm able to access

{
    "version": "1.0",
    "response": {
        "shouldEndSession": true
    },
    "sessionAttributes": {
        "lastSpeech": "my value"
    }
}

But, I know the full response object looks like this:

 {
  "version": "1.0",
  "response": {
    "shouldEndSession": false,
    "outputSpeech": {
      "type": "SSML",
      "ssml": "<speak> my value </speak>"
    },
    "reprompt": {
      "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak> my value reprompt  </speak>"
      }
    }
  },
  "sessionAttributes": {
    "lastSpeech": "my value"
  },
  "userAgent": "ask-nodejs/1.0.25 Node/v8.11.1"
}

How can I access these properties like response.outputSpeech and response.reprompt? My intent controllers return a promise that resolves after I've called the emit() function that should attach these outputSpeech and reprompt objects onto my response object. Is the emit() function async? Is there any way I can wait for emit() to finish attaching these objects onto the response object?

alexa skills kitdebuggingintentsresponsenodejs
2 comments
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

coreycole avatar image coreycole commented ·

Looks like the emit function is defined here. Doesn't look like it returns a promise or anything https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/alexa.js#L246

0 Likes 0 ·
coreycole avatar image coreycole commented ·

I tried setting a timeout to wait and see if the response object was updated asynchronously. It still hasn't been updated 15 seconds after I call the emit function, so I don't think so

'LaunchRequest'() {
            hallooIntentController.launchRequest(this, deviceId)
                .then(() => {
                    const response = this.response;
                    setTimeout(() => console.dir(response), 15000);
                    devicelogsService.logResponse(this.response, deviceId);
                })


0 Likes 0 ·

1 Answer

Jenn@amazon avatar image
Jenn@amazon answered

The emit() should be the last thing in the Function since it forms the response JSON and returns it back to Alexa.

If you're using Node, pretty everything is async unless specified.

6 comments
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

coreycole avatar image coreycole commented ·

I am using node. Is there any way to access the response JSON the emit() function attaches to the response object?

0 Likes 0 ·
Jenn@amazon avatar image Jenn@amazon coreycole commented ·

If you are using the this.response.speak().listen() syntax, adding a console.log() before the final this.emit(':responseReady');

For example:

var outputSpeech = "My output speech";
var reprompt = "My Reprompt";

this.response.speak(outputSpeech).listen(reprompt);

console.log("==Response== " + JSON.stringify(this.handler.response));
this.emit(':responseReady');
1 Like 1 ·
coreycole avatar image coreycole Jenn@amazon commented ·

@Jenn@amazon

This will work for ':tell' responses, but it doesn't seem possible to do ':ask' using this alternative syntax.

0 Likes 0 ·
Show more comments