Actions on Google Using Cloud Functions

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.

actions on google console

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

accept terms and conditions

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.

templates

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

console

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.

intent suggestions

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

Actions on google using cloud functions

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

  1. Default Fallback Intent
  2. 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.

dialogflow intent

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.

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.

Actions on google using cloud functions

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

Actions on google using cloud functions

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

Actions on google using cloud functions

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.

Actions on google using cloud functions

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.

Now open index.js and write below code.

  • const { dialogflow } = require(‘actions-on-google’): 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.
  • app: 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
  • 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.

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

Actions on google using cloud functions

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.

Actions on google using cloud functions

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.

Actions on google using cloud functions

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 🙂

Follow me

Ravi Rupareliya

He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.
Ravi Rupareliya
Follow me

Latest posts by Ravi Rupareliya (see all)

React Native – Introduction
Dialogflow Entities With Actions on Google