简体   繁体   中英

Alexa intent is not waiting for the api response

We are developing alexa skill using alexa-app, in one of our intent we are trying to fetch albums from facebook and on success/failure we want alexa to respond accordingly. But intent is not waiting for FB call to complete. Below is the code snippet we are using:

function fetchAlbums(){
  return new Promise(resolve =>{
   FB.api("/me/albums", function (res) {
    if (res && !res.error) {
        // If we have data
        if (res.data) {
            resolve("Got Albums");
        } else {
            // REPORT PROBLEM WITH PARSING DATA
            resolve("Error getting albums");
        }
    } else {
        // Handle errors here.
        resolve("Error hitting endpoint");
    }
  });
 });
}

alexaApp.intent("readFeedIntent", {
  "utterances": [
    "read my facebook feed", "read facebook feed", "read feed"
  ]
},
function(request, res1) {
  // Again check if we have an access token
  if(request.hasSession() && !accessToken){
   accessToken = request.data.session.user.accessToken;
   FB.setAccessToken(accessToken);
  }
  if (accessToken) {
    var session = request.getSession();
    fetchAlbums().then(function(data){
        console.log(data);
        res1.say(data);
    });
  } else {
    res1.say(noAccessToken, tryLaterText).send();
  }
});

It is not throwing any errors, but Alexa is not speaking the anything where I can see the response in the console logs.

If I add: res1.say("Whatever!") at the end of function, then Alexa will speak 'Whatever' in response to this intent.

Got it solved my myself:

instead of this:

fetchAlbums().then(function(data){
      console.log(data);
      res1.say(data);
})

you have to return it, like:

return fetchAlbums().then(function(data){
      console.log(data);
      res1.say(data);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM