Using Cloud Run as a webhook for Actions on Google
Cloud Run 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 Cloud Functions, pricing is “pay-per-use”. As part of the Actions on Google community program, 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 Cloud Functions to create stateless webhooks using the Node.js client library. For developers who have been using the Java/Kotlin client library, 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.
You can start with one of the samples in the Actions on Google GitHub organization. In this post, the sample being used is
dialogflow-silly-name-maker-webhook-java, 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 Actions console, 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 SpringBoot framework, follow the steps in the Cloud Run quickstart 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:
Make sure that you’ve downloaded the Google Cloud SDK. 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 https://random-url.a.run.app
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 Stackdriver Logging to view logs of your Action as it runs.
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 documentation 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.