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:
- Open https://console.cloud.google.com/projectselector/appengine
- Accept “Terms & conditions”
- 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:
- Install: IntelliJ Community Edition (or Android Studio)
- Install: gcloud CLI for Windows/Linux/Mac from https://cloud.google.com/sdk/docs or alternative for Mac OS:
brew cask install google-cloud-sdk
(read more http://macappstore.org/google-cloud-sdk) - Open terminal and run:
gcloud components install app-engine-java
- Open https://github.com/omarmiatello/dialogflow-appengine-kotlin and choose “Use this template” to create a new project (you should be logged on GitHub)
- 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 theappengineDeploy
task.
Dialogflow Fulfillment
Configure Fulfillment in Dialogflow
- Go to Dialogflow console, from the left navigation menu under Fulfillment → Enable Webhook
- Set the value of URL to
https://<YOUR_PROJECT_ID>.appspot.com
(you could use thetarget 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 userequest.getParameter()
to obtain the pizza in the current request.
- 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.