Build Chat bots with Dialog flow — Step By Step Guidelines

Build Chat bots with Dialog flow — Step By Step Guidelines

Dialog Flow from Google (earlier known as API.ai) is a great platform for developing chat bots for multiple platforms. Earlier when Dialog Flow was known as API.ai, they had an Android and iOS client library to integrate chat bot into mobile apps....

Dialog Flow from Google (earlier known as API.ai) is a great platform for developing chat bots for multiple platforms. Earlier when Dialog Flow was known as API.ai, they had an Android and iOS client library to integrate chat bot into mobile apps. But after merging into Google, the existing library is not getting updated rather we are seeing Dialog flow 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 chat bot developed in Dialog Flow using new Java client API. This is image title

  1. Now, we will create a Dialog Flow Agent which we will name as FirstActionApp. Please click on the link below: https://dialogflow.cloud.google.com/#/editAgent/f8227687-9a5c-4eb6-a9a5-b2078728da92/ This is image title

· Note: Dialog Flow V1 API shutdown on march 31st, 2020.

· So we are Creating dialog flow app with V2 API Version. Service Account in this image is used for creating credentials for Java client API.

This is image title

· For creating chatbot with V1 API we use client access token which is used for integrating android client Library. · For creating chatbot with V2 API we have to go with a service account. · Select your App. This is image title

· Create new key with Dialog Flow Integrations. · Download the created json File. 2. Now we will create the Intents and Entities to train the agent.

This is image title

This is image title

· Adding synonyms to capture track values.

This is image title

This is image title

· created a parameter to fetch track

This is image title

This is image title

· Enable webhook calls are deployed code through web service to provide data to User.

This is image title

· Fulfillment: By default, your agent responds to a matched intent with a static response. If you’re using one of the integrations options, you can provide a more dynamic response by using fulfillment. When you enable fulfillment for an intent, Dialogflow responds to that intent by calling a service that you define. For example, if an end-user wants to schedule a haircut on Friday, your service can check your database and respond to the end-user with availability information for Friday.

  1. The end-user types or speaks an expression.

  2. Dialogflow matches the end-user expression to an intent and extracts parameters.

  3. Dialogflow sends a webhook request message to your webhook service. This message contains information about the matched intent, the action, the parameters, and the response defined for the intent.

  4. Your service performs actions as needed, like database queries or external API calls.

  5. Your service sends a webhook response message to Dialog Flow. This message contains the response that should be sent to the end-user.

  6. Dialogflow sends the response to the end-user.

  7. The end-user sees or hears the response.

  8. Create a cloud function fulfillment to handle the webhook request. Based on the detected track number, an appropriate response will be send to the user.

This is image title

· Inline Editor: · The Dialogflow Console has a built-in code editor, called the inline editor that you can use to create fulfillment code and deploy the code to Clout functions for Firebase. · The inline editor is intended for simple fulfillment testing and prototyping. Once you are ready to build a production application, you should create a webhook service. 4. 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. // Java V2 implementation ‘com.google.cloud:google-cloud-dialogflow:0.67.0-alpha’ 5. The chatbot will be initiated in the onCreate method of the Android activity. We will first configure the Dialog Flow 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. · Client access token can not be used because that was used by V1 API. · We are using V2 so, we will use the Json key. private void initV2Chatbot() { try { InputStream stream = getResources().openRawResource(R.raw.test_agent_credentials); GoogleCredentials credentials = GoogleCredentials.fromStream(stream); String projectId = ((ServiceAccountCredentials)credentials).getProjectId(); SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder(); SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build(); sessionsClient = SessionsClient.create(sessionsSettings); session = SessionName.of(projectId, uuid); } catch (Exception e) { e.printStackTrace(); } } · The test_agent_credentials is the JSON file which we downloaded in First step from the Service account. · We have copy and paste that file in the directory app -> res -> raw(new directory) and Paste. 6. 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 send as a request to the chatbot and the response is captured. public class RequestJavaV2Task extends AsyncTask<Void, Void, DetectIntentResponse> { Activity activity; private SessionName session; private SessionsClient sessionsClient; private QueryInput queryInput; RequestJavaV2Task(Activity activity, SessionName session, SessionsClient sessionsClient, QueryInput queryInput) { this.activity = activity; this.session = session; this.sessionsClient = sessionsClient; this.queryInput = queryInput; } @Override protected DetectIntentResponse doInBackground(Void… voids) { try{ DetectIntentRequest detectIntentRequest = DetectIntentRequest.newBuilder() .setSession(session.toString()) .setQueryInput(queryInput) .build(); return sessionsClient.detectIntent(detectIntentRequest); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(DetectIntentResponse response) { ((MainActivity) activity).callbackV2(response); } } 7. The user can send a query from the Android app using EditText and the response will be shown in a TextView. private void sendMessage(View view) { String msg = queryEditText.getText().toString(); if (msg.trim().isEmpty()) { Toast.makeText(MainActivity.this, “Please enter your query!”, Toast.LENGTH_LONG).show(); } else { showTextView(msg, USER); queryEditText.setText(“”); // Java V2 QueryInput queryInput = QueryInput.newBuilder().setText(TextInput.newBuilder().setText(msg).setLanguageCode(“en-US”)).build(); new RequestJavaV2Task(MainActivity.this, session, sessionsClient, queryInput).execute(); } } · Get Response: public void callbackV2(DetectIntentResponse response) { if (response != null) { // process aiResponse here String botReply = response.getQueryResult().getFulfillmentText(); showTextView(botReply, BOT); } else { showTextView(“There was some communication issue. Please Try again!”, BOT); } } Conclusion: In this blog, we have explained about how to build Chatbot with Dialogflow. This post is helpful to beginners and experienced mobile app developers who want to build chat bot in mobile apps.

Android App developers Java API

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Android App Development Company

Skenix Infotech stands as a top Android App Development Company in India & USA that deliver android apps by our highly skilled android application developers.

Hire Android App Developer

Looking for a dedicated team or individual developer for your app idea? Android is the most-used OS in smartphones. It has the capability to reach a wider audience base. So, **[Hire Android App...

Best Android App Development Company in India

AppClues Infotech is the best mobile app development company in New York that offers custom mobile app development & design services for Android and iOS.

Best Android App Development Company New York

Are you looking for the [Best Android App Development Company in New York](https://www.appcluesinfotech.com/android-app-development-company/ "Best Android App Development Company in New York")? if yes? then you have come to the right place....