Assistant & … Pizza 🍕 — Backend — Part 4

Build Actions on Google Assistant with Kotlin! — Your custom response, written in Kotlin

--

This article is part of a series “Build Actions on Google Assistant with Kotlin!”
Overview | Dialogflow | Intent | Backend | Tips & Tricks

If we want to help the customers of our imaginary “Incredible Amazing Pizza” restaurant, we need to respond to the question “What’s a Margherita like?”.
In the previous article, we create the intent Ask Ingredients with the parameter PizzaName.

Now we should create a service to handle this intent.

Our backend as Fulfillment

This request needs to be handled by a backend service, I choose Google App Engine because it’s a free service (with quotas) and does not require a credit card (it’s very useful for codelabs).

So, let’s start with App Engine! 🎉

Create an App Engine project

App Engine is a platform for developing and hosting web applications in Google-managed data centers and is free, up to a certain level of consumed resources.

To create an App Engine project follow those steps:

  • Choose our project in: Select a project
  • Press on “Create Application” and choose a region
  • After that, you can “Enable billing” or just skip it for now, and press “Cancel”
  • You should read: “Your App Engine application has been created”

Create a Kotlin project

I’m an Android developer and I use Kotlin daily for app development. I tried Kotlin also on the back-end side and it’s a very versatile and powerful language.

The initial setup requires a few steps:

  • Choose a name for your new repository, for example, dialogflow-pizza
  • After creating the repo, choose “Clone or download” and copy the web URL
  • Open IntelliJ (or Android Studio), choose “Check out from Version Control” and paste our URL
  • Open src/main/kotlin/***/MyActionsApp

Add Intent: Ask Ingredients

Create a new method askIngredients() for handle our intent

Deploy the project

To deploy the current version, go to the terminal:

  • Run: gcloud init
  • Run: gradle appengineDeploy or from within IntelliJ, open the Gradle tray and run the appengineDeploy task.
Before run appengineDeploy
After run appengineDeploy, we need to copy the target url

Dialogflow Fulfillment

Configure Fulfillment in Dialogflow

  • Go to Dialogflow console, from the left navigation menu under FulfillmentEnable Webhook
  • Set the value of URL to https://<YOUR_PROJECT_ID>.appspot.com (you could use the target url)
  • Press Save

Enable Fulfillment in Intent

  • Open the intent “Ask Ingredients”
  • At the bottom of the screen, we have a section called “Fulfillment”
  • Open it and flag “Enable webhook call for this intent”, in this way Dialogflow tries to contact our backend before sending the response.

Try it now

Now we can try if everything works, use the “Try it now” in Dialogflow and insert our request, for example: “What’s inside the Margherita?”

Now that everything works, take a look for the current workflow

Handle the parameter “PizzaName”

  • Update askIngredients() function and use request.getParameter() to obtain the pizza in the current request.
Tip: You can also clone/download this repo https://github.com/IncredibleAmazingPizza/dialogflow-pizza
  • Deploy again

Try it again!

  • Use “Try it now” in Dialogflow, and click on “See how it works in Google Assistant”
  • Accept “Terms & conditions”
  • In the “Develop” tab we can choose a name for our Action and press Save

And that’s our final result! 🎉

Next step — Final tips! 😁

We have a working Action backed by our backend. Now we could add some more new intent to complete our project or follow our final tips.

Useful links

Repository template

Repository final project

Other articles on Medium

Official links

--

--