# Get Current Location of a user using Helper Intents in Actions on Google

*In this post, you will learn how to get the current location of a user for your action using Dialogflow Helper Intents*

### Objectives

1. What is Google Assistant
    
2. What is Cloud Function
    
3. Create a new action in Google Assistant
    
4. Custom action using Dialogflow
    
5. Fulfilment logic to Cloud Function
    
6. Test your action
    

### Prerequisites

* Google Cloud Account
    
* Pair of headphones or turning the volume up on your computer is recommended.
    

### 1\. What is Google Assistant

[Google Assistant](https://assistant.google.com/#?modal_active=none) is a personal voice assistant that offers a host of actions and integrations. From sending texts and setting reminders, to ordering coffee and playing music, the 1 million+ actions available suit a wide range of voice command needs.

### 2\. What is Cloud Function

[Google Cloud Functions](https://cloud.google.com/functions/docs/) is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment.

### 3\. Creating a new action in Google Assistant

Regardless of the Assistant application, you’re building, you will always have to create an Actions project so your app has an underlying organizational unit.

Open the [Actions on Google Developer Console](http://console.actions.google.com/) in a new tab. You should be looking at a clean Actions console that resembles the following (If you are a new user (:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056513207/9011cbdf-32f6-4884-9dc4-9c1c4a5d553d.png align="left")

Actions on Google Console

Click **Add/import project** and agree to Actions on Google’s terms of service when prompted.

Click into the Project Name field and add the project name **Location Tracker**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056514751/a0d07037-8478-45c1-984e-a6848a14a89b.png align="left")

Adding project name

Once the project is created you will see a welcome screen like below, scroll down and select **Conversational**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056516698/5f24e761-9592-4765-a660-c1d6a3fba4a8.png align="left")

Conversational

Once the Conversational is selected click the **Invocation** option at the left bar and set the **Display Name** as **Location Tracker**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056518489/1cff1337-6a03-4b1b-90a3-80da3e378557.png align="left")

Invocation Name

Once the **Invocation** name is given click **Actions** and select **Get Started** to Build your first action

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056519951/5067c44f-5c92-4bb9-b089-d39e023f2a8f.png align="left")

Actions

Now Select **Custom intent** and click **Build**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056521450/facc2d61-8760-4c4d-b92f-e0393a7a7a66.png align="left")

Custom intent

### 4\. Custom action using Dialogflow

It will now navigate to the **Dialogflow** console. In the Dialogflow console check whether the agent name is **Location Tracker** or not

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056523051/d7da08a0-ebc0-4425-b810-7250f8b4c9e4.png align="left")

Dialogflow agent

Now click **Create** if the agent name is the same as your **Action** name

Once the agent is created on the left side you will see **Intents**. Click **Intents**

By default, you could see two intents namely

* Default Welcome Intent
    
* Default Fallback Intent
    

Click **Default Welcome Intent**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056524767/e53f2acf-03a1-4f6a-87dd-9722453c6a9a.png align="left")

Default Welcome Intent

> Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

Now just scroll down you could see some Responses like the below image

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056526274/f5b8069b-1805-4c87-a05b-66e8232dd46b.png align="left")

Response

Delete all the default response and create a new response like `Hi! Welcome to Location Tracker`

Once the response is added click **Save**

Now click the **+** button in **Intents** and give a new intent name like `get_currentlocation`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056527918/01ea3ba6-e4d2-4448-ac91-05cbe036dc6f.png align="left")

Get Current Location Intent

In the **Training Phrase** add some location by default the **@sys.location** entity should match up like the above image.

Now scroll down and select Fulfilment section and Enable webhook call for the intent

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056529744/3a970bea-9420-4e42-9be4-2c6d14864aaf.png align="left")

Enable Webhook

Click **Save** and then click **Test** tab you could see the message as `Hi! Welcome to Location Tracker`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056531010/3161460d-fb96-451a-95db-5056e3c42e05.png align="left")

Testing your action

### 5\. Fulfilment logic to Cloud Function

Now we will create the firebase cloud function, Go to your local terminal and create a new directory named **Location Tracker** and navigate to that directory

Once navigated initialize Firebase Cloud Function using the command `firebase init` and then select Cloud Function and select the default project as **Location Tracker**

Once the project is select **Firebase CLI** will create the below files and **node\_modules** folder

* index.js
    
* package.json
    

Now install the `actions-on-google` plugin using the command `npm i actions-on-google`

Once the plugin is installed, Open a text editor and select `index.js` file

Import the dependencies like below by removing the existing code

```bash
const functions = require("firebase-functions");  
const { dialogflow, Permission, SimpleResponse } = require("actions-on-google");  
  
const app = dialogflow();
```

Now what we will do is when the user opens the action we will get permission from the user to get the current location of the user from Google.

If the user says **Yes** then we will take the **Latitude** and **Longitude**

For that, we need to refactor the `Default Welcome Intent` in Dialogflow

In the Default Welcome Intent remove the existing Training phrases and add the below

where am i  
locate me

Once it is added scroll down and enable the **Webhook.**

Now go to the `get_current_location` intent and click Events and select `actions_intent_permission` and remove all the Training phrases

Now go to your cloud functions index.js file and create the `Default Welcome Intent` like below

```bash
app.intent("Default Welcome Intent", conv => {  
  conv.data.requestedPermission = "DEVICE_PRECISE_LOCATION";  
  conv.ask(new SimpleResponse('Welcome to location tracker'));  
  return conv.ask(  
    new Permission({  
      context: "to locate you",  
      permissions: conv.data.requestedPermission  
    })  
  );  
});
```

In the above code, we are getting the user permission to take the current location of the user from Google

Once the user says `Yes` or `No` it will trigger the `get_current_location` intent like below

Now deploy this code using the command `firebase deploy` . Once your code is deployed you will get a Webhook URL.

Copy the webhook URL and paste the webhook URL in Dialogflow

Now if you run the action you could see an output like below

<iframe src="https://www.youtube.com/embed/T1acit929bw?feature=oembed" width="700" height="393"></iframe>

Source: Nidhinkumar

Now we are able to get the Latitude and Longitude of a particular user. We can get the Address of the user using Latitude and Longitude using geocoder reverse

To show the address we will install the plugin `npm i node-geocoder` . Once the plugin is installed import the dependency like below

const NodeGeocoder = require("node-geocoder");

Once imported go to the `get_current_location` intent and add the below code

Now if you deploy the latest changes you will get the output like below

<iframe src="https://www.youtube.com/embed/t2i8THGCrQc?feature=oembed" width="700" height="393"></iframe>

In the above demo, you could notice that before showing the address the conversation is closed.

#### Option 2

If you don’t want to use reverse geolocation Google Assistant Helper Intents will show the formatted address add the below code in get\_currentlocation intent

```bash
const { location } = conv.device;  
conv.ask(new SimpleResponse(`Your address is ${location.formattedAddress}`))
```

### 6\. Test your action

Deploy your action you could see the output like below

<iframe src="https://www.youtube.com/embed/l6kwryBBr5E?feature=oembed" width="640" height="480"></iframe>

Source: Nidhinkumar

### Reference Links

* Dialogflow get a current location — [Click here to view](https://cloudops2pm.com/2018/07/26/get-location-in-google-home-assistant-app-using-dialogflow-v2-api/)
    
* Helper Intents — [Click here to view](https://developers.google.com/assistant/conversational/helpers)
    

### Congratulations!

You have learned how to get the current location of the user from Google using Helper Intent. Happy Learning :)
