Alexa 函数在 AWS lambda 中有效,但在服务模拟器中无效

Alexa function works in AWS lamda, but not from the Service Simulator

我对 AWS 和 Alexa 技能比较陌生。我正在构建一个简单的自定义技能,可以根据天气为您提供穿衣建议。

我有 2 个自定义意图:dressingTodayIntent 和 dressingTomorrowIntent。在开发人员门户的服务模拟器中,我的两个意图不起作用,虽然我确实得到了 lambda 响应,但具有未定义的 outputSpeech,如下所示:

{ 
 "version": "1.0", 
 "response": { 
   "outputSpeech": { 
     "type": "SSML", 
     "ssml": "<speak> undefined </speak>" 
   }, 
   "card": null, 
   "reprompt": null, 
   "speechletResponse": { 
     "outputSpeech": { 
       "id": null, 
       "ssml": "<speak> undefined </speak>" 
     }, 
     "card": null, 
     "directives": null, 
     "reprompt": null, 
     "shouldEndSession": true 
   } 
 }, 
 "sessionAttributes": {} 
}

这可能是我的意图代码中的范围问题吗?

'DressingTodayIntent': function() {
    var dressingAdvice;
    var speechOutput = getJSON('https://api.darksky.net/forecast/9e0495a835ed823a705a9a567eee982a/48.861317,2.348764?units=si&exclude=currently,minutely,hourly,alerts,flags',
        function(err, forecast) {
            if (err) {
              console.log('Error occurred while trying to retrieve weather data', err);
            } else {
              dressingAdvice = getDressingAdvice(forecast, true);
              console.log("one " + dressingAdvice);
            }
            console.log("two " + dressingAdvice);
            return dressingAdvice;
        });
    console.log("three " + speechOutput);
    this.response.cardRenderer("Your dressing advice for today:", speechOutput);
    this.response.speak(speechOutput);
    this.emit(':responseReady');
},

在 AWS Lambda 中,我看到前 2 个日志的输出正确,第 3 个日志的输出错误:

谢谢你的帮助!

当您说 "tested from AWS Lambda" 时,我假设您的意思是使用 AWS 控制台向 Lambda 发送 JSON 测试消息,然后查看响应 JSON 以确定是否正确吗?

如果是,请确保它与 JSON 发送 to/from 开发门户中的 Alexa 测试页面匹配。听起来他们可能有所不同。

此外,请确保您已链接到 Alexa 技能中的正确 ARN。

undefined 可能是代码中的变量范围问题。

我在您的回复中注意到您没有 sessionAttributes。您的代码是设置还是从会话值中提取响应值?如果是这样,这些值需要用 sessionAttributes 发回。

我弄清楚哪里出了问题,我需要将响应代码移到回调函数中,如下所示:

'DressingTodayIntent': function() {
    var speechOutput;
    var self = this;
    var dressingAdvice = getJSON('https://api.darksky.net/forecast/9e0495a835ed823a705a9a567eee982a/48.861317,2.348764?units=si!ude=currently,minutely,hourly,alerts,flags',
        function(err, forecast) {
            if (err) {
              console.log('Error occurred while trying to retrieve weather data', err);
            } else {
              speechOutput = getDressingAdvice(forecast, true);
            }
            self.response.cardRenderer("Your dressing advice for today:", speechOutput);
            self.response.speak(speechOutput);
            self.emit(':responseReady');
        });    
},