Sentiment Analysis
Combination of Channel and Overlay to do Sentiment Analysis via Google Cloud API.
- Utilizes store.org.sentiment for results (array of 11 numbers representing counts of sentiment from 0 to 10)
- Stores the last message sentiment in the customer store as well in case they want to use that for an agent facing app or something.
- Note that the API key below utilizes our Google Cloud API key (the same one we usually use for Google Maps — we should encourage customers to get their own Google Cloud API key instead of using our credential.
Channel App [Shared]
Needs to be installed on every channel where you want Sentiment Analysis to take place...
var payload = {
"document": {
"type": "PLAIN_TEXT",
"language": "en",
"content": message
}
}
chatbox.buildCallExternalWebAPI("POST", "https://language.googleapis.com/v1beta2/documents:analyzeSentiment?key=AIzaSyBL40Gq2YPrxKo7gNzrO-8VWPxTSLvxPWI").value(payload).eventName("sentiment");
Sentiment Callback:
if (statusCode == 200) {
app.setElementLabel("value", returnData);
var rd = JSON.parse(returnData);
//returns -1.0 to 1.0
// convert to 0 to 10
var score = Math.round(((rd.documentSentiment.score * 10) + 10) / 2);
store.save("customer", "lastSentiment", score);
var x = json(store.org.sentiment, [0,0,0,0,0,0,0,0,0,0,0]);
x[score] = x[score] + 1;
store.save("org", "sentiment", JSON.stringify(x));
}
Overlay App [Shared]
Data Changed
var d = json(store.org.sentiment, [0,0,0,0,0,0,0,0,0,0,0]);
var l = [];
for (var i = 0; i < d.length; i++) {
l.push({"x": String(i), "y": d[i]});
}
app.setElementProperties("chart", {"data": l});