3…2…1…Deploy!

Deploying Google Actions on the Cheap!

Create your own voice command for Google Assistant with Python, Dialogflow and Google Cloud Functions (serverless!) [Part 3]

In the previous sections, we learned how to build the Apprentice webhook for our Dad Joke action. In addition, we walked through the configuration of Dialogflow intents.

Here we will learn how to deploy our Apprentice webhook on Google Cloud using their Cloud Functions.

It is unarguably the most important milestone in the development of software. When the rubber meets the road.


But first, WTF is a Google Cloud Function?? And importantly, why should we use it?

Well, at the risk of sound too obvious, a Cloud Function is a function.

Over the past couple of years cloud functions, or more widely known as serverless functions, have matured and come into vogue.

The reasons why are many but to briefly name a few, serverless functions are: highly scalable, simple, and cheap.

Since they are functions that are run when called, you are only charged the server time of when they are running. Meaning your function that is sitting there 24/7 and only being used when someone says “Tell me a dad joke”, is not racking up the usual bills a traditional server would.

They are also spun up and down very easily making it a breeze to accept hundreds or thousands of requests per minute.

Lastly, they are functions. If done right, it means functionality is kept quite specific. No need to go digging around thousands of lines of code to find a bug since all your logic is encapsulated by one function.

In our example, the logic is only responsible for getting a dad joke. Nothing less, nothing more.

I won’t go into more detail about serverless function here, but please dive into the subject, because they are quite powerful in production environments.


Step 1: Google Cloud Project

In the chance that you did not create a Google Project earlier, don’t worry. You can create a project under the action settings.

In the settings you should see a project id. Right next to the project id, should be a link to the Google Cloud console for that action.

Let’s go there.

Step 2: Cloud Function

Once in the Google Cloud dashboard, we will see to the left a bunch of tabs. One of which will be called Cloud Functions.

If you haven’t done it already, accept all the payment authorizations that you will be prompted for.

If you are worried about the pricing you are accepting, please read the pricing section.

TLDR; According to the Google Cloud Functions pricing page (link) at the time of this writing, you will receive 2 million + invocations to your endpoint before being charged. That’s a lot of jokes!

Create your first function.

Once created, you can see a couple of options.

Rename the function to “joke.”

The other thing we need to make sure of is the runtime. For this tutorial, we are using Python 3. Let’s make sure that Python 3.7 is selected.

You may notice a Beta tag on that run time selection. For our purposes in this tutorial, it shouldn’t be a big deal. However, you’ll have to weigh the risks vs. reward for production environments. (My two-cents = it appears pretty stable).

Step 3: Copy Pasta

For this tutorial, we will do a little copy-pasta. Yes, there is the option of using the CLI for updating our Cloud Function code which I may cover at a later time. But for now, we will be doing it the, shall I say, less error prone way.

Below all the settings for our dad-joke function, is a section with some text-boxes. One should be labelled main.py and another requirements.txt. Here is where our logic and requirements will go.

If all of our code is good to go from the local set up, we can copy and paste directly to the respective text boxes. All the Apprentice webhook code goes in the main.py and the project requirements go into requirements.

main.py
requirements.txt

Lastly, we can tell the platform which function to execute when our url is called. In our case, this will be called webhook.

This is the main entry point for the webhook

Once saved, the function should start to update. This could take a couple of moments.

You will know that it has been successful when next to the function name, a green check box appears.

Great success!

Step 4: The Trigger

To call our function via https, we need a trigger.

As a serverless function, our trigger will be a url found in the “Trigger” section of the console.

It will be a url of the area you deployed your function in followed by some other project information and finally your function name.

Look, the trigger url!

Copy this because we are going to update our action with this url.

Navigate back to our Dialogflow console. Update the Fullfillment endpoint with this new trigger and don’t forget to Save.

We can then open up the Google Assistant console and invoke our action.

“Tell me a dad joke” intent should now be calling the cloud function!

We can prove it by shutting down all the local servers and ngrok processes. As well as looking at the function console which should show a graph of the number of invocations per minute.

You will see a spike in the graph.

Step 5: Deployment to the Real World

Now that we have everything running independently of our local environment, we can deploy it to the world! Or at least Alpha testers that way we can show our friends what we have built.

On the left-hand side of the Google Assistant console, we should see a Deploy section with a tab called Release.

Like all good projects, we are going to release our action in Alpha for testing and bug hunting. In Google action speak, this means that we can give access of functionality for up to 20 Google users of our choosing.

But before we create a release for our Action, we must prepare some information first.

On the top of the deployments page, you’ll see a blue header telling you that you must finish configuring your action. Click Overview.

Managing our release

Follow all the instructions on the page, ex. naming, invocation, etc. These will be unique to your action, so define them as you wish.

All ready to go!

After this is done, go back to the releases and submit an Alpha release.

You will need to agree to the terms and conditions.

We will need to wait a few moments for the release to finish.

Once released successfully, we should see a list of one release at the bottom of the releases page.

One whole release!

Step 6: Test it on a Real Device

By default, the email account that you use for the Google Cloud is added to the whitelist of Alpha Testers. If you would like to add more to the list, you may add them in the manage alpha testers section in releases. Note, whitelisted accounts must be Google accounts.

Once satisfied with the release, we can invoke our action on the device. This can be either a Google Home Pod, Google Assistant App, etc.

This is the invocation defined when updating the information from the step before.

From the Google Assistant overview section
Hey Google! Talk to Andrews Dad Joke
Alright. Getting the test version of Andrew Dad Joke.
Tell me a joke.
What did the pirate day on his 80th birthday? Aye matey!

Woohoo!! 🚀 🎉 🎉

Congratulations!

We have successfully built, expanded and deployed a Google Action webhook!

It can be activated by voice or text on ANY Google Voice supported device.

In addition, the backend infrastructure is highly scalable, simple, and inexpensive.

I hope you enjoyed this process. The library that we are using is called Apprentice to make iteration as easily as possible. As we move forward I will continue to expand Apprentice to allow for more complex responses and support for the full Dialogflow 2.0 API.

If you feel like contributing to a project, the easiest way is to let me know how development went for you and what features you would most like from Apprentice. Any feedback, positive or negative, will be greatly appreciated.

Thanks for following along!