Dialogflow v2 Actions on Google response timeout

1k views Asked by At

Hi I have a timeout problem to get a json response; I am using google places API to look for the closest location.

Could anyone help me with this? Thanks.

const PlaceSearch = require("./node_modules/googleplaces/lib/NearBySearch.js");
const PlaceDetailsRequest = require("./node_modules/googleplaces/lib/PlaceDetailsRequest.js");

app.intent('Ask Location', conv => {conv.ask(new Permission({context: 'To start',permissions: 'DEVICE_PRECISE_LOCATION',}));});

app.intent('geolocation.intent', (conv,params,granted) =>{   
if(granted){    
    var coordinates = conv.device.location.coordinates;
    var location = [coordinates.latitude, coordinates.longitude];
    var searchParameters = {
        location: location,
        name:'Store Name',
        radius:10000    
    };     
    var config = {
        apiKey:'#####',
        outputFormat:'json'
    };
    var placeSearch = new PlaceSearch(config.apiKey, config.outputFormat);
    var placeDetailsRequest = new PlaceDetailsRequest(config.apiKey, config.outputFormat);     
    placeSearch(searchParameters, function (error, search_response) {
        if(search_response.status === 'OK'){             
            placeDetailsRequest({reference: search_response.results[0].reference}, function (error, details_response) {
            conv.ask(`Your closest store is at ${details_response.result.formatted_address}.`);
            });            
        }
    });
}
});
1

There are 1 answers

0
Juvenal Jaimes On

I solved the issue using a request to Google API via URL; and using a promise.

const request = require("request");
app.input("geolocation.intent", conv => {
  return new Promise((resolve, reject) => {
    ...
    request(options, (error, response, body) => {
      ...
      if (error) {
        ...
        reject(...);
      } else {
        ...
        resolve(...);
      }
  }).then(result => {
    const address = result.address;
    conv.ask('Your closest store is...');
  }).catch(error => {
    conv.close('Error in Promise');
  });
});

What I learned is that in Dialogflow API v2 you need to use promises when you make a request.