Header Ads

How to Create Chatbot in Android App

Okay, now let's code!
For the backend of this chatbot, I am using Firebase real time database and I used dialog flow for chatbot engine.


How to Create Chatbot in Android App


Note: In this tutorial, I am not going to go into depth about chatbot training, I am just showing a general way to add chatbot in your Android app. Setting up dialog flow. 
  
For the purpose of this tutorial, we need to enable the small talk of the chat boot agent in the dialog flow console. Just go to your dialog flow console and create an agent. Fill out the required form, including time and default language. Now you have an agent but he doesn't know anything. We need it to train to make it work more efficiently. As I mentioned earlier we will not focus on this so we can use a per-prepared agent instead. A pre-built agent is basically a pre-built agent who has been trained to understand a specific context. Go to the sidebar. Then import the Small Talk Agent. Now we have an agent who can get involved in the little things. Yeayy!

Setting dependencies in android project
 
Here are some things we need to do to make the app:
  • Dialog flow
  • Firebase real time database 
  • Firebase UI 

As I said earlier, we are going to use Firebase Real Time Database as a backend ie all user and chatbot conversations will be stored in this database. To make our UI creation easier, we used the Firebase UI where we can easily integrate the data from the database and populate it in the UI and we can also observe the data and if Updates can be made automatically if there are any new changes. You can also use your Firebase in Android Studio by adding Firebase assistant in android studio, go to tools -> firebase -> add firebase

Realtime Database 

And at the end, here is the build.gradle file : 

Create the ui

We are going to create a simple UI where this message displays. We are going to get it using recyclerview. So first we need to create an item_view. Such :
Next, we're going to create a layout that has a recycler view to show the chat and a button with an edit text at the bottom of the screen to type and send a message. Code like this:

Work with the business logic

In this tutorial, we are going to use the MVP approach to loosen up the architecture (I suggest you learn about this or another approach if you are focusing on Android development). First we need to define an agreement to make the point of view and the role of the presenter clear. Basically the presenter receives the input message from the user and sends it to the database and retrieves the reply message from the chatbot. Wave, on the other hand, is responsible for retrieving the user's typed message and populating the recycled view with both the user's message and the boot message. The agreement looks like this:
As you can see above, there is no function in WAV, the reason is that Firebase Adapter can observe data changes from database so we do not need to handle it. 

Now that we have a clear role, the second step is to enforce the agreement for each role. Before we go to View and Presenter, we need to first write the adapter for Recycle View. Here we are using firebaseadapter and viewholder. The code looks like this:

The Presenter

In the presenter we handle the message entered by the user to send to the firebase database and to activate the chat boot in the dialog flow, here I used Kotlin Cortens to perform the request in the background.
Now all we need to do is set up the activity, to get the user's input, populate the recycler view and observe the data changes in the Firebase real time database.

 


 

 

No comments:

Powered by Blogger.