Alexa意图不等待api响应

时间:2017-10-11 10:26:25

标签: javascript node.js alexa alexa-skill alexa-app

我们正在使用alexa-app开发alexa技能,在我们的一个意图中我们试图从Facebook获取专辑,并且在成功/失败时我们希望alexa做出相应的响应。但意图并不是等待FB呼叫完成。以下是我们使用的代码段:

  public static int versionCompare(String str1, String str2) {
    String[] vals1 = str1.split("\\.");
    String[] vals2 = str2.split("\\.");
    int i = 0;
    // set index to first non-equal ordinal or length of shortest version string
    while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
      i++;
    }
    // compare first non-equal ordinal number
    if (i < vals1.length && i < vals2.length) {
        int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
        return Integer.signum(diff);
    }
    // the strings are equal or one string is a substring of the other
    // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
    return Integer.signum(vals1.length - vals2.length);
}

它没有抛出任何错误,但Alexa并没有说出我可以在控制台日志中看到响应的任何内容。

如果我在功能结束时添加: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(); } }); ,那么Alexa将根据此意图说出“Whatever”。

1 个答案:

答案 0 :(得分:0)

让我自己解决了:

而不是:

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

你必须退货,例如:

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