Dialogflow from Google (earlier known as API.ai) is a great platform for developing chatbots for multiple platforms. There are sufficient tutorials and documents available for someone new to start learning and use the platform to create chatbots. However, support for Android and iOS are not that well documented.
Earlier when Dialogflow was known as API.ai, they had an Android and iOS client library to integrate a chatbot into mobile apps. But after merging into Google, the existing library is not getting updated rather we are seeing Dialogflow added to Google’s Cloud platform and a new Java-based client API is being developed.
In this article, I will show you how you can integrate a chatbot developed in Dialogflow using both old API.ai based library and new Java client API. So, to show this, I will create a simple chatbot that will provide me the information about the different tracks at the DevFest18 organized by GDG New Delhi.
So first I will have a script of the user conversation with the chatbot.
User: I want the list of sessions at Dev Fest in New Delhi.Bot: Sure! There will be 4 tracks of sessions. For which track do you want the information?User: third track.Bot: 9:00–9:30 Registrations… Anything else?User: No.Bot: Thank you! Have a great day :)
Now, we will create a Dialogflow Agent which we will name as Test_agent.
service account is used for creating credentials for Java client API
client access token is used for integrating Android client library
2. For Java client API, we need to click on the service account and get our JSON key generated from the IAM console.
3. Now we will create the Intents and Entities to train the agent.
get_agenda will handle the user’s request and fetch the track for which details need to be provided
adding synonyms to capture track values
created a parameter to fetch track and enabled webhook integration
4. Create a cloud function fulfillment to handle the webhook request. Based on the detected track number, an appropriate response will be sent to the user.
5. Now our Agent is ready and we will now integrate it to the Android app. In the build.gradle(app), we will add the dependencies for both Android client library and Java client API as we will be developing for both.
6. The chatbot will be initiated in the onCreate method of the Android activity. We will first configure the Dialogflow agent using either the Client Access Token or using the JSON key. Then a new session is created using the unique ID and then the bot is made ready to communicate with the user.
Android client library
Java client API
7. We will handle the communication with the chatbot in an Asynchronous way. This we will do in AsyncTask where the request query from the user is sent as a request to the chatbot and the response is captured.
AsyncTask handling Android client library calls
AsyncTask handling Java client API calls
8. The user can send a query from the Android app using EditText and the response will be shown in a TextView.
request and response handled in Android client library
request and response handled in Java client API
9. Our chatbot integration is complete. After adding the necessary UI changes to capture user query and bot’s response, here is the final chatbot app sample
Android Chatbot with Dialogflow
Source: Medium - Abhinav Tyagi
The Tech Platform
Comments