{{announcement.body}}
{{announcement.title}}

Axway Integration Builder in a Sentiment Analysis Application, Part 1

DZone 's Guide to

Axway Integration Builder in a Sentiment Analysis Application, Part 1

This article demonstrates how to use Axway Integration Builder to create a sentiment analysis application based on two examples.

· Integration Zone ·
Free Resource

Introduction

Sentiment analysis (also known as opinion mining or emotion AI) refers to the use of natural language processing, text analysis, computational linguistics, and biometrics to identify systematically, extract, quantify and study affective states and subjective information. Sentiment analysis is widely applied to voice of the customer materials such as reviews and survey responses, online and social media, and healthcare materials for applications that range from marketing to customer service to clinical medicine.

We’ll use Integration Builder to extract the text from our data source, make a call to Amazon Comprehend natural language processing (NLP) service, through API Builder, to determine the sentiment and send a notification to the appropriate stakeholder.

We’ll look at two examples:

In both cases, we would like to notify a stakeholder if a negative sentiment is discovered. For our example, we’ll send a notification to Microsoft Teams but we could also or alternatively, use Integration Builder’s Twilio connector to send an SMS to a mobile phone or the Slack connector to send a notification to Slack.

This will give the stakeholder a means to be proactively aware of negative sentiment and take immediate corrective action instead of waiting for the data to be analyzed manually.

In this blog post, we’ll focus on the Net Promoter Score survey example. In the next blog post, we’ll look at the Salesforce example.

Setup

The diagram below illustrates the flow of data:


  • We’ll use Typeform for our NPS Survey since it is (a) easy to use, (b) provides a free tier and (c) provides a means to call a webhook when the user submits the completed form.
  • In this example, our NPS Survey will contain two questions:
    • “How likely are you to recommend us to a friend or colleague?”
    • “What is the primary reason for your score?”
  • We’ll look for the sentiment in the second question (What is the primary reason for your score?)
  • We’ll configure our Integration Builder flow for manual trigger since that will give us an API in AMPLIFY Central in order to trigger the flow as described here. This is the webhook that we’ll use in Typeform
  • We’ll create an API Builder project to expose AWS Comprehend’s sentiment service as a sentiment API as described here. We’ll publish this API Builder project to Axway AMPLIFY Runtime Services (ARS) as described here
  • We’ll use the swagger docs for our API Builder microservice to create a connector in Integration Builder as described here to make it easy to call the sentiment API in Integration Builder
  • If the sentiment is determined to be negative, we’ll send a notification to MS Teams as shown below:

Note that information in the notification contains the sentiment, the answer to the question, the form name and id and the token corresponding to the particular response

Typeform NPS Survey

In this section, we’ll create a Typeform NPS Survey.

You can review the Typeform Help Center for help on how to create a survey.

Here is what we’ll do:

  • Create a simple survey
  • Assign a reference to the question that you are concerned about
  • Configure a webhook so that when the survey is submitted, our flow will be triggered

I created a simple NPS Survey made up of two questions as shown below:



As you are creating your form, note the form ID in the URL as shown below:

Note that my form has a form id of QsM3CQ. We will use this in our flow to make sure we are processing the correct form.

For our NPS Survey, we are mainly concerned about the second question, “What is the primary reason for your score?” since that is where the user may express their sentiment. When our Integration Builder flow parses the survey data we will look for the second question. Typeform enables you to assign a unique name to the question to make the coding easier as shown below:

Note that I named the question ref sentimentQuestion. We’ll see this later when we look our Integration Builder flow.

Finally, I configured the survey to send the survey data to a webhook as shown below:


The webhook is the AMPLIFY Central API corresponding to my Integration Builder manually triggered flow.

Note that at this point we don’t know the webhook URL yet because we haven’t created our flow but we’ll get to that soon

We can test our survey and webhook by using a service like pipedream’s requestbin, a modern request bin to inspect HTTP events and see what is posted, as shown below:

HTTP
 




x
50


 
1
{
2
    "event_id": "01E82GJ98P8YCTF6N8TRDV8PA1",
3
    "event_type": "form_response",
4
    "form_response": {
5
        "form_id": "QsM3CQ",
6
        "token": "5ndeib6dyv37m70eo06ae5ndeib6gezh",
7
        "landed_at": "2020-05-11T18:39:37Z",
8
        "submitted_at": "2020-05-11T18:39:44Z",
9
        "definition": {
10
            "id": "QsM3CQ",
11
            "title": "Axway NPS Poll May 2020",
12
            "fields": [
13
                {
14
                    "id": "kYJjqO9kwidB",
15
                    "title": "How likely are you to recommend us to a friend or colleague?",
16
                    "type": "opinion_scale",
17
                    "ref": "c525dc67-d2d0-438f-81a8-6efa4c0443fc",
18
                    "properties": {}
19
                },
20
                {
21
                    "id": "tVprjNrOIfM2",
22
                    "title": "What is the primary reason for your score?",
23
                    "type": "long_text",
24
                    "ref": "sentimentQuestion",
25
                    "properties": {}
26
                }
27
            ]
28
        },
29
        "answers": [
30
            {
31
                "type": "number",
32
                "number": 8,
33
                "field": {
34
                    "id": "kYJjqO9kwidB",
35
                    "type": "opinion_scale",
36
                    "ref": "c525dc67-d2d0-438f-81a8-6efa4c0443fc"
37
                }
38
            },
39
            {
40
                "type": "text",
41
                "text": "Terrible Support",
42
                "field": {
43
                    "id": "tVprjNrOIfM2",
44
                    "type": "long_text",
45
                    "ref": "sentimentQuestion"
46
                }
47
            }
48
        ]
49
    }
50
}


Note the question ref sentimentQuestion at the bottom of the response. We will parse the response for that answer in our Integration Builder flow.

At this point, we’re done with building our Typeform NPS Survey. We’ll have to come back later and set up the webhook URL but for now we can move onto API Builder.

API Builder Sentiment API

In this section, we’ll create an API Builder microservice that will expose the AWS Comprehend sentiment service.

Here is what we’ll do:

  • Create an API Builder project to expose AWS Comprehend’s sentiment service as a sentiment API as described here.
  • Publish this API Builder project to Axway AMPLIFY Runtime Services (ARS) as described here.
  • Get the swagger docs for this deployed microservice

Follow this blog post to create your API Builder project but instead of using the AWS Comprehend detectDominantLanguage service, use the detectSentiment service as shown below:

JavaScript
 




xxxxxxxxxx
1
43


 
1
var APIBuilder = require('@axway/api-builder-runtime');
2
 
          
3
var AWS = require('aws-sdk');
4
AWS.config.update({
5
  region: 'us-east-1'
6
});
7
var comprehend = new AWS.Comprehend();
8
 
          
9
var detectsentiment = APIBuilder.API.extend({
10
    group: 'detectsentiment',
11
    path: '/api/detectsentiment',
12
    method: 'GET',
13
    description: 'this is an api that detects the sentiment of the text passed in the text query parameter',
14
    parameters: {
15
        text: { description: 'the text to detect the sentiment of' }
16
    },
17
    action: function (req, resp, next) {
18
 
          
19
        console.log(req.params.text);
20
 
          
21
        var params = {
22
        LanguageCode: "en",
23
        Text: req.params.text
24
      };
25
 
          
26
        comprehend.detectSentiment(params, function(err, data) {
27
        if (err) {
28
                console.log(err, err.stack);
29
                resp.response.status(400);
30
                resp.send(err);
31
            }
32
        else {
33
                console.log(data);
34
                resp.response.status(200);
35
                resp.send(data);
36
            }
37
            next();
38
      });
39
 
          
40
    }
41
});
42
 
          
43
module.exports = detectsentiment;


Note that we are assuming that the language is english in our API above. Instead we could have used the AWS Comprehend language detection service described here and then called the detectSentiment service with the proper language

Test your API and when you are satisfied that it’s working properly, you can follow the instructions here to create a Docker image and deploy in any docker platform or to publish to the Axway’s platform runtime service, ARS.

The swagger for your microservice is accessible from /apidoc/swagger.json. If you used ARS, then it looks similar to following:

JSON
 




xxxxxxxxxx
1


 
1
https://<SUB_DOMAIN_TOKEN>.cloudapp-enterprise.appcelerator.com/apidoc/swagger.json


Paste the swagger contents into a swagger.json file on your computer. We’ll use this later when we create an Integration Builder connector to this microservice.

Integration Builder

In this section, we’ll create our Integration Builder flow and create a connector for the API Builder microservice. You can download the flow here.

Here is what we will do:

  • Create a connector to our API Builder microservice
  • Create a manually triggered flow so that when the Typeform survey is submitted the flow will be triggered via a webhook
  • Make sure the flow is processing the correct survey
  • Call the sentiment API Builder microservice via the connector we created
  • Check the sentiment to see if it’s negative (or mixed)
  • If so, send a notification to MS Teams
  • Create an instance of the flow, enter the variables and then get the URL that is proxied in Central. This will be the webhook that we will use for the Typeform webhook when the survey is submitted

The resulting flow is shown below:

Create a Connector to our API Builder Microservice

Follow this blog post to create a connector to the API Builder microservice you published above. I called mine Detect Sentiment. Test your connector in the API Docs:

Create a Manually Triggered Flow

Follow the instructions here to create a manually triggered flow. I called mine NPS Sentiment Notifier.

I created the following flow variables:

  • *detectSentiment* – connector instance variable for the API Builder connector
  • *formId* – value variable for the survey id
  • *msTeamsWebhookURL* – value variable for the MS Teams webhook
  • *sentimentRef* – value variable for the survey question reference who’s sentiment we are looking for

Make Sure the Flow Is Processing the Correct Survey

Add a JS Filter step, shouldProcess, to the output of the trigger. The contents are as follows:

JavaScript
 




xxxxxxxxxx
1


 
1
let form_id = trigger.args.form_response.form_id;
2
let answers = trigger.args.form_response.answers;
3
let obj = answers.find(obj => obj.field.ref == config.sentimentRef);
4
config.myData = {
5
  answerObject: obj
6
}
7
done((form_id === config.formId) && ( obj && obj !== "null" && obj !== "undefined" ));


The JavaScript above performs the following:

  • Checks the form ID to make sure we are processing the correct Typeform survey
  • Checks to see if the user answered the second question (What is the primary reason for your score)
  • Stores the answer to the question for later (in the config object)

Call the Sentiment API

We’ll use a Connector API Request step to call the API Builder sentiment API. First we use a JS Script step called prepareGetSentiment to calculate the query parameters as follows:

JavaScript
 




xxxxxxxxxx
1


 
1
let query = {
2
  text: config.myData.answerObject.text
3
}
4
done({query: query});


The Connector API Request step, getSentiment, is shown below:


Check the Sentiment Response

Add a JS Filter step, shouldNotify, to check if the sentiment response is negative or mixed as follows:

JavaScript
 




xxxxxxxxxx
1


 
1
done(steps.getSentiment.response.body.Sentiment === 'NEGATIVE' || steps.getSentiment.response.body.Sentiment === 'MIXED');


Send a Notification to MS Teams

If the sentiment is negative or mixed, we’ll send a notification to MS Teams.

We’ll use an HTTP Request step to send a notification to MS Teams via a webhook. First, we use a JS Script step called prepareNotifyTeams to calculate the body as follows:

JavaScript
 




xxxxxxxxxx
1
43


 
1
var sentiment;
2
if(steps.getSentiment.response.body.Sentiment === 'NEGATIVE') {
3
  sentiment = 'Negative';
4
} else if(steps.getSentiment.response.body.Sentiment === 'MIXED') {
5
  sentiment = 'Mixed';
6
} else {
7
  sentiment = 'Unknown';
8
}
9
 
          
10
let resultsUrl = "https://admin.typeform.com/form/"+config.formId+"/results";
11
 
          
12
let body = {
13
    "@type": "MessageCard",
14
    "@context": "http://schema.org/extensions",
15
    "themeColor": "0076D7",
16
    "summary": "NPS Score Sentiment Notifier",
17
    "sections": [{
18
        "activityTitle": "NPS Score Sentiment Notifier",
19
        "activitySubtitle": sentiment+" sentiment detected",
20
        "facts": [{
21
            "name": "Sentiment",
22
            "value": sentiment
23
        },{
24
            "name": "Form Name",
25
            "value": trigger.args.form_response.definition.title
26
        },{
27
            "name": "Form ID",
28
            "value": trigger.args.form_response.form_id
29
        },{
30
            "name": "Note",
31
            "value": "'"+config.myData.answerObject.text+"'"
32
        },{
33
            "name": "Form Results",
34
            "value": "[Click to View]("+resultsUrl+")"
35
        },{
36
            "name": "Token",
37
            "value": trigger.args.form_response.token
38
        }],
39
        "markdown": true
40
    }]
41
}
42
 
          
43
done({body: body});



The HTTP Request step, notifyTeams, is shown below:


Create an Instance of The Flow

Create a flow instance and attach the API Builder Detect Sentiment connector and enter the other variables for your Typeform survey and MS Teams webhook:


Test Your Flow

Follow the instructions here to get your flow’s API in AMPLIFY Central. This is the webhook you’ll enter in your Typeform survey.

Fill out your survey and enter a negative sentiment: “Terrible support”. You should get a notification in MS Teams as follows:


Summary

In this blog post, we saw how Integration Builder and API Builder can be used together with AWS Comprehend Natural Language Processing service to implement a sentiment analysis application to alert marketing via MS Teams. This enables a company to be more proactive in analyzing Net Promoter Score survey responses.

Topics:
axway, integration, integration builder, nps score, sentiment analysis

Published at DZone with permission of Leor Brenman , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}