Adding Voice assistant to an e-commerce app built using Kotlin
In this article, I will show you how you can integrate Alan’s voice assistant into your existing e-commerce app. You can also use this technique to integrate Alan’s voice assistant for any other use case in your Android app. Before deep-diving into the coding part first, let’s understand what is Alan and how it works.
You all are familiar with voice assistants like Siri, Google Assistant, Alexa, and many more. These voice assistants help you to do your daily tasks and answer your questions. Have you ever wondered if you can integrate such type of voice assistant inside your application where it will help users to use your application just with the voice commands? To do so you can integrate Alan’s voice assistant inside your application. Alan is a developer-friendly voice AI platform using which you can add a conversational voice interface to your existing application.
Before going further first let’s see what we are going to build in the below video.
How does Alan Work?
Now, let’s understand practically how you can integrate Alan into your existing Android app.
Creating a project in Alan Studio
First, you need to create an account on Alan where you can easily signup using Google or Github account.
After creating an account you will redirect to Alan Studio’s dashboard where you see your projects but as don’t have any projects for now first you need to create a new project by clicking create Voice Assistant. Then select the empty project with the appropriate name as shown below.
Now, after creating the project you can start writing the voice script. You can see that Alan provides us a sample voice command i.e. hello world. You can test this in Alan Studio, so to test your voice commands you can click on the voice icon on the bottom right of your screen and make sure that you have enabled permission for the microphone. If you don’t want to speak you can also type.
Now, before writing voice scripts first, we see how we can integrate Alan Voice assistant inside our Android app. Alan provides its Alan Android SDK using which we can easily integrate Alan’s voice assistant.
Steps to Integrate Alan with Android
- Open an existing Android project or create a new project from Android studio. To use Alan SDK we need to add its dependency in
build.gradle (module-level)file as shown below and sync our project.
2. After adding a dependency, we need to add the Alan button to the app’s layout file.
AlanButton class provides a view that consists of a voice button and instance methods that help us in interactions.
3. Now, we have to create the Alan Config object which we need to pass with
AlanButton instance. We also required our key from Alan Studio. To get the Alan SDK key click on Integrations inside Alan Studio and copy the SDK key.
With all these steps you can integrate Alan into your Android app. Now, we see how we can send commands from Alan to our Android app using which we can perform operations.
Sending commands from Alan to Android
Before learning about commands let’s first learn about Intent. The intent is a function that takes a user’s question and responds according to it.
As we saw in the above code snippet first we have passed a question i.e
What is this app which implies that if the user asks this question to Alan it will respond with the response that we have written. So, here we have a written description of our app inside
p.play() which speaks the text passed inside it.
Now, as we learned about Intent let’s deep dive into commands.
We can send commands from our Alan script to our Android app using
onCommand method. Suppose, we have to open a particular tab in our app using Alan and for that, we need to send a command to our Android app. Inside
p.play() we can also send JSON object which we can access in our Android app. Here, we are sending commands with tab names like to open the favorite tab we are sending
Now, after sending commands from the script we can access it inside our Kotlin code by overriding
onCommand method. Here, we are using the when statement which is like a switch case for
commandName such that for every command it will open the appropriate tab as shown below.
We have successfully integrated commands in our app so now we can navigate to different tabs using Alan's voice assistant. Now, let’s add some more advanced features which Alan can do like adding items to their favorite list.
Using slots to store item names from user’s utterances
We already know how to use commands from Alan in our Android app. Now, let’s see how we can use slots with it. We can use slots to capture important information from user utterances like item names that we want to add to our favorites list. Here, we use user-defined slots which is an expected list of words that the user will speak. First, we create a list of items that the user can speak inside
onCreateUser which will call when Alan's instance is loaded. We will use that list while capturing utterances from users and send it inside a JSON object at the item as shown below.
Inside our Android app, we will add another command
addFavorite and write a code to add items to the favorites list as shown below.
Adding items to the cart using Alan
Now, let’s create the functionality to add items to a cart using Alan. Here, we need to capture items as well as quantity from the user’s utterance. So, for that, we will store the quantity inside
NUMBER and the item name inside
ITEM from the user’s utterance. If we observe items in our app, we have a few items like ginger and rice where we define their quantity by grams, and for items like red apple and fresh bananas, we use kg as a unit of measure. So, we need to handle this condition carefully inside our Alan script and for that, we can use the if-else condition where we compare the item name and assign the appropriate quantity.
For example, if the user speaks
Add 500 gram rice to the cart we need to add only 2 quantity of rice which are 250 grams each. If the user speaks
Add 2 Kg rice to the cart we need to add 8 quantities (2000 gram/250 = 8) of rice.
Now, we need to get
count from Alan script to our Android app from a JSON object and add that item to the cart with the given count on
Finally, we have added all required features in our online grocery store where we can do the following things using Alan.
- Navigate to different tabs
- Add items to favorites
- Add items to cart with the quantity
You can add many more advanced features using Alan like you can send data from your app to Alan using Project API. I hope you have a great time reading this article.
Github Link: https://github.com/vrajdesai78/Grocery-Store-Alan