简体   繁体   English

对话流返回一个空响应,尽管有数据

[英]dialogflow returning an empty response, despite having the data

I'm trying to create a chatbot in DialogFlow that checks the status of your insurance claim.我正在尝试在 DialogFlow 中创建一个聊天机器人来检查您的保险索赔状态。

I have set up a call to an external API (mock), and I use a promise to wait for the response and then return it.我已经设置了对外部 API(模拟)的调用,并且我使用 promise 等待响应然后返回它。 However, I consistently get [empty response] from DF, despite getting the correct data from the mock API.但是,尽管从模拟 API 获得了正确的数据,但我始终从 DF 获得[empty response] Is it just taking too long?只是时间太长了吗?

Below is the relevant code:下面是相关代码:


    var callClaimsApi = new Promise((resolve, reject)=>{
      try{
        https.get('https://MOCKAPIURL.COM', (res) => {
            res.setEncoding('utf8');
            let rawData = '';
            res.on('data', (chunk) => { rawData += chunk; });
            res.on('end', () => {
            resolve(JSON.parse(rawData));   
      });
    });} catch(e){reject(e.message);}
  }); 

  function checkClaims(agent){ 
    callClaimsApi
      .then(function(fulfillment){
        console.log("fulfillment name: " + fulfillment.name);
        agent.add("It looks like you want to find a claim for " + fulfillment.name);
      })
      .catch(function(error){console.log(error);});
  }

intentMap.set('checkClaims', checkClaims);    

here is the output from the logs:这是日志中的 output:

错误信息

According to documentation, Dialogflow's wait time is 5 seconds.根据文档,Dialogflow 的等待时间为 5 秒。 If you can optimize your code that would be awesome.如果你能优化你的代码,那就太棒了。 There are some tricks to make DF wait for longer using Follow-Up events or use one intent to request -> respond to the user with some confirmation (ex. Can you wait for 3 seconds? Yes/No) -> By this time the request will be available so you can send it in the next message.有一些技巧可以让 DF 等待更长的时间,使用后续事件或使用一个意图来请求 -> 用一些确认来响应用户(例如,你能等待 3 秒吗?是/否) -> 到这个时候请求将可用,因此您可以在下一条消息中发送它。
You can check his post for me info您可以查看他的帖子以获取我的信息

The issue is that, although you're doing all your processing through a Promise, you are not returning that Promise in your Handler.问题是,尽管您正在通过 Promise 进行所有处理,但您并没有在处理程序中返回 Promise。 The library needs the Promise so it knows there is an asynchronous operation going on and that it should wait till that operation is completed before sending a reply.该库需要 Promise,因此它知道正在进行异步操作,并且它应该等到该操作完成后再发送回复。

Fortunately, in your case, you may be able to do this by just adding the return statement before callClaimsApi .幸运的是,在您的情况下,您可以通过在callClaimsApi之前添加return语句来做到这一点。

You may also wish to look into using a library such as axios to do the http call, since it has promise support built-in.您可能还希望研究使用诸如axios之类的库来执行 http 调用,因为它内置了 promise 支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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