2017-03-09 91 views
2

在Node.js Lambda函數和Alexa之間對API進行REST調用時出現問題。我正在使用request庫來使用帳戶關聯技能撥打電話。我只爲這個意圖設置了一個樣本話語,模擬器看到這個很好。Node.js Lambda函數從REST調用返回「Alexa響應無效」

此外,cloudwatch日誌顯示來自api端點的200響應代碼以及API中從console.logs到CW的任何返回數據。

'use strict'; 
var http = require('http'); 
var request = require('request'); 
var Alexa = require('alexa-sdk'); 
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX"; 

exports.handler = function(event, context, callback) { 
    var alexa = Alexa.handler(event, context); 
    alexa.appId = APP_ID; 
    alexa.registerHandlers(handlers); 
    alexa.execute(); 
}; 

var handlers = { 
    'LaunchRequest': function() { 
     this.emit(':tell', 'Hi!'); 
    }, 

    'ApiWelcomeIntent': function() { 
     request('https://some.web/api', function (error, response, body) { 
      if (!error && response.statusCode == 200) { 
      // from within the callback, write data to response, essentially returning it. 
       var speechOutput = JSON.stringify(body); 
       console.log(body + " :Raw output?"); 
       console.log(speechOutput + ' :JSON stringified'); 
       console.log(response.statusCode); 
       this.emit(':tell', speechOutput); 
      } else { 
       console.log(error + ' : ' + response.statusCode); 
       this.emit(':tell', 'There was an error'); 
      } 
     }); 
    }, 

    'AMAZON.HelpIntent': function() {} //.........And other built in intents. 

    } 
}; 

我在猜測它與某些我要求Alexa「發出/說出」的speechOutput格式有關嗎?

回答

4

不,它與speechOutput的格式無關。問題是執行request方法的回調時,對this的引用丟失。爲了解決這個問題,保持對this參考調用request之前(例如分配this一個叫self變量):

'use strict'; 
var http = require('http'); 
var request = require('request'); 
var Alexa = require('alexa-sdk'); 
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX"; 

exports.handler = function(event, context, callback) { 
    var alexa = Alexa.handler(event, context); 
    alexa.appId = APP_ID; 
    alexa.registerHandlers(handlers); 
    alexa.execute(); 
}; 

var handlers = { 
    'LaunchRequest': function() { 
     this.emit(':tell', 'Hi!'); 
    }, 

    'ApiWelcomeIntent': function() { 
     self = this 

     request('https://some.web/api', function (error, response, body) { 
      if (!error && response.statusCode == 200) { 
      // from within the callback, write data to response, essentially returning it. 
       var speechOutput = JSON.stringify(body); 
       console.log(body + " :Raw output?"); 
       console.log(speechOutput + ' :JSON stringified'); 
       console.log(response.statusCode); 
       self.emit(':tell', speechOutput); // USE SELF HERE 
      } else { 
       console.log(error + ' : ' + response.statusCode); 
       self.emit(':tell', 'There was an error'); // AND HERE AS WELL 
      } 
     }); 
    }, 

    'AMAZON.HelpIntent': function() {} //.........And other built in intents. 

    } 
}; 
+0

嘿@renansdias你的答案聽起來很正確的,但使用自後我仍然面臨的問題。你可以檢查我的代碼以及在https://stackoverflow.com/questions/45613412/aws-lambda-not-able-to-make-rest-call-to-external-api – devutkarsh