I am new to Dialogflow. How to ask users for their location permission and how to get their current location if they agree to share it.
Asked
Active
Viewed 1,866 times
2
-
Post some code you have tried – farooq Oct 29 '19 at 09:07
-
Are you just using dialogflow or are you building a bot for one of the integrations such as Google Assistant, Facebook, etc.? – Jordi Oct 29 '19 at 12:13
2 Answers
1
I followed the following link and it works perfectly link to post
So the idea is to create two intent, one for asking permission the other for getting user permission. For both intents Enable Webhook.
Add the following code in Fulfillment for first intent:
app.intent('location', (conv) => {
conv.data.requestedPermission = 'DEVICE_PRECISE_LOCATION';
return conv.ask(new Permission({
context: 'to locate you',
permissions: conv.data.requestedPermission,
}));
});
For the second intent add the following code:
app.intent('user_info', (conv, params, permissionGranted) {
if (permissionGranted) {
const {
requestedPermission
} = conv.data;
if (requestedPermission === 'DEVICE_PRECISE_LOCATION') {
const {
coordinates
} = conv.device.location;
// const city=conv.device.location.city;
if (coordinates) {
return conv.close(`You are at ${coordinates.latitude}`);
} else {
// Note: Currently, precise locaton only returns lat/lng coordinates on phones and lat/lng coordinates
// and a geocoded address on voice-activated speakers.
// Coarse location only works on voice-activated speakers.
return conv.close('Sorry, I could not figure out where you are.');
}
}
} else {
return conv.close('Sorry, permission denied.');
}
});
For detailed explanation you can refer to the link above

Gray_Rhino
- 1,153
- 1
- 12
- 31
-
thks! but i have this error No handler for requested intent. maybe i miss something. – Inc Bot Oct 30 '19 at 09:33
-
thanks, but i have this error > **No handler for requested intent** i think i miss something, my index.js end " exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);" can you help me? – Inc Bot Oct 30 '19 at 09:39
-
If you're getting an error with your code, please [update your original question](https://stackoverflow.com/posts/58604105/edit) to include your code and screen shots of the Intents that you think should be triggering it. – Prisoner Oct 30 '19 at 13:02
0
Getting Location is not generally available in every Dialogflow integration, but to do it in Actions on Google you can follow this guide:
app.intent('Permission', (conv) => {
const permissions = ['NAME'];
let context = 'To address you by name';
// Location permissions only work for verified users
// https://developers.google.com/actions/assistant/guest-users
if (conv.user.verification === 'VERIFIED') {
// Could use DEVICE_COARSE_LOCATION instead for city, zip code
permissions.push('DEVICE_PRECISE_LOCATION');
context += ' and know your location';
}
const options = {
context,
permissions,
};
conv.ask(new Permission(options));
});
After getting the permission, you can then handle it in your next handler:
app.intent('Permission Handler', (conv, params, confirmationGranted) => {
// Also, can access latitude and longitude
// const { latitude, longitude } = location.coordinates;
const {location} = conv.device;
const {name} = conv.user;
if (confirmationGranted && name && location) {
conv.ask(`Okay ${name.display}, I see you're at ` +
`${location.formattedAddress}`);
} else {
conv.ask(`Looks like I can't get your information.`);
}
conv.ask(`Would you like to try another helper?`);
conv.ask(new Suggestions([
'Confirmation',
'DateTime',
'Place',
]));
});

Nick Felker
- 11,536
- 1
- 21
- 35