Device location in dialogflow (api.ai) webhook request

Caedmon picture Caedmon · Nov 25, 2017 · Viewed 7k times · Source

I'm working on a Dialogflow chat bot for Google Assistant, in the webhook request JSON example here:

https://developers.google.com/actions/reference/v1/dialogflow-webhook

the webhook receives device data including its location:

"device": {
       "location": {
           "coordinates": {
               "latitude": 123.456,
               "longitude": -123.456
           },
           "formatted_address": "1234 Random Road, Anytown, CA 12345, United States",
           "zip_code": "12345",
           "city": "Anytown"
       }
   }

For some reason my webhook is not receiving any device data (the "device" key is missing completely). Does anyone know what I'm doing wrong or how I can include this data?

Answer

Mapsy picture Mapsy · Nov 25, 2017

You'll need to ask for the user's location permissions first. Then their location will be made available in the request JSON. Here's a good example of how to do it.

Note, you can ask for two different kinds of location:

DEVICE_PRECISE_LOCATION will give you their exact latitude and longitude, and DEVICE_COARSE_LOCATION will provide you with broader location information. It's generally good practice to only use the least intrusive permissions your applications need, so if your app can settle for a coarse location, then you should opt for it.

Note that you're looking at the v1 API, which is deprecated and scheduled to be removed in the near future. Make sure you're working with the v2 documentation.