alexa-js / alexa-app

A framework for Alexa (Amazon Echo) apps using Node.js
https://www.youtube.com/watch?v=pzM4jv7k7Rg
MIT License
1.03k stars 213 forks source link

Make API request inside intent #306

Closed luizfranca closed 6 years ago

luizfranca commented 6 years ago

Hi. I've seen other questions like this one here, but I couldn't make it work with the answers from the other questions. I'm try to call a weather api and send it as a response, but it always returns blank on the speech output. Here is my intent:


app.intent('sayWeather',
  {
    "slots" : {"city" : "AMAZON.US_CITY"},
    "utterances":[ 
        "what is the weather in {-|city}",
        "tell me the weather in {-|city}",
        "how is the weather in {-|city}"]
  },
  function(request,response) => {
    var city = request.slot("city");
    var forecast = "Searching...";
    weather.find({search: city, degreeType: 'C'}, (err, result) => {
        if(err) {
        response.say("The weather in " + city + " could not be found!").send();
        } else {
            var forecast = result[1].current.temperature;
        response.say("The weather in " + city + " is " + forecast + " degrees celsius").send();
      }

    });
  }
);```

And when I try I get this response:

![screen shot 2017-12-12 at 09 03 49](https://user-images.githubusercontent.com/4142065/34212872-6d97604a-e57c-11e7-99cb-c5622e39ca7e.png)

Does anyone know what is wrong?
dblock commented 6 years ago

You have to return a promise from the function. If whether.find returns a promise, then you're just missing a `return weather.find..., otherwise you need to wrap this callback up in one.

I'll close this, but feel free to ask for more details if you can't figure it out.

luizfranca commented 6 years ago

It worked. Thank you :D