如何在Google的操作中为每个Webhook请求获取设备的粗略位置

时间:2018-10-17 04:55:33

标签: dialogflow actions-on-google

嗨,我试图通过获取用户许可来获取设备的粗略位置。但是我需要在不反复询问许可的情况下获取每个Webhook请求的设备位置详细信息,所以我有点困惑如何做到这一点。

这是我尝试的以下代码。

const {Permission} = require('actions-on-google');
const {WebhookClient} = require('dialogflow-fulfillment');
const agent = new WebhookClient({ request: req, response: res });
function x(agent){
    conv.ask(new Permission({context:'To Locate You',permissions:'DEVICE_COARSE_LOCATION'}));
}

        function userinfo(agent){
                var conv=agent.conv();
                var resp=conv.arguments.get('PERMISSION');
            console.log(conv.device.location);
            if(resp){

                        var country=conv.device.location.country;
                        var speech="you are located in "+country;
                        conv.ask(speech);
                        agent.add(conv);
                }else{
                    conv.ask('Sorry, I could not figure out where you are');
                    agent.add(conv);
            }
        }

1 个答案:

答案 0 :(得分:0)

请检查辅助功能here。您需要执行以下操作:

  • 创建请求许可的意图。
  • 出于这种意图,请问您想要的许可
  • 通过将Dialogflow事件actions_intent_PERMISSION放入该意图来创建第二个意图,以捕获用户对意图的响应。
  • 在第二个意图检查的webhook句柄中进行确认。

首先要获得许可

app.intent('FIRST_INTENT_NAME', (conv) => {
  // Choose one or more supported permissions to request:
  // NAME, DEVICE_PRECISE_LOCATION, DEVICE_COARSE_LOCATION
  const options = {
    context: 'To address you by name and know your location',
    // Ask for more than one permission. User can authorize all or none.
    permissions: ['NAME', 'DEVICE_PRECISE_LOCATION'],
  };
  conv.ask(new Permission(options));
});

第二意图捕获结果

app.intent('SECOND_INTENT_NAME', (conv, params, confirmationGranted) => {
  const {name} = conv.user;
  if (confirmationGranted) {
    if (name) {
      conv.ask(`I'll send the driver you're way now ${name.display}.`);
    }
  }
});

有关代码示例的确切信息,请查看this GitHubb example link

相关问题