How to Create a Chatbot in Android with BrainShop API?
We have seen many apps and websites in which we will get to see a chatbot where we can chat along with the chatbot and can easily get solutions for our questions answered from the chatbot. In this article, we will take a look at building a chatbot in Android.
What we are going to build in this article?
We will be building a simple application in which we will be building a simple chatbot where we can ask a question to our bot and the bot will answer our questions. Below is the video in which we will get to see what we are going to build in this article. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add the below dependency in your build.gradle file
Navigate to the app > Gradle Scripts > build.gradle file and add the below dependency to it in the dependencies section.
After adding this dependency sync your project and now move towards the AndroidManifest.xml part.
Step 3: Adding permissions to the internet in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to it.
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 5: Creating a Modal class for storing our messages
Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as MessageModal and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 6: Creating a layout file for user messages
Icons used in this file are present in the drawable folder. Navigate to the app > res > layout > Right-click on it > New > layout resource file and name the file as user_msg and add the below code to it.
Step 7: Create a layout file for bot messages
Icons used in this file are present in the drawable folder. Navigate to the app > res > layout > Right-click on it > New > layout resource file and name the file as bot_msg and add the below code to it.
Step 8: Working with the Adapter class
For setting data to our items of Chat RecyclerView we have to create an Adapter class. Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name your class as MessageRVAdapter and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 9: Generating API key for using the chatbot service
Go to Brainshop.ai generate your simple account with your username and password. Simply create your account on this website. After creating a new account you will get to see the below screen. After creating your account you have to request a new password from the request password option and enter your email address. After adding your email address you have to add the password to your account. Now we are good to go to generate our API key.
Follow the above steps to Generate a new brain for your chatbot. After generating your bot now we will get the API URL for this brain. Navigate to the settings tab inside your created brain you will get to see your bot details as shown below.
Note: Now we will be using this API URL only inside the MainActivity.java file.
Step 10: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.
Check out the project on the below link: https://github.com/ChinmayMunje/GFG-Bot