Reputation: 39
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.
Upvotes: 2
Views: 1943
Reputation: 1283
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
Upvotes: 1
Reputation: 11970
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',
]));
});
Upvotes: 0