2017-04-03 160 views
11

已經走了兩天不同的路線,現在無法弄清楚。也許有人可以解釋我的問題。我試圖運行一個連接到多個平臺並且已經有大約5個工作的botserver。亞馬遜Alexa,alexa-app和中間件

我現在也試圖整合Alexa。我看到Alexa請求進入我的服務器(所以Alexa技能和端點配置是正確的),但是這也花了我相當長的一段時間,因爲亞馬遜顯然只向端口443發送流量,因此允許在Amazon開發中心定義另一個端口號,但什麼都不做......很好!通過添加端口轉發的負載均衡器來解決。

對真正的問題。我試圖使用Alexa的應用程序內從下面的例子中我的框架:

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 

我想不通的部分是如何使用這個,當我安裝我在一個文件中明確的服務器,然後要使用中間件功能設置我的聽衆在第二個文件...是這樣的:

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>; 
} 

感謝您的幫助或指點!

+0

我不綁定到Alexa的應用程序內,所以如果人們有其他圖書館可以用來實現這一點,我非常樂於提供建議。 – lleto

+0

最後,我設法基於Express和Alexa-app獲得此項工作。 – lleto

+0

你可以發佈你的解決方案作爲這個問題的答案嗎?嘗試使用alexa-sdk的 –

回答

0

最後,我設法通過epxress路由器將我的主要app.js連接到alexa-app的getMessagingHandler函數。所以在app.js路線你的Alexa網絡掛接到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; 

在主app.js:

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