1

I'm trying to create a chatbot in DialogFlow that checks the status of your insurance claim.

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. However, I consistently get [empty response] from DF, despite getting the correct data from the mock API. 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:

error messages

Captain Chaos
  • 189
  • 2
  • 2
  • 13
  • I think the problem is that the API call is just taking too long, and DialogFlow wants to answer and doesn't wait. So, I think I either need to speed up my call, or find a way to make DialogFlow wait, either in the code or in settings, if that is possible... – Captain Chaos Jun 11 '20 at 21:47

2 Answers2

1

According to documentation, Dialogflow's wait time is 5 seconds. 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.
You can check his post for me info

Gray_Rhino
  • 1,153
  • 1
  • 12
  • 31
1

The issue is that, although you're doing all your processing through a Promise, you are not returning that Promise in your Handler. 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.

Fortunately, in your case, you may be able to do this by just adding the return statement before callClaimsApi.

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.

Prisoner
  • 49,922
  • 7
  • 53
  • 105