1

I am trying to retrieve the location of user in Dialogfow. Following docs and blogs, this is what I have written in index.js file of fulfilment.

const functions = require('firebase-functions');
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 });
  //const agent = new dialogflow();
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

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

  const requestPermission = (agent) => {
    //agent.add("Locaation access!");
    //agent.add(new Permission({permissions:'NAME', context:''}));
    agent.requestSource = agent.ACTIONS_ON_GOOGLE;
    const conv = agent.conv();
    conv.ask(new Permission({
        context: '',
        permissions: 'DEVICE_PRECISE_LOCATION',
    }));
    console.log(conv);
    agent.add(conv);
    agent.add("Apple");
  };

  const userInfo = (agent) => {
    if(agent.isPermissionGranted()){
        const address = agent.getDeviceLocation().address;
        if (address){
          agent.add("You are at ${address}");
        }
        else{
            agent.add("Cannot locate you");
        }
    }
    else{
        agent.add("Cannot locate you");
    }
  };

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('request_permission',requestPermission);
  intentMap.set('user_info', userInfo);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

EDIT This is JSON response I get. Raw API response:

{
  "responseId": "RESPONSE-ID",
  "queryResult": {
    "queryText": "locate",
    "action": "request_permission",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "platform": "ACTIONS_ON_GOOGLE",
        "simpleResponses": {
          "simpleResponses": [
            {
              "textToSpeech": "Apple",
              "displayText": "Apple"
            }
          ]
        }
      }
    ],
    "webhookPayload": {
      "google": {
        "systemIntent": {
          "intent": "actions.intent.PERMISSION",
          "data": {
            "optContext": "",
            "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
            "permissions": [
              "DEVICE_PRECISE_LOCATION"
            ]
          }
        },
        "expectUserResponse": true
      }
    },
    "intent": {
      "name": "projects/MY_PROJECT_NAME",
      "displayName": "request_permission"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {
      "webhook_latency_ms": 3564
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "message": "Webhook execution successful"
  }
}

Fulfillment request:

{
  "responseId": "RESPONSE-ID",
  "queryResult": {
    "queryText": "locate",
    "action": "request_permission",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "PROJECT-NAME",
      "displayName": "request_permission"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {
    "payload": {}
  },
  "session": "SESSION-ID"
}

Fulfillment response:

illmentMessages": [
    {
      "platform": "ACTIONS_ON_GOOGLE",
      "simpleResponses": {
        "simpleResponses": [
          {
            "textToSpeech": "Apple",
            "displayText": "Apple"
          }
        ]
      }
    }
  ],
  "payload": {
    "google": {
      "expectUserResponse": true,
      "systemIntent": {
        "intent": "actions.intent.PERMISSION",
        "data": {
          "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
          "optContext": "",
          "permissions": [
            "DEVICE_PRECISE_LOCATION"
          ]
        }
      }
    }
  },
  "outputContexts": []
}

This response is correct I think. But nothing is added on screen. It shows this on screen, simulator

There are not any errors in console as well. But I am not getting anything when I try the agent. In raw response, the action.intent.PERMISSION is present but why it is not added or agent asked for yes/no?

Nikhil Jagtap
  • 110
  • 1
  • 13
  • Can you update your question to show what response you're getting (I assume in the simulator?) and what you're seeing or hearing when the `requestPermission` intent handler is triggered? (Is it triggered?) Adding screen shots of the Intents in question in Dialogflow may also help. – Prisoner Dec 09 '19 at 14:20
  • @Prisoner Thank you, sir. I have updated the question with JSON responses and the screenshot of a simulator. – Nikhil Jagtap Dec 10 '19 at 09:24

1 Answers1

2

The location permissions only work if you're using Actions on Google with the Google Assistant. You also need to test it using the Actions on Google simulator - click where it says "See how it works in Google Assistant".

It doesn't work with the other Dialogflow integrations (such as Facebook or Slack) and won't work with the Dialogflow simulator (the simulator on the right hand side of the page).

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • What do I have to change then? I cannot use actions on Google and Webhook together right? ( It doesn't work in Google assistant as well. It asks me to check adminstrator preferences. I think this has to do with my domain name admin ) – Nikhil Jagtap Dec 10 '19 at 12:57
  • You can certainly use AoG, Dialogflow, and webhooks. But the real question is - are you *trying* to write an Action? If you're not, then you can't use the AoG permissions. If you are, you should be using the AoG simulator to test and may need to make sure your account is permitted to do so. – Prisoner Dec 10 '19 at 13:18
  • Ok, thanks a lot. Marking this as correct answer as it appears to solve my problem. I'll update in case it doesn't work even after contacting admin. – Nikhil Jagtap Dec 10 '19 at 13:21