Skip to content

Instantly share code, notes, and snippets.

@rominirani
Created May 31, 2018 08:09
Show Gist options
  • Save rominirani/1c257f009d2777ed41740761b6de1f10 to your computer and use it in GitHub Desktop.
Save rominirani/1c257f009d2777ed41740761b6de1f10 to your computer and use it in GitHub Desktop.
Apps Script method to invoke the Cloud Natural Language API
function retrieveSentimentCF (line) {
var apiEndpoint = 'YOUR_CLOUDFUNCTIONS_HTTPS_ENDPOINT';
var reviewData = {
review_text: line
};
var cfCallOptions = {
method : 'post',
contentType: 'application/json',
payload : JSON.stringify(reviewData)
};
// Invoke the Cloud Function
var response = UrlFetchApp.fetch(apiEndpoint, cfCallOptions);
var data = JSON.parse(response);
var sentiment = 0.0;
if (data && data.score && data.magnitude) {
sentiment = data.score;
}
return sentiment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment