Beginner's guide to creating an empathetic chatbot
Tags: beginners, machinelearning, webdev
Published: 2020-07-10 17:51:31

"I am sorry, I didn't catch that. Please try again."

There is nothing more frustrating than talking to a chatbot that does not know how to help you. You find yourself repeating "let me talk to a human" multiple times before you get so frustrated that you leave the conversation. 

![alt text image](https://media.giphy.com/media/yoJC2yftYP4kP6Ku40/source.gif)

Unfortunately, frustrations caused by chatbots happen too often and it could forever ruin customers' sentiment towards your brand.

If only we could build a chatbot to detect customers' negative emotions so we can take right measures to help the them...

Wait.. we can! 

[Dialogflow](https://cloud.google.com/dialogflow) is a natural language processing platform used to build conversational user interface like chatbots. 

With Dialogflow, you can give unique personality to your chatbot and analyze emotional tone of customers based on the words that they use. 

**By end of this blog, you will be able to:**
1. create an agent and intent
2. perform sentiment analysis

Let's get to it!

####STEP 1: Sign into [Dialogflow] with your Google account (https://dialogflow.cloud.google.com/#/login) then choose the standard edition(free).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2kw02xfuq4xrnc8uh3up.png)

####STEP 2: Create an agent
Agent is a virtual agent that handles conversations with your end-users.

In the upper left corner of your console, click on create new agent. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ntmpzqjoa04tutrpf5j6.png)

Follow the red arrows to complete the following steps.

1. Name your bot to SentimentBot
2. Choose the default language of your choice
3. Choose your time zone
4. Click on create button
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n0iuhsq0r4dojn6eljya.png)

This process will create a new agent and a new Google project. You will see the following displayed in your browser.  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zgytkvdwcwbah51uk6bf.png)

####STEP 3: Train the intent with example phrases for what customers will say

Intent is customers' motive for talking to a chatbot. For example, when customers say "I want headphones", their intent is most likely that they want to browse headphone selections.  

We can train a chatbot to recognize customer's intent by providing variations of typical phrases a customer will say. 

Click on the plus sign (red arrow 1) then click on add training phrase (red arrow 2).
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lj6bh8vcbydz614qweha.png)

In the image below, there is a section highlighted with a red arrow. Name the intent "refund policy".

Then, provide variations of phrases that a customer will say to learn about the refund policy(highlighted with red line).  

Some of the phrases could be:
- "How much time do I have until I can return an item"?
- "Can I still get a refund after 30 days? 
- "What is the refund policy?"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6lncesaft7tv9iukbnbf.png)

####STEP 4: Train your chatbot with relevant responses for specific intent
Our chatbot would be useless if it recognized customers' intent but had no idea how to respond!

Let's train the chatbot with some of the responses we want it to say to our customers. 

Scroll down to responses and click on ADD RESPONSE.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kungq2nostq2bmp5jel9.png)

In the image below, there is a section highlighted with a red box. Add variations of responses that you would like your chatbot to display to the customer. 

Some of the responses could be...
- We have a 30 day return policy. All items returned in its original packaging with approved reasons for return will be given a full refund within 14 days. 

- Thanks for asking! As long as items are returned in its original packaging with approved reasons for return, we will give you a refund within 14 days. Make sure that your request is within 30 days of purchase. 

- Great question! As long as items are returned within 30 days in its original packaging with approved reasons for return, refund will be issued within 14 days. 

Once you have added your sample responses, click save. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p1fad6llck4jvj3kt6av.png)

####STEP 4: Test it out!
In the image below, look at the box highlighted in red. In the region highlighted with red arrow, copy and paste one of the customer example phrases you have trained your chatbot with.

You will see that our chatbot is responding exactly the way we trained it to do! 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0okolwy9m6ohscz4aqd0.png)

You can create as many intent and responses to fit your use case.
Now, let's move on to sentiment analysis!

####STEP 5: Add sentiment analysis to your chatbot
To add sentiment analysis functionality to your chatbot, you need to upgrade to Enterprise Edition Essentials. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l2puq72iyqg76da7a364.png)

At first, I felt weary about upgrading to a paid version. But the cost is very low($0.002 cents/text request) so I thought it was worth trying out!

Once you upgrade, you will see that your plan has been changed to Enterprise Essentials, Pay as You Go plan. 

Click on settings button next to SentimentBot
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x4yjgrkcsh7hl1oo5rwd.png)

Click on Advanced tab(arrow #1) then enable sentiment analysis(arrow #2) then save(arrow #3)!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3i35ze2u8vcgpy7xq8wd.png)

####STEP 6: Test sentiment analysis
In the image below, you will see a box highlighted with a red arrow. Write something positive such as "Great customer service!"

If you look at the Sentiment section highlighted with a green arrow, it will show a query score of 0.9.

Positive 1 is the highest query score in the happiness scale you can achieve. Our chatbot senses that it is doing a great job and the customer is very happy. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cbihnvarxxohvzzticjd.png)

What if we enter a negative comment by a customer? 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vp6o5vkb697xnu39eiam.png)

A query score of -0.9. Ouch! Chatbot senses that the customer is not very happy. 

There you have it!Try this out and integrate Dialogflow to your app. You can then determine how you want your app to take care of an unhappy customer based on sentiment query score!