首页 > 解决方案 > 如何在 Dialogflow 聊天机器人中获取用户的当前位置?

问题描述

我正在尝试在 Dialogflow 中获取用户位置,但到目前为止我无法获取它。

我找到了显示如何使用 Google 帮助在 Dialogflow 中获取位置的信息,例如本文。问题是我只想用聊天机器人来做这件事并在那里得到回应。我也尝试了下面的代码,但仍然无法正常工作:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');


const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Permission} = require('actions-on-google');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements


exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

   function requestPermission(agent) {
    agent.requestSource = agent.ACTIONS_ON_GOOGLE;
    let conv = agent.conv();
    console.log('conv '+conv);
    conv.ask(new Permission({
    context: 'to locate you',
    permissions: 'DEVICE_PRECISE_LOCATION',
    }));
    agent.add(conv); // Please add this

    }

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }



  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);

  intentMap.set('request_permission', requestPermission);

  agent.handleRequest(intentMap);
});

我添加了一个意图调用“request_permission”。我将不胜感激任何帮助或建议。

标签: dialogflow-esdialogflow-es-fulfillment

解决方案


Dialogflow 中没有通用的解决方案来获取位置。并非所有使用 Dialogflow 的系统都有自动传输位置的方法,其他系统通过消息使用不同的方法。

Actions on Google 的解决方案仅适用于 Google 助理。


推荐阅读