Search code examples
dialogflow-esactions-on-googledialogflow-es-fulfillment

Cannot see the location permission in Dialogflow


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?


Solution

  • 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).