Actions for Google Assistant, the Android Engineer’s Way!

Kotlin Talk: Building Actions for the Google Assistant with the Kotlin client Library

Joel Anderson
Plated Engineering & Data Science
3 min readFeb 19, 2019

--

Voice Assistants: they’re one of the fastest growing technologies in the digital space. It’s predicted that, by the end of 2019, over half of all US households will bring a smart speaker into their homes to answer questions, tell jokes, and enable novel human-machine interactions, all whilst silently collecting valuable data on these experiences.

As a result, it’s no wonder that companies are eager to create and offer applications encompassing these devices, a task that often seems to fall to mobile application developers due to the intersections of these platforms. In many ways, voice assistant applications are extensions of mobile applications, allowing companies to augment their existing technologies rather than replace them altogether.

In Plated’s case, for instance, managing a user’s subscription via the Google Assistant can be clumsy and more cumbersome than within our mobile application. Following cooking instructions, however, is much simpler for a user with the aid of a voice assistant than it is attempting to interface with their phone while also chopping an eggplant or sautéing an onion.

Despite the similarities between the two, making the leap from developing applications for traditional mobile devices to voice assistants can be somewhat daunting. In this series, we’ll explore that transition from the perspective of an Android engineer who wants to expand their existing digital experiences to this new platform. You’ll learn how to leverage the Kotlin client library to create a fully functional, interactive Google Assistant application from the lens of an engineer who is experienced with the Kotlin language, Gradle, and Java libraries commonly found in Android codebases.

Since Google announced official Android support for Kotlin in 2017, its prevalence as a language has grown astronomically. Its plethora of advantages over Java, such as null safety, extension functions, and native support for coroutines, plus the ability to interoperate seamlessly with Java code, has propelled Kotlin to the forefront of the Android development sphere.

Thankfully, Google has extended support for JetBrains’ language to the Google Assistant. With the Kotlin client library for actions on Google, not only can Android engineers accustomed to the language quickly jump in to development for the Google Assistant, but they can also share code between platforms, saving valuable time for improving the conversational user experience.

Part 1 — Hello World

To start, let’s explore the most basic of all applications, Hello World, in glorious, null-safe Kotlin. Before jumping into this code, make sure to clone Google’s Boilerplate project to use as a foundation, as well as to follow the rest of the setup instructions therein.

Here we can see the basic format for MyAssistantApp, a class which will handle all of our DialogFlow intents. Each intent should have a corresponding function in this file, each of which should be annotated with @ForIntent(intentName: String), take in an ActionRequest object as a parameter, and return an ActionResponse object.

Extending this class from DialogFlowApp allows us to create a ResponseBuilder object using the function getResponseBuilder(). We can add a simple response to this builder in the form of a string, and then build and return our ActionResponse object. If we deploy and test our application right now, the Assistant will generate this simple response when our action is invoked. Easy enough!

If we want an added level of personalization, we can modify this function slightly to respond with the user’s name as well.

And that’s it! We now have a simple action that can be deployed and run on any Assistant-enabled device. In part 2, we’ll explore fetching and returning data from the web using common Java/Kotlin libraries, like Retrofit, Dagger, and OkHTTP. Finally, in part 3, you’ll learn how to parse request parameters and use them to return relevant information.

--

--