简体   繁体   English

Amazon Alexa、alexa-app 和中间件

[英]Amazon Alexa, alexa-app and middleware

have been going different routes for 2 days now and can't figure it out.现在已经走不同的路线 2 天了,无法弄清楚。 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.我正在尝试运行一个连接到多个平台并且已经有大约 5 个工作平台的机器人服务器。

I am now also trying to integrate Alexa.我现在也在尝试整合 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!我看到 Alexa 请求进入我的服务器(因此 Alexa 技能和端点配置是正确的),但是这也花了我很长时间,因为亚马逊显然只将流量发送到端口 443,因此允许在亚马逊开发中心定义另一个端口号,但什么都不做……很好! 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:我试图从以下示例中使用 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 

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:应用程序.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:监听器.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!感谢您的帮助或指点!

In the end I managed to connect my main app.js via epxress router to the getMessagingHandler function of the alexa-app.最后,我设法通过 epxress 路由器将我的主 app.js 连接到 alexa-app 的 getMessagingHandler 函数。 So in the app.js route your alexa webhook to the getMessagingHandler in your listener and then in the listener:所以在 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;

In the main app.js:在主 app.js 中:

app.use('/alexa', controller.Bot.Alexa.Listener.getMessagingHandler());

have been going different routes for 2 days now and can't figure it out.已经走了2天的不同路线,无法解决。 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.我正在尝试运行连接到多个平台的botserver,并且已经有大约5个工作。

I am now also trying to integrate Alexa.我现在也在尝试整合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!我看到Alexa请求进入我的服务器(因此Alexa技能和端点配置是正确的),但是这也花了我很多时间,因为Amazon显然仅将流量发送到端口443,因此允许在Amazon开发中心中定义另一个端口号,但是什么也没做……很好! 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:我正在尝试从以下示例中使用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 

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: 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: 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!感谢您的帮助或指点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM