This post was published in Chatbots Magazine: Personalize Google Assistant skill with user data.
This post is a part of series about building the personal assistant app, designed for voice as a primary user interface. More posts in series:
In first post we have built very first version ofĀ WaterLog, a Google Assistant skill which lets us track daily water intake by our voice or text written in natural language.
https://assistant.google.com/services/a/id/12872514ba525cc6/
Personalising experience with userĀ data
Today weāll make use of permissions and basic user information to make our appās experience a bit more personal. Itāll also fix one of the biggest limitations of our app ā being a timezone independent.
WaterLog app gives user possibility to sum up amount of water which was drunk during the day. But until now āthe dayā was calculated according to Firebase Cloud Function local time. Every time when we load data from database, day starts fromĀ 0:00 UTC. It means that for users in New York day starts at 20:00, London: 0:00, Warsaw 1:00 and so on.
Google Assistant API doesnāt give information about user local time so we need to calculate it on our own. Fortunately we can ask assistant for userās device location and use it to find local timezone.
āUpdate settingsā scenario
Letās start with adding new scenario to our app. Here are example conversations:
Happy path
WaterLog:Ā ā¦
User:Ā Update settings
WaterLog:Ā Sure, to customise settings, Iāll just need to get your name from Google. Is that ok?
User:Ā Yes
WaterLog:Ā Ok MirosÅaw. My clock shows me 10:24 am. If thatās not your time, to adjust my settings to your timezone, Iāll just need to get your street address from Google. Is that ok?
User:Ā Sure
WaterLog:Ā Ok, now it’s all set!
//End of conversation
User denies permission
WaterLog:Ā ā¦
User:Ā Change settings
WaterLog:Ā Sure, to customise settings, Iāll just need to get your name from Google. Is that ok?
User:Ā No
WaterLog:Ā Sure thing! You can ask me to personalise your experience whenever you want.
//End of conversation
As you can see, our app now has ability to ask for user name (will be used to make our conversations a bit more personal) and userās device location (so weāll be able to find local timezone).
Here you can official documentation aboutĀ user information.
Dialogflow Agent
Letās extend our Dialogflow agent by 2 new intents:
update_settings
This intent is fired when user is asking for updating app settings.
ā Config ā
Action name:Ā update_settings
User says:
Fulfillment:Ā ā
Use webhook
user_data
This intent is fired under the hood by Google Assistant every time when user interacts with permissions (accepts or denies). Based on our scenarios, here are the places in our app which will perform it:
WaterLog:Ā Sure, to customise settings, Iāll just need to get your name from Google. Is that ok?
User:Ā YesĀ āĀ here
WaterLog:Ā Ok MirosÅaw. My clock shows me 10:24 am. If thatās not your time, to adjust my settings to your timezone, Iāll just need to get your street address from Google. Is that ok?
User:Ā SureĀ āĀ here
To handle it properly, we need to configure this intent a bit different:
ā Config ā
Action name:Ā user_data
User says:Ā leave it empty
Events:Ā actions_intent_PERMISSION
ā there are special built-in intents which can be fired by Google Actions platform. Full list can be found in theĀ documentation.
Fulfillment:Ā ā
Use webhook
Thatās it. Our Dialogflow Agent is able to handle our new scenarios to customise app settings. If you would like to see full config, you can download it and import into you agent from theĀ repositoryĀ (WaterLog.zipĀ file, tag: v0.1.1).
The code
Now letās build backend implementation. Weāll add new, recently defined intents inĀ assistant-actions.jsĀ andĀ index.js:
Logic forĀ conversation.actionUpdateSettings()
Ā is pretty straightforward:
We use DialogflowĀ helpersĀ to ask user for permissions.askForPermission(context, permission)
Ā method will generate permission request which looks like this: ā{your context}, Iāll just need to get your name from Google.Ā Is that ok?āĀ .
When user replies to this question (āyesā, ānoā, āsureā, āof courseā, āno wayā etc.), our Firebase Cloud Function will callĀ conversation.actionUserData()
:
Logic should be self-explaining. What is important here, we should remember that this method is called when:
- User denies permission request (
dialogflowApp.isPermissionGranted()
returnsĀfalse
Ā ) - User allows permission ā when we ask for name and device location in the same conversation session, the second request can contain data from the first one (so: user name + device location).
Side note about permissions
If you checkedĀ SupportedPermissions documentation, you have probably noticedĀ DEVICE_COARSE_LOCATION
Ā permission. So why do we need the exact device location when we want to find a timezone? Two reasons:
- There are libraries which help to translate exact coordinates into timezone (moment-timezone). Coarse location returns us just a city and zip code (which with some additional work should give us similar results when using any geocoding API).
- Unfortunately during my development time there were no way to get any information from Google Assistant API forĀ
DEVICE_COARSE_LOCATION
. Probably bug, probably my oversight.
More appĀ logic
While that is pretty much it when it comes to Google Actions SDK, there were also some other things to code:
TimeManagerĀ class helps with time management operations. Here our app converts coordinates into timezone and saves it to Firebase Realtime Database. It also returns platform and user-local time and calculates local time for start of the day.
Now when we have userās given name we can also use it to personalize our experience a bit:
Unit tests
Did I mention that you should always write unit tests which makes your work million times faster, esp. in long term coding?
$ npm test
:
Source code
Full source code of WaterLog app with:
- Firebase Cloud Functions
- Dialogflow agent configuration
- Assets required for app distribution
can be found on Github:
https://github.com/frogermcs/WaterLog-assistant-app/
Code described in this post can be found underĀ release/tag v0.1.1.
Thanks for reading! š
1 reply on “<span class="entry-title-primary">Personalize Google Assistant skill with user data</span> <span class="entry-subtitle">Actions on Googleāāāpermissions handling</span>”
Great app and thanks for the writeup! Is there any way to view historical data as a user? I’d love to use this app to feed into my other metrics.