Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Alexa, alexa-app and middleware

have been going different routes for 2 days now and can't figure it out. Perhaps someone can shed some light on my problems. I am trying to run a botserver that connects to multiple plaforms and already have around 5 working.

I am now also trying to integrate Alexa. I am seeing Alexa requests coming into my server (so the Alexa skill and endpoint config are correct), however this also took me quite some time as Amazon apparently only sends traffic to port 443, so defining another port number in Amazon dev center is allowed, but does nothing... nice! Solved by adding a load balancer with port forwarding.

Onto the real question. I am trying to use alexa-app as my framework from the following example:

var express = require("express");
var alexa = require("alexa-app");
var express_app = express();

var app = new alexa.app("sample");

app.intent("number", {
    "slots": { "number": "AMAZON.NUMBER" },
    "utterances": ["say the number {-|number}"]
  },
  function(request, response) {
    var number = request.slot("number");
    response.say("You asked for the number " + number);
  }
);

// setup the alexa app and attach it to express before anything else 
app.express({ expressApp: express_app });

// now POST calls to /sample in express will be handled by the app.request() function 
// GET calls will not be handled 

// from here on, you can setup any other express routes or middleware as normal 

The part which I can't figure out is how to use this when I setup my express server in one file and then want to use the middleware function to setup my listener in the second file... something like:

app.js:

var express = require("express");
var express_app = express();

https.createServer({
    key: fs.readFileSync(key),
    cert: fs.readFileSync(cert),
    ca: fs.readFileSync(ca)
}, app).listen(port, function () {
   console.log("http:  api server listening on port " + port);
});

app.use('/alexa', controller.Bot.Messenger.Listener.botMiddleWare());

listener.js:

var alexa = require("alexa-app");
var app = new alexa.app("sample");

bot.botMiddleWare = function botMiddleWare () {
    return <return function to connect to express in app.js>;
}

Thanks for any help or pointers!

like image 259
lleto Avatar asked Apr 03 '17 19:04

lleto


1 Answers

In the end I managed to connect my main app.js via epxress router to the getMessagingHandler function of the alexa-app. So in the app.js route your alexa webhook to the getMessagingHandler in your listener and then in the listener:

var bot = new alexa.app('my_bot');

bot.getMessagingHandler = function getMessagingHandler() {
    return function (req, res) {
         req.on('end', function(){
             var jsonData = JSON.parse(requestBody);
             if(jsonData.request.type == "LaunchRequest") {
               // handle response here
             }
         }
    }
}
module.exports = bot;

In the main app.js:

app.use('/alexa', controller.Bot.Alexa.Listener.getMessagingHandler());
like image 140
lleto Avatar answered Oct 15 '22 02:10

lleto