subcollection | copyright | lastupdated | lasttested | ||
---|---|---|---|---|---|
solution-tutorials |
|
2019-10-24 |
2019-10-22 |
{:java: #java .ph data-hd-programlang='java'} {:swift: #swift .ph data-hd-programlang='swift'} {:ios: #ios data-hd-operatingsystem="ios"} {:android: #android data-hd-operatingsystem="android"} {:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
{: #android-watson-chatbot}
Learn how easy it is to quickly create a voice-enabled Android-native chatbot with {{site.data.keyword.conversationshort}}, {{site.data.keyword.texttospeechshort}} and {{site.data.keyword.speechtotextshort}} services on {{site.data.keyword.Bluemix_short}}.
This tutorial walks you through the process of defining intents and entities and building a dialog flow for your chatbot to respond to customer queries. You will learn how to enable {{site.data.keyword.speechtotextshort}} and {{site.data.keyword.texttospeechshort}} services for easy interaction with the Android app. {:shortdesc}
{: #objectives}
- Use {{site.data.keyword.conversationshort}} to customize and deploy a chatbot.
- Allow end users to interact with chatbot using voice and audio.
- Configure and run the Android app.
{: #services}
This tutorial uses the following products:
- {{site.data.keyword.conversationfull}}
- {{site.data.keyword.speechtotextfull}}
- {{site.data.keyword.texttospeechfull}}
{: #architecture}
- Users interact with a mobile application using their voice.
- The audio is transcribed to text with {{site.data.keyword.speechtotextfull}}.
- The text is passed to {{site.data.keyword.conversationfull}}.
- The reply from {{site.data.keyword.conversationfull}} is converted to audio by {{site.data.keyword.texttospeechfull}} and the result sent back to the mobile application.
{: #prereqs}
- Download and install Android Studio.
{: #setup}
In this section, you will create the services required by the tutorial starting with {{site.data.keyword.conversationshort}} to build cognitive virtual assistants that help your customers.
- Go to the {{site.data.keyword.Bluemix_notm}} Catalog and select {{site.data.keyword.conversationshort}} service > Lite plan:
- Set Name to android-chatbot-assistant
- Create.
- Click Service credentials on the left pane and click New credential.
- Set Name to for-android-app.
- Add.
- Click View Credentials to see the credentials. Make note of the API Key and URL, you will need it for the mobile application.
The {{site.data.keyword.speechtotextshort}} service converts the human voice into the written word that can be sent as an input to {{site.data.keyword.conversationshort}} service on {{site.data.keyword.Bluemix_short}}.
- Go to the {{site.data.keyword.Bluemix_notm}} Catalog and select {{site.data.keyword.speechtotextshort}} service > Lite plan.
- Set Name to android-chatbot-stt.
- Create.
- Click Service credentials on the left pane and click New credential to add a new credential.
- Set Name to for-android-app.
- Add.
- Click View Credentials to see the credentials. Make note of the API Key and URL, you will need it for the mobile application.
The {{site.data.keyword.texttospeechshort}} service processes text and natural language to generate synthesized audio output complete with appropriate cadence and intonation. The service provides several voices and can be configured in the Android app.
- Go to the {{site.data.keyword.Bluemix_notm}} Catalog and select {{site.data.keyword.texttospeechshort}} service > Lite plan.
- Set Name to android-chatbot-tts.
- Create.
- Click Service credentials on the left pane and click New credential to add a new credential.
- Set Name to for-android-app.
- Add.
- Click View Credentials to see the credentials. Make note of the API Key and URL, you will need it for the mobile application.
{: #create_workspace}
A skill is a container for the artifacts that define the conversation flow.
For this tutorial, you will save and use Ana_skill.json file with predefined intents, entities and dialog flow to your machine.
- In the {{site.data.keyword.conversationshort}} service details page, navigate to Manage on the left pane, click on Launch {{site.data.keyword.conversationshort}} to see the {{site.data.keyword.conversationshort}} dashboard.
- Click on Skills tab.
- Create skill, select Dialog skill and Next.
- Import skill and choose the JSON file downloaded above.
- Select Everything option and click Import. A new skill is created with predefined intents, entities and dialog flow.
- Go back to the list of Skills. Select the action menu on the
Ana
skill to View API Details.
{:#define_intent}
An intent represents the purpose of a user's input, such as answering a question or processing a bill payment. You define an intent for each type of user request you want your application to support. By recognizing the intent expressed in a user's input, the {{site.data.keyword.conversationshort}} service can choose the correct dialog flow for responding to it. In the tool, the name of an intent is always prefixed with the #
character.
Simply put, intents are the intentions of the end-user. The following are examples of intent names.
#weather_conditions
#pay_bill
#escalate_to_agent
-
Click on the newly created skill - Ana.
Ana is an insurance bot for users to query their health benefits and file claims. {:tip}
-
Click on the first tab to see all the Intents.
-
Click on Create intent. Enter
Cancel_Policy
as your intent name after#
and provide an optional description. -
Click Create intent.
-
Add user examples when requested to cancel a policy
I want to cancel my policy
Drop my policy now
I wish to stop making payments on my policy.
-
Add user examples one after another and click Add example. Repeat this for all the other user examples.
Remember to add at least 5 user examples to train your bot better. {:tip}
-
Click the close button next to the intent name to save the intent.
-
Click on Content Catalog and select General. Click Add to skill.
Content catalog helps you in getting started faster by adding existing intents (banking, customer care, insurance, telco, e-commerce and many more). These intents are trained on common questions that users may ask. {:tip}
{:#define_entity}
An entity represents a term or object that is relevant to your intents and that provides a specific context for an intent. You list the possible values for each entity and synonyms that users might enter. By recognizing the entities that are mentioned in the user's input, the {{site.data.keyword.conversationshort}} service can choose the specific actions to take to fulfill an intent. In the tool, the name of an entity is always prefixed with the @
character.
The following are examples of entity names
@location
@menu_item
@product
-
Click Entities tab to see the existing entities.
-
Click Create entity and enter the name of the entity as
location
after@
. Click Create entity. -
Enter
address
as the value name and select Synonyms. -
Add
place
as a synonym and click the icon. Repeat with synonymsoffice
,centre
,branch
etc., and click Add Value. -
Click System entities tab to check the common entities created by IBM that could be used across any use case.
System entities can be used to recognize a broad range of values for the object types they represent. For example, the
@sys-number
system entity matches any numerical value, including whole numbers, decimal fractions, or even numbers written out as words. {:tip} -
Toggle the Status from off to
on
for@sys-person
and@sys-location
system entities.
{:#build_dialog}
A dialog is a branching conversation flow that defines how your application responds when it recognizes the defined intents and entities. You use the dialog builder in the tool to create conversations with users, providing responses based on the intents and entities that you recognize in their input.
-
Click on Dialog tab to see the existing dialog flow with intents and entities.
-
Click Add node to add a new node to the dialog.
-
Under if assistant recognizes, enter
#Cancel_Policy
. -
Under Then respond with, enter the response
This facility is not available online. Please visit our nearest branch to cancel your policy.
-
Scroll to see the
#greeting
node. Click on the node to see the details. -
Click the icon to add a new condition. Select
or
from the dropdown and enter#General_Greetings
as the intent. Then respond with section shows the assistant's response when greeted by the user.A context variable is a variable that you define in a node, and optionally specify a default value for. Other nodes or application logic can subsequently set or change the value of the context variable. The application can pass information to the dialog, and the dialog can update this information and pass it back to the application, or to a subsequent node. The dialog does so by using context variables. {:tip}
-
Test the dialog flow using the Try it button.
An assistant is a cognitive bot that you can customize for your business needs, and deploy across multiple channels to bring help to your customers where and when they need it. You customize the assistant by adding to it the skills it needs to satisfy your customers' goals.
- In the {{site.data.keyword.conversationshort}} tool, switch to Assistants and use Create assistant.
- Set Name to android-chatbot-assistant
- Create assistant
- Use Add dialog skill to select the skill created in the previous sections.
- Add existing skill
- Select Ana
- Select the action menu on the Assistant > Settings > API Details, make note of the Assistant ID, you will need to reference it from the mobile application( in the
config.xml
file of the Android app).
{:#configure_run_android_app}
The repository contains Android application code with required gradle dependencies.
-
Run the below command to clone the GitHub repository to your machine:
git clone https://github.com/IBM-Cloud/chatbot-watson-android
{:pre}
-
Launch Android Studio on your machine. Choose Open an existing Android Studio project from the options and point to the cloned code on your machine.Gradle build will automatically be triggered and all the dependencies will be downloaded.
-
From the left pane of Android Studio or from the cloned folder, Open the
config.xml
file under the pathapp/src/main/res/values
to see the placeholders(ASSISTANT_ID_HERE
) for service credentials. Enter the service credentials (you saved earlier) in their respective placeholders and save the file.<?xml version="1.0" encoding="UTF-8"?> <resources> <!--Watson Assistant service credentials--> <!-- REPLACE `ASSISTANT_ID_HERE` with ID of the Assistant to use --> <string name="assistant_id">ASSISTANT_ID_HERE</string> <!-- REPLACE `ASSISTANT_API_KEY_HERE` with Watson Assistant service API Key--> <string name="assistant_apikey">ASSISTANT_API_KEY_HERE</string> <!-- The URL provided below is for DALLAS region/location --> <!-- If the Assistant service is created in another region/lpcation --> <!-- REPLACE the URL with the one provided in the credentials of the service you created--> <string name="assistant_url">https://gateway.watsonplatform.net/assistant/api</string> <!--Watson Speech To Text(STT) service credentials--> <!-- REPLACE `STT_API_KEY_HERE` with Watson Speech to Text service API Key--> <string name="STT_apikey">STT_API_KEY_HERE</string> <!-- The URL provided below is for DALLAS region/location --> <!-- If the STT service is created in another region/lpcation --> <!-- REPLACE the URL with STT URL provided in the service credentials --> <string name="STT_url">https://stream.watsonplatform.net/speech-to-text/api</string> <!--Watson Text To Speech(TTS) service credentials--> <!-- REPLACE `TTS_API_KEY_HERE` with Watson Text to Speech service API Key--> <string name="TTS_apikey">TTS_API_KEY_HERE</string> <!-- The URL provided below is for DALLAS region/location --> <!-- If the TTS service is created in another region/lpcation --> <!-- REPLACE the URL with TTS URL provided in the service credentials --> <string name="TTS_url">https://stream.watsonplatform.net/text-to-speech/api</string> </resources>
The URLs provided are for Dallas region/location. If you have created the services in a different region/location, replace with the URL provided in the service credentials of the respective service. {:tip}
-
Build and Run the project by clicking Run from the Android studio menu > click Run app and start the application on a real Android device or with a simulator.
![](images/solution28-watson-chatbot-android/android_watson_chatbot.png)![](images/solution28-watson-chatbot-android/android_chatbot.png)
For detailed instructions to run your app on a real Android device or on an emulator, follow the instructions here. To build your app from the command line, follow the instructions provided here {:tip}
-
In the Enter your query space provided below, interact with your chatbot by typing
Hi
and click the arrow icon to send the query to {{site.data.keyword.conversationshort}} service. -
The response will be passed to {{site.data.keyword.texttospeechshort}} service and you should hear a voice reading out the response.
-
Click the mic icon in the left bottom corner of the app to input speech that gets converted to text and then can be sent to {{site.data.keyword.conversationshort}} service by clicking the arrow icon.
{:removeresources}
- Navigate to Resource List,
- Delete the services you have created:
- {{site.data.keyword.conversationfull}}
- {{site.data.keyword.speechtotextfull}}
- {{site.data.keyword.texttospeechfull}}
{:related}