Search code examples
amazon-web-servicesaws-lambdaalexa-skills-kit

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


I am relatively new to AWS and Alexa skills. I am building a simple custom skill that gives you a dressing advice depending on the weather.

I have 2 custom intents : dressingTodayIntent & dressingTomorrowIntent. In the Service Simulator of the developer portal, my two intents don't work, I do get a lambda response though, but with an undefined outputSpeech, like this:

{ 
 "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": {} 
}

Could it be a scope issue in my intent code?

'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');
},

In AWS Lambda, I see a correct output for the first 2 logs, and an error for the 3rd one:

  • first log: "one " + dressingAdvice, as expected
  • second log: "two " + dressingAdvice, as expected
  • third log: "three " + undefined

Thank you for you help!


Solution

  • I figured out what was wrong, I needed to move the response code into the callback function, like this:

    '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');
            });    
    },