Using IBM Watson to detect Facebook messages’ tone and prioritize support requests

Giuliano Iacobelli
9 min readAug 23, 2016

--

Not every customer that reaches out for support is a happy camper. When an unhappy customer walks through the door you want to react instantly to avoid any bad escalation. But tracking the mood of conversations at scale is hard.

Luckily IBM Watson comes to rescue us with its Tone Analyzer APIs! In this post we’re going to do just that using Stamplay and IBM Watson’s API along with a Facebook Page.

Things you’ll need:

Let’s go!

Overview

With Stamplay you can create new services combining existing ones often without much code. These API mashups are called Flows and ours will do the following:

When a customer writes us a message to our Facebook Page, we’ll forward it into the Watson Tone Analyzer API which will give us an emotional analysis of what was said. If one of the values or greater than some threshold that we decide, we will send an alert via email and optionally on a communication tool such as Slack.

1. Creating the Facebook Messenger app

Let’s start by creating a new app and enable Messenger integration on one of our pages.

Go to developers.facebook.com and create a new app, don’t select any four options and just click on basic setup to move on.

Create the new app as an App for Pages

Once the App has been created, you’ll see the Product Setup View and here we select Messenger by clicking on Get Started

1. Initializing the Flow

Go to editor.stamplay.com/home and create a new app. Once this is done, we can start building our flow.

  1. Click on Flow on the left-hand sidebar and then click the rounded green button in the upper right corner of the page to initialize a new one.

The first step of a flow is always a trigger. A trigger is an event fired by an API connected to our Stamplay app, in this example we want this to be fired everytime we receive a new message on Messenger. So let’s select Messenger from the dropdown menu

And then select New Message which is the event we want to listen for. Once this is selected click on Continue.

Now we are prompted for a Page Token:

Let’s get it on Facebook, go back to your Facebook app, go to Messenger settings and select your Page from the dropdown available in the Token Generation section

Copy and paste it on the Page Access Token field on Stamplay and click the Connect button and then Continue

Last step for this connection is to configure Messenger webhooks to have it sending notification in realtime to Stamplay and trigger this flow. Copy the URL displayed on Stamplay and then go back to our Facebook app.

On Messenger settings, right below the Token Generator section, click on Setup Webhooks, paste the URL you just copied on Stamplay, type stamplay in the Verify Token field and check messages and messaging_postbacks checkboxes. Click Verify and Save, and then click on Save on Stamplay to finalize this first step.

You will end up in seeing this

2. Connecting Watson Tone Analyzer API

Now it’s time to add a new Step and a new API integration to our flow. So let’s plug IBM Watson super powers into this app!

  1. Click on Action to start configuring a new step, and this time let’s look for Tone Analyzer API from the dropdown and then click Continue.

Now we need to add the credentials to use Tone Analyzer API in this app. Go to http://www.ibm.com/cloud-computing/bluemix/watson and create an account.

Once logged in, select the Catalog tab and enter “watson tone analyzer” in the search input. Click Create to add the service to your account.

Go to Service Credentials, and copy the username & password values.

Go back on Stamplay and paste the credentials in the Account tab of the Tone Analyzer step we’re building, click Connect and then Continue.

For the Text field, open the dropdown field by clicking on the button to the right of the input to select the incoming message data from the previous step of this Flow.

Here we are able to inspect the data payload sent by Messenger and pick the values that we want to pass dynamically to the following step, for this use case we just need the text property nested under message.

After clicking on text, the text area will look similar to the one in the picture below. The signature between the curly brackets tells to the system that the input of the IBM Watson step comes from a field of the output of a previous step.

Click on Save, an now turn on the flow by clicking on the red switch on the upper right corner.

3. Test Checkpoint

Time to check if everything is fine, let’s turn on the flow clicking on the switch and send a test message to our page. If everything has been configured fine the Flow will be executed and we’ll be able to see its execution result in the Flow history.

Click on the chrono icon right next to the switch to open Flow log, and you should see your first successful execution.

You can inspect the results of the API requests step-by-step by clicking on the magnifier icon but now let’s move on and get back to edit our Flow.

4. Filtering hi-emotion messages

Now we need to add a Filter to the Flow so that the following step will only be fired if the Filter conditions are met.

In your Flow, go on the + icon of the second step and select Add Filter.

Before this flow moves to the next step, we want to check for the following 4 negative tone scores: Anger, Disgust, Fear, and Sadness. So in the “Property” field, select the dynamic score for each of these negative tones. For the “Condition” field, select (Number) Greater than. Last, enter 0.75 in the “Condition Value” field, as tone strength is scored on scale from 0 to 1 per the Watson Tone Analyzer API docs. Click Save.

5. Sending out Email alerts

Let’s add one more action to this Flow that will send a notification via email if there is negative tone detected within the message. ì

Within the Flow, click Add Action.

Select Email > Send Email and click Continue.

Let’s configure the email that we want to receive from Stamplay, we can configure who will be the recipient with the To field, who’s the sender (no need to be an existing email address) with the From and From Name fields, the Subject and the Body of the email message.

In this example the body of the email will contain once again the body of the message received. So we can personally check if we actually need to access our Facebook Page messenger right away! We do this again by picking the result from a previous step.

Click Save and you’re good to go! You’ll be notified everytime someone sends a very angry or a very happy message. Isn’t it super cool? If you want to send the notification to Slack keep reading!

6. Sending notification to Slack (Optional)

If you want to take an extra step and receive a nice Slack notification like the one below a few extra steps are required, just 3 extra minutes, ready?

After the email step, let’s add a new one, this time we’ll select Slack from the dropdown and then check the Post Message action.

Click Connect, then when prompted click Authorize to grant access to your Slack organization to this Stamplay app.

Next, select the Slack channel from the “Channel” dropdown. For the “Message” field, enter “Emotional Peak on Facebook Page”.

Important: in the “Attachments” textarea, copy & paste the code below and populate it with your own app values using the JSON selector to the right of the input.So you have to replace every single variable (those with the double curly brackets) with yours.

Attachments let you add more context to a message by referencing images, external websites, and relevant pieces of data. (Read more on attachments).

Attachments

[ {
“color”: “danger”,
“title”: “User message tone analysis”, “text”:”{{e69Y.message.body.message.text}}”,
“fields”: [
{ “title”: “Anger”, “value”: “{{xZIe.tone.document_tone.tone_categories.[0].tones.[0].score}}”, “short”: true },
{ “title”: “Disgust”, “value”: “{{xZIe.tone.document_tone.tone_categories.[0].tones.[1].score}}”, “short”: true },
{ “title”: “Fear”, “value”: “{{xZIe.tone.document_tone.tone_categories.[0].tones.[2].score}}”, “short”: true },
{ “title”: “Sadness”, “value”: “{{xZIe.tone.document_tone.tone_categories.[0].tones.[4].score}}”, “short”: true }
] } ]

Conclusion

As you can see, we were easily able to setup an emotion analyzer for our Facebook Page messages with Watson’s Tone Analyzer API! Now you can be alerted whenever a customer is having a bad experience and step in to save the day, which is sure to increase your customer satisfaction rating :)

If you have an app you want to build with Stamplay, you can signup HERE. Also, feel free to shoot us a tweet at @stamplay and/or join our Slack organization for real-time updates and community support!
Happy coding!

--

--

Giuliano Iacobelli

Co-Founder @Stamplay, Lego™ for APIs. Enabling people to unleash the power of APIs. A #500STRONG Software Engineer in love with web products. Hip Hop addicted.