代码在 Dialogflow 实现中不起作用

code does not work in Dialogflow fulfillment

我有一些测试过的代码,它在我的环境中工作,我将 agent.add(value) 更改为 console.log(value),然后打印结果。但是,当我在 dialogflow fulfillment 中尝试它时,它不会 运行 响应并且主体始终未定义。

有谁知道什么可以使 Dialogflow Fulfillment 中的代码不 运行。谢谢。

 function SearchKnowwledgeBaseMethod(intention){
    var username = "something";
    var password = "something";
    var auth = "Basic " + Buffer.from(username + ":" + password).toString('base64');
    var result;
    return new Promise(function(resolve, reject){
      rq(
        {
          url : `https://xxxxxx/api/now/table/kb_knowledge?sysparm_query=textCONTAINS${intention}`,
          headers : {
            "Authorization" : auth
          }
        },
        function (error, response, body) {
          if (!error && response.statusCode == 200) {
            if (typeof body !== undefined) {
              const obj = JSON.parse(body);
              if (obj.result.length > 0){
                result = obj.result[0].text;
              }
            }
          }else {
            reject("I have a problem");
          }
          resolve(result);
        });
    });
  }

  function SearchKnowledgeBase(agent){
    var intention = agent.parameters.intention;
        return SearchKnowwledgeBaseMethod(intention).then(function(result){
            if (result !== undefined){
                console.log(result);
                return agent.add(result);
            } else {
                var stringArray = intention.split(' ');
                stringArray.forEach(function (item) {
                    return SearchKnowwledgeBaseMethod(item).then(function(answer){
                        console.log(answer);
                        return console.log(answer);
                    });
                });
            }
        });
    }

  let intentMap = new Map();
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('search_knowledge_base', SearchKnowledgeBase);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);

有两件事需要处理。

if (result !== undefined){
                console.log(result);
                agent.add(result); // no need to return here you are already returning a promise just do agent.add(result)
            } else {
                var stringArray = intention.split(' ');
                // I don't think you can return promises in loop for dialogflow, just return one promise 
                stringArray.forEach(function (item) {
                    return SearchKnowwledgeBaseMethod(item).then(function(answer){
                        console.log(answer);
                        return console.log(answer);
                    });
                });
            }

我找到了问题的答案,很简单。原因是我使用的是免费版的 Firebase,所以它不允许调用外部 API,当我切换到按需付费计划时,API 请求工作正常。 感谢 Youtube 上的此视频:https://www.youtube.com/watch?v=n4IPOeFCDxI&t=591s