Reputation: 69
My problem is that in Dialogflow, My intent calls on a webhook THAT calls on a method. The method "CheckCosts" is running as all the console.logs work. BUT the webhook is not retrieving anything from the output of the "CheckCosts" method.
Below is the webhook. This is the webhook thats called upon when the intent is submitted.
function checkCostsIntent(app) {
let origin = parameter.originAddress;
let destination = parameter.destinationAddress;
let costString = '';
/*let testing = CheckCosts(origin, destination);
app.add(testing);*/
CheckCosts(origin, destination).then((cost)=>{
console.log(cost);
costString = cost;
});
app.add(costString);
}
Below is the method that the webhook will call upon.
function CheckCosts(originAddress, destinationAddress) {
return new Promise((resolve, reject) => {
let defaultPath = 'maps.googleapis.com/maps/api/distancematrix/json?units=metric';
let APIKey = '&key=<API KEY>';
let origin = originAddress;
let destination = destinationAddress;
let newPath = defaultPath + '&origins=' + origin + '&destinations=' + destination + APIKey;
let url = 'https://' + newPath;
console.log(url);
https.get(url, (res) => {
let body = '';
res.on('data', (d) => { body += d; }); // store each response chunk
res.on('end', () => {
let response = JSON.parse(body);
console.log(response);
let distance = response.rows[0].elements[0].distance.text;
let travelTime = response.rows[0].elements[0].duration.text;
let distanceValue = response.rows[0].elements[0].distance.value;
let travelTimeValue = response.rows[0].elements[0].duration.value;
var ref = database.ref().child('price');
return ref.once('value').then(function (snapshot) {
let baseFair = snapshot.child('base').val();
let perKm = snapshot.child('perKm').val();
let perMin = snapshot.child('perMin').val();
let distanceCost = distanceValue / 1000 * perKm;
let timeCosts = travelTimeValue / 60 * perMin;
let cost = baseFair + distanceCost + timeCosts;
cost = cost.toFixed(2);
console.log('Total costs of distance is $' + distanceCost);
console.log('Total costs of time is $' + timeCosts);
console.log('The total cost of the ride is $' + cost);
let output = 'Distance between ' + origin + ' and ' + destination + ' is ' + distance + '. Time taken is ' + travelTime + '. And the total cost of the ride is $' + cost;
console.log(output);
resolve(output);
},
function (error) {
console.log('error' + error.code);
});
});
response.on('error', (error) => {
console.log(`error calling the Distance Matrix API:' ${error}`);
reject('fail');
});
});
});
}
Basically the main problem is that im not getting the resolve(output)
in my webhook as i get nothing from the console.log()
in my checkCostsIntent()
webhook.
Is there a problem with my Return promise? or my resolve?
Upvotes: 1
Views: 95
Reputation: 50701
The Intent dispatcher in the Dialogflow code requires to you return a Promise if you're doing any async work, so it knows to wait for the Promise to complete before it sends the response back to the user.
Although CheckCosts
is returning a Promise, and you're treating the return value as a promise in your Intent Handler (you have a then()
section), you need to do two more things:
app.add()
in the then()
portionUpvotes: 0
Reputation: 69
Its ok. I just needed to add "return" infront of my code
return CheckCosts(origin, destination).then((cost)=>{
app.add(cost);
});
and it works
Upvotes: 2