我是Dialogflow的新手。如何要求用户提供位置许可,以及如果他们同意共享该位置,如何获得他们的当前位置。
答案 0 :(得分:1)
我点击了以下链接,并且效果很好link to post
因此,该想法是创建两个意图,一个用于请求许可,另一个用于获取用户许可。对于这两种意图,请启用Webhook。
首先在实现中添加以下代码:
app.intent('location', (conv) => {
conv.data.requestedPermission = 'DEVICE_PRECISE_LOCATION';
return conv.ask(new Permission({
context: 'to locate you',
permissions: conv.data.requestedPermission,
}));
});
对于第二个意图,请添加以下代码:
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.');
}
});
有关详细说明,请参阅上面的链接
答案 1 :(得分:0)
并不是每个Dialogflow集成都通常提供“获取位置”,但是要在Google的“操作”中进行定位,您可以遵循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));
});
获得许可后,您可以在下一个处理程序中对其进行处理:
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',
]));
});