【问题标题】:Alexa intent is not waiting for the api responseAlexa 意图不等待 api 响应
【发布时间】:2017-10-11 10:26:25
【问题描述】:

我们正在使用 alexa-app 开发 alexa 技能,我们的意图之一是尝试从 facebook 获取相册,并且在成功/失败时我们希望 alexa 做出相应的响应。但意图不是等待 FB 调用完成。下面是我们使用的代码sn-p:

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 没有说出我可以在控制台日志中看到响应的任何内容。

如果我在函数末尾添加:res1.say("Whatever!"),那么 Alexa 会说出“随便”来响应这个意图。

【问题讨论】:

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


    【解决方案1】:

    我自己解决了:

    而不是这个:

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

    您必须退回它,例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 2017-01-28
      • 2017-07-21
      • 2018-08-04
      • 2022-07-01
      相关资源
      最近更新 更多