Handling Permissions with DialogFlow and Actions On Google

Wassim Chegham
Google Developer Experts
4 min readOct 22, 2017

--

Update 2019: The code samples used in this blog are showing the SDK v1 which has been deprecated. Please refer to this link for the SDK v2 migration process. If you have any questions, please post them on Stack Overflow or on the Google+ community.

When building your actions for the Google Assistant, you might need to get the user’s name or location in order to provide a rich user experience. Since I got asked this question a lot, I thought it’d be interesting to walk you through the necessary steps to handle this use case. Let’s dive in.

To get the user’s name or location she saved on her Google account, you’ll need to ask her to grant you access; and for that, you’ll need to engage a conversation with her asking her what kind of information you need to access.

Let’s first write down the “happy path” for this conversation, between our user “Lilia” and our action “R2D2”:

  • Lilia: Ask R2D2 to locate me
  • R2D2: To locate you, I’ll just need to get your street address from Google. Is that ok?
  • Lilia: Sure
  • R2D2: You are at Googleplex, Mountain View, CA 94043, United States.

Now, let’s open up DialogFlow and design this conversation.

tl;dr;

Are you in hurry? Here are the steps to follow in order to handle the permissions workflow:

  1. Create an intent that will be triggered by DialogFlow and whose role is to send an action to your application.
  2. In your application, use the askForPermission() helper to let Actions On Google ask the user to grant or decline your application accessing her private info.
  3. Create a second intent that will be triggered by the “actions_intent_PERMISSION” special event, and whose role is to send an action to your application.
  4. In your application, use the isPermissionGranted() helper to check the user’s response. Use the right helper to access the user’s info.

Designing the conversation in DialogFlow

In order to ask and then access the user’s location, we’ll need to create two intents:

  1. request_permission: this intent will be triggered when the user asks for the location.
  2. user_info: this intent will be triggered by a special event from Actions On Google (read below).

Here is the server side code for this demo application (using a Cloud Function for Firebase):

The “request_permission” intent

In this intent, we’ll provide a set of sample utterances that DialogFlow will use to match and trigger this intent, ie: “find my position” or “locate me”.

Once this intent is triggered, we’ll send an action name “request_permission” to our application. You can name this action whatever you want; but just make sure you use the same name in DialogFlow and the code.

On the application side, we’ll register the “request_permission” action that’s sent by DialogFlow. We’ll use this action to call the askForPermission helper method providing both the reason why we want to access the permission, ie: “To locate you”, and the DEVICE_PRECISE_LOCATION permission. See line 8–10 in the code below:

Invoking this method will send a PLACEHOLDER_FOR_PERMISSION response to Actions On Google which will trigger the “To locate you, I’ll just need to get your street address from Google. Is that ok?” message.

The “user_info” intent

This intent will be triggered “automagically” when the user responds to the question: “To locate you, I’ll just need to get your street address from Google. Is that ok?”.

In fact, when Actions On Google asks the question, and the user responds “yes” or “no” (grants or declines); Actions On Google will then send an event called “actions_intent_PERMISSION” to DialogFlow. We’ll use that event to trigger this particular intent. Once the intent is triggered, we’ll make sure to send the “user_info” action to our application.

In the application, we’ll register the “user_info” action and make sure to check whether the user has granted or declined the permissions. For that, we call the isPermissionGranted helper method. We also use the getDeviceLocation method to access the user’s location from Google. See line 12–27 in the code below:

That’s it

You can test your action in the Actions Simulation:

Bonus

Did you know? You can build and manage fulfillments directly in DialogFlow via Cloud Functions for Firebase. So just use the code provided above in the inline editor and hit deploy. You are now ready to test your action for the Google Assistant.

Follow me on Twitter @manekinekko to learn more about the Assistant and the web platform.

--

--

Wassim Chegham
Google Developer Experts

#javascript @ Microsoft ★ GDE for Angular, Action On Google and GCP at Google ★ Member of Node.js Foundation and core contributor ★ Follow me @manekinekko