Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created October 26, 2016 12:00
Show Gist options
  • Save sunnygleason/3512ac02ab018e4c902651982d061e04 to your computer and use it in GitHub Desktop.
Save sunnygleason/3512ac02ab018e4c902651982d061e04 to your computer and use it in GitHub Desktop.
PubNub Sentiment Analysis with IBM Watson BLOCK
const xhr = require('xhr');
const qs = require('codec/query_string');
export default (request) => {
const apiUrl = 'https://gateway-a.watsonplatform.net/calls/text/TextGetTextSentiment';
const apiKey = '0000000000000000000000000000000000000000';
const querystring = qs.stringify({
outputMode: 'json',
showSourceText: false,
text: request.message.text,
apikey: apiKey
});
return xhr.fetch(apiUrl + '?' + querystring)
.then(function (r) {
const body = JSON.parse(r.body);
request.message.sentiment_score = body.docSentiment.score;
request.message.sentiment_type = body.docSentiment.type;
return request.ok();
})
.catch(function (e){
console.error(e);
return request.ok();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment