alexa-js / alexa-app

A framework for Alexa (Amazon Echo) apps using Node.js
https://www.youtube.com/watch?v=pzM4jv7k7Rg
MIT License
1.03k stars 213 forks source link

Normal Express logging altered with alexa-app? How to show incoming POST requests from Alexa? #338

Open roschler opened 6 years ago

roschler commented 6 years ago

I've noticed that when I have the alexa-app handlers active, the normal HTTP URL request logging seems to go inactive. That is, whenever I receive an Alexa Skill request, I don't see anything in my IDE Console/Log window describing the request. Normally I expect to see a message like "POST /api/alexa" or similar. Is there some option or setting I need to set to get the POST requests handled by alexa-app shown in the console/log?

Note, the console.log() does work since I have several of those in my alexa-app preRequest and postRequest functions.

DRIVE-BY QUESTION: In the incoming JSON I several instances like this:

sessionId: 'amzn1.echo-api.session.[unique-value-here]'

Is the presence of the [unique-value-here] string due to the fact the Alexa request is originating from the management console and not real Alexa skill origin point like the simulator or a device like an Echo? will I see actual values during real interactions with Alexa when I am handling real requests instead of requests coming from the management console Test facility?

Note, if there is an IRC chat room, gitter room, or other place I should be asking these general question rather than on the Issues forum here, please point me to that venue.

fremail commented 6 years ago

@roschler Every request to your skill goes through preRequest (app.express options) and app.pre callbacks. You can log all required data in one of those places.

So the code for logging will look like that:

var express = require("express");
var Alexa = require("alexa-app");
var express_app = express();
var app = new Alexa.app("my_skill");
app.express({ 
    expressApp: express_app,
    preRequest: function (json, req, resp) {
        // log incoming request here
        console.log(`POST ${req.originalUrl}\n\n${JSON.stringify(json)}`);
    }
});

app.pre = function (request, response, requestType) {
    // or log the request here
    // path is an Alexa app name
    // incoming JSON is stored in `request.data`
    console.log(`POST /${app.name}\n\n${JSON.stringify(request.data)}`);
};