0
votes

Hi i'm trying to get device coarse location by getting permission from user.But i required to get device location details for each webhook request without asking permission again and again.So I'm bit confused how to do this one.

Here is the below code which i tried.

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

1 Answers

0
votes

Please check the helper functions here. You need to do the following:

  • Create an intent to ask for permission.
  • In that intent, ask the permission you want
  • Create second intent to capture user's response to intent by putting the Dialogflow event actions_intent_PERMISSION to that intent.
  • In the webhook handle of the second intent check for confirmation.

Ask Permission in First Intent

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

Capture result in Second Intent

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

For exact understanding with code example, check out this GitHubb example link.