亚马逊Alexa,alexa-app和中间件

lle*_*eto 15 alexa node.js express alexa-app

现在已经走了2天的不同路线,无法弄明白.也许有人可以解释我的问题.我正在尝试运行连接到多个平台的botserver,并且已经有大约5个工作.

我现在也在尝试整合Alexa.我看到Alexa请求进入我的服务器(因此Alexa技能和端点配置是正确的),但是这也花了我很长时间,因为亚马逊显然只是将流量发送到端口443,因此允许在Amazon开发中心定义另一个端口号,但什么也没做......真好!通过添加带端口转发的负载均衡器解决.

在真正的问题上.我试图使用alexa-app作为我的框架,来自以下示例:

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 
Run Code Online (Sandbox Code Playgroud)

当我在一个文件中设置我的快速服务器然后想要使用中间件功能在第二个文件中设置我的监听器时,我无法弄清楚的部分是如何使用它...类似于:

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());
Run Code Online (Sandbox Code Playgroud)

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>;
}
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助或指点!

lle*_*eto 1

最后,我设法通过 epxress 路由器将我的主 app.js 连接到 alexa-app 的 getMessagingHandler 函数。因此,在 app.js 中,将您的 Alexa Webhook 路由到侦听器中的 getMessagingHandler,然后再将其路由到侦听器中:

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;
Run Code Online (Sandbox Code Playgroud)

在主 app.js 中:

app.use('/alexa', controller.Bot.Alexa.Listener.getMessagingHandler());
Run Code Online (Sandbox Code Playgroud)