Using Cloud Run as a webhook for Actions on Google

Nick Felker
May 21 · 4 min read

is a new product for Google Cloud Platform that makes it easier to run your application in the cloud as a container without having to worry about the infrastructure. Cloud Run is automatically able to scale to meet the demands of your users. Similar to , is “pay-per-use”. As part of the , you are eligible to earn $200 in Google Cloud credit each month after you publish your first Action.

Actions on Google developers have been using to create stateless webhooks using the . For developers who have been using the , the release of Cloud Run offers a new way to develop your Actions using a serverless backend on Google Cloud. This post explains how to take one of the current samples and publish it using Cloud Run.

Project setup

You can start with one of the samples in the Actions on Google GitHub organization. In this post, the sample being used is , although this will work for any of the related samples.

After cloning or downloading the source code, you can follow the README to create a new project in the , upload the agent.zip to Dialogflow, and open the project in an IDE or text editor.

The project uses HTTP servlets to handle requests and responses. To configure your project to use the framework, follow the steps in the Cloud Run for the Java programming language. Replace the ActionsServlet class with a new class called ActionsApplication. This class is shown in the code snippet below:

The work of handling the requests and generating responses is done by the SillyNameMakerApp class. The ActionsApplication class just serves to bootstrap the Action requests and send responses.

Make sure that the pom.xml file is set up correctly to import the SpringBoot and Actions on Google dependencies, as shown below:

To package your Springboot application, you can use the open source container tool . Make sure you update the URL of the with your project id.

Deployment

Open your project in the . Make sure to before proceeding.

Make sure that you’ve downloaded the . You will need to install the beta add-on to the command-line interface if you have not done so before:

$ gcloud components install beta

$ gcloud components update

Once that has been installed, you are ready to deploy your application and connect it to Cloud Run and Dialogflow. To do so, follow these steps:

1. Build your container using Jib, which as configured in the pom.xml will push it to Google Cloud Container Registry:

$ gcloud auth configure-docker

$ mvn jib:build

2. Deploy the container image to Cloud Run:

$ gcloud beta run deploy --image gcr.io/[PROJECT-ID]/helloworld --allow-unauthenticated --set-env-vars=”JAVA_TOOL_OPTIONS=-XX:MaxRAM=256m”

After running this command, the response will include a URL.

Service [service-name] revision [service-name-00001] has been deployed and is serving traffic at

3. Copy this URL and go back to the Dialogflow console. On the Fulfillment page, put this URL in the Webhook URL field.

Now, when you use test project with the Actions on Google simulator, your webhook will be called with the responses coming from your container. You can use to view logs of your Action as it runs.

Conclusion

If you are interested in building an Action with the Java Actions on Google client library, you should consider Cloud Run for your webhook. It is designed to give you an easy setup while providing you with the flexibility of the programming language and framework that you want. Visit the on Cloud Run to learn more about how to make it work for your project.

Want more? Head over to the Actions on Google community to discuss Actions with other developers. Join the Actions on Google developer community program and you could earn a $200 monthly Google Cloud credit and an Assistant t-shirt when you publish your first app.

Google Developers

Engineering and technology articles for developers, written and curated by Googlers. The views expressed are those of the authors and don't necessarily reflect those of Google.

Nick Felker

Written by

Social Media Expert -- Rowan University 2017 -- IoT & Assistant @ Google

Google Developers

Engineering and technology articles for developers, written and curated by Googlers. The views expressed are those of the authors and don't necessarily reflect those of Google.