Make Google assistant to assist your users
How to setup Google assistant's Actions, Serverless, to assist your app users for free.
Modern platforms have more than one entry point to provide data to a user. They provide consistency and performance amongst other features. Happy times. Assuming there is a nice architecture and some best practices in place. Google assistant has great improvements over the past few months, in terms of documentation and the API usage, in various aspects.
I am going to describe an easy way to help you leverage the basics of Google assistant for your platform. Just in a few steps, consuming the free versions of some Google APIs. Presumably, you can replace any part of these steps to integrate with your own application.
Overview
The Google assistant will be based on Intents defined in "Actions console". Directly from there, we will be deploying "Cloud Functions" and "Intents" for our Assistant to ask for user input params, and report back the calculated results of our fetched data. Similar logic with the Web Hooks we saw in Miss Duck project, but in this case the Web Hooks will be automatically hooked to our Cloud Functions.
With a very little effort we will create actions for an assistant to communicate simple information to our users.
For example, our flow will look like this:
- "Hey google, ask miss Duck, how many apples do I have?".
- "What colour? Green or Red ones?"
- "Green"
- You have 3 more green apples.
Behind the scenes, we invoke an agent directly with our first question which is very convenient. Alternatively you can ask Google Assistant to "talk to miss Duck" first and then continue the conversation after the welcome message.
Google Assistant
Setup a new project in Google assistant and follow the basic on-screen information. Not much to do at the moment in this console, You can use it in the end to publish your Assistant. All you will need is the Privacy policy text, and a logo icon.
You'll be asked to fill in some more information about the type of the project you are creating.
Dialogflow
Let's jump to define our first Intent.
- Select a name for your intent which we will use in our webhook (cloud function) to be triggered. e.g. "get apples"
- Set Training phrases, to define how your users will trigger this action. For example "how many apples do I have?"
- An interesting field is the Actions and parameters.
Here you will define how the value will be passed in your script from the user. The name of the action can be set here, alongside the params you expect your user to give you.
If the param is missing from their request, you can define what the followup questions will be from the Agent. (last table column).
Required - self explanatory
Parameter name - will be parsed from the fulfilment scripts as agent.parameters.color
Entity - you can define here what type of data you're expecting, seems we have color as a System entity to pick.
Value - you can set the initial value as placeholder. More information here
IsList - self explanatory
Prompts - A great way to define how the Agent will ask for each value if it's not provided initially
more about the actions and parameters, here - Lastly you need to enable webhook in the fulfilment section, which will enable the use of the deployed Cloud Functions
Firestore
Enable your database and create your documents (Assuming you already have a platform running, you have data, and maybe even a different storage service.
For our example we are using Firestore, that looks like this
![]() |
Firestore Database setup |
Fulfilment
Now we are ready to bind the pieces together. All you need to do is to setup the Fulfilment. We enable the cloud functions which will automatically deploy and link the function, ready to consume.
To access the database form the function you'll need to load the firebase-admin as seen here.
![]() |
The Function editor within the Dialogflow Fulfilmen |
const admin = require('firebase-admin');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
The configuration setup is done for you, and the projects are linked altogether. All you need to do now is to grab your collection and start reading data.
const applesDoc = db.collection('apples')
Add the handler for your intent. Use the intent's name set earlier.
intentMap.set('get apples', getApples);
Define the handler:
function getApples(agent) {
return applesDoc
.where("color", "===", agent.parameters.color)
.get()
.then((snapshot) => {
let apples = [];
snapshot.forEach((childSnapshot) => {
const childData = childSnapshot.data();
console.log(childData);
//do more here, or better to use a counting query for the applesDoc
apples.push(childData);
});
agent.add(`found ${apples.length} ${agent.parameters.color} apples `);
})
.catch((err) => {
console.log(err);
agent.add("Error reading entry from the Firestore database.");
});
}
Deploy
All you need to do now is to deploy and test. You can define environments and try a few ways to test, and view logs. The information in the dashboard, is pretty clear to follow from this point on.
Takeaway
Wow your users, by providing them a way to interact with your platform via google assistant. Learn how to easily set up a new entry point for your platform's data, frictionless, free and fast.Use Cloud functions to access your database and link Intents for a Google Assistant Agent. In a few fast steps you can provide another accessible way for your users to get data from your platform. Increase users' engagement by providing more ways for them to access your data.
HAVE FUN
Comments
Post a Comment