首页 > 解决方案 > DialgoFlow - 获取用户位置

问题描述

我是 Dialogflow 的新手。如何向用户询问他们的位置许可,以及如果他们同意共享,如何获取他们当前的位置。

标签: dialogflow-esactions-on-googledialogflow-es-fulfillment

解决方案


我点击了以下链接,它完美地链接到发布
所以这个想法是创建两个意图,一个用于请求许可,另一个用于获取用户许可。为这两个意图启用 Webhook。在 Fulfillment 中为第一个意图添加以下代码:

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.');
}
});

详细解释可以参考上面的链接


推荐阅读