Created
September 13, 2017 12:48
-
-
Save JEverhart383/eb58b64d99bdd3c098378e60867f5da3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myFunction() { | |
//Get a particular label used in Gmail | |
var label = GmailApp.getUserLabelByName('wordpress'); | |
//Get threads within tagged with this label | |
var threads = label.getThreads(); | |
//Loop through threads and get messages | |
threads.forEach(function(thread){ | |
var messages = thread.getMessages(); | |
messages.forEach(function(message){ | |
//Start collecting text for analysis and | |
//anything else you want logged to the SS | |
var subject = message.getSubject(); | |
var body = message.getPlainBody(); | |
//This is the payload format for the Azure Text Analytics service | |
//Could easily send more than one document, upper limit might be 100 or something | |
var payload = { | |
"documents": [ | |
{ | |
"language": "en", | |
"id": "1", | |
"text": body | |
} | |
] | |
}; | |
//Create params for UrlFetch, note the custom headers which Azure needs | |
//Replace Ocp-Apim-Subscription-Key with your key | |
var params = { | |
"method": "post", | |
"payload": JSON.stringify(payload), | |
"headers": { | |
"Ocp-Apim-Subscription-Key": "yourkeygoeshere", | |
"Content-Type": "application/json", | |
"Accept": "application/json" | |
} | |
} | |
//Make synchronus requests to API. It would be nice if there were on endpoint and different flags or options, but right now | |
//you have to do a request for each thing you want analyzed | |
//**Your endpoint may be different. You get this through Azure | |
var phrases = UrlFetchApp.fetch('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases', params); | |
var sentiment = UrlFetchApp.fetch('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment', params); | |
//Parse responses from JSON string and extract values | |
var sentimentScore = JSON.parse(sentiment).documents[0].score; | |
var keyPhrases = JSON.parse(phrases).documents[0].keyPhrases; | |
var phraseString = keyPhrases.join(", "); | |
Logger.log(keyPhrases); | |
//Dump everything into a new row of the SS | |
var contents = [subject, body, sentimentScore, phraseString ]; | |
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().appendRow(contents) | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment