Actions on Google Using Cloud Functions

In recent articles, we have seen how we can create an Action using Dialogflow and how to deploy it over the store. Today we will learn how to generate a dynamic responses in our actions on google application using cloud functions.

Today we will cover how to make cloud functions for Actions on Google which can respond dynamically or by performing some calculation.

What is Cloud Functions?

Cloud Functions is a serverless execution environment for building and connecting cloud services.

Basically we are writing some functions which are made to call some events. Whenever a particular or pre-defined event occurs, cloud functions will be called to perform some operations.

Why do we need it in Actions on Google?

We do have a lot many cases where we need dynamic output for the user’s input. It can be anything like reading data from firestore, calling 3rd party API, calculations based on user input etc. In those cases, we can use cloud functions to write backend logic and generate output for the user.

For example, our action provides user astrology, we need to take today’s date and based on that we will call 3rd party API and generate an appropriate response for the user to check in our action.

All these tasks we can perform in our cloud functions. We can host it over google or any other platform which provides functionality to host our functions.

Let’s learn in deep by developing an example app. We will create a function that will generate a random number and respond it back to our assistant application. We will use cloud functions for firebase in the rest of the article.

Prerequisites:

Once you have everything ready, move to actions on google console in your browser and sign-in with your google account. After signing in, you will be able to see the following screen in your browser.

Click on Add/Import project and agree to the terms and conditions of Actions on Google.

It will ask for the project name, enter your project name and click on Create Project.

Once you are done with the above steps, select Conversational from the options given.

Now you will be redirected to your project’s console which will look similar to the below image.

Select Build your Action and add action(s) to it. From here we are going to create our Dialogflow agent. Just select Custom Intent from the options given after clicking Add your first action. Press Build and login with your google credential into Dialogflow.

You might have observed that the agent name is prefilled which is the project name that we have entered, click on Create.

As you can see we have some pre-defined intent available with our Dialogflow agent.

Default Fallback Intent Default Welcome Intent

Click on Default Welcome Intent. As you can see there will be some training phrases and response text also. This intent will be called when a google assistant launches our application. We can consider it as a starting point of our action.

As we just want to create a random number, we do not need to create another intent that takes user input. We will serve a random number when a user enters in our action.

In order to enable webhooks, we need to enable it from the fulfillment section, just enable an option called Enable webhook call for this intent. It will call a webhook whenever this intent will be called.

Click on Save to make sure all the settings we have changed will be saved.

It will enable webhook calls for our assistant which means whenever this intent will be called, it will redirect request to our cloud function.

Create Cloud Function

The first step is to make sure you have node installed in your system.

Now we are going to install firebase-tools which will allow us to create and deploy function on firebase. Open terminal/command prompt and enter below command.

npm install -g firebase-tools 1 npm install - g firebase - tools

After getting it installed, we need to login in firebase through terminal/command prompt. Enter below command which will log in through CLI to our firebase account.

firebase login 1 firebase login

Now we will initialize firebase function in our project. Write below command and follow steps

firebase init 1 firebase init

Enter Y or Yes and press enter. Select Functions fro available list by using press and press enter.

Select a project from given list of projects and language as Javascript. At the end, it will ask you to install dependencies. Enter Y and all the node modules will be installed.

Now if you will move to the directory it should have the following structure.

In order to deal with actions on google request and response we will install actions-on-google node library. Move to functions folder and enter below command in terminal.

npm install actions-on-google --save 1 npm install actions - on - google -- save

Now open index.js and write below code.

const { dialogflow } = require('actions-on-google'); // Import the firebase-functions package for deployment. const functions = require('firebase-functions'); // Instantiate the Dialogflow client. const app = dialogflow(); // Handle welcome intent request and generate output. app.intent('Default Welcome Intent', (conv) => { var num = Math.floor(Math.random() * 1000); conv.ask('Your random number is ' + num); }) // Set the DialogflowApp object to handle the HTTPS POST request. exports.randomNumber = functions.https.onRequest(app); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const { dialogflow } = require ( 'actions-on-google' ) ; // Import the firebase-functions package for deployment. const functions = require ( 'firebase-functions' ) ; // Instantiate the Dialogflow client. const app = dialogflow ( ) ; // Handle welcome intent request and generate output. app . intent ( 'Default Welcome Intent' , ( conv ) = > { var num = Math . floor ( Math . random ( ) * 1000 ) ; conv . ask ( 'Your random number is ' + num ) ; } ) // Set the DialogflowApp object to handle the HTTPS POST request. exports . randomNumber = functions . https . onRequest ( app ) ;

const { dialogflow } = require(‘actions-on-google’) : It will import dialogflow class from actions-on-google node module that we have installed in above step.

: It will import dialogflow class from actions-on-google node module that we have installed in above step. Default Welcome Intent : It is intent name that we have defined in Dialogflow.

: It is intent name that we have defined in Dialogflow. app : It is an object of Dialogflow which will catch and respond to request received from google assistant.

: It is an object of Dialogflow which will catch and respond to request received from google assistant. exports.randomNumber : It will export the current function with name randomNumber

: It will export the current function with name conv.ask: It will send response back to assistant. It can be called with any of the Response types.

The above code will generate a random number when user enters to google assistant application.

Now we need to deploy this function to our firebase. In order to deploy we need to fire below command.

firebase deploy 1 firebase deploy

Open Firebase console and select Functions from left selection menu. It will show you function that we have deployed.

You will find url of function there, copy it, we need to add this function reference to Dialogflow console.

Open Dialogflow console and select Fulfillment from left side menu. Enable webhook and enter url that you have copied from firebase.

That’s it, we are done with our first google assistant application which will generate random number using cloud function.

Move to actions on google console and test it with simulator. It should give you following output.

Isn’t it easy to create google assistant applications which can generate a dynamic response for the user? It is just the matter of having a basic knowledge of javascript/typescript/python and you have everything in your hand to play around google assistant applications.

Comment down applications you have created for google assistant. Let’s share and contribute 🙂