Last active
January 7, 2019 12:44
-
-
Save kindly/9c6105b9489e21fd7548086e98816853 to your computer and use it in GitHub Desktop.
asana gmail integration
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
| var asana_api_key = 'personal_access_token' | |
| var gmail_address = 'mr.raznick@gmail.com' | |
| var asana_project = '922286080685111' | |
| var asana_tag = '930213334200619' | |
| var labels = { | |
| "HIGH": 0, | |
| "MEDIUM": 2, | |
| "LOW": 4 | |
| } | |
| function processEmails() { | |
| Object.keys(labels).forEach(function(label) { | |
| _add_tasks_for_label(label, labels[label]) | |
| }) | |
| } | |
| function _add_tasks_for_label(label, daysDue) { | |
| var labelObject = GmailApp.createLabel(label);; | |
| var processedLabelObject = GmailApp.createLabel(label + "_PROCESSED"); | |
| var threads = labelObject.getThreads(); | |
| for each (var thread in threads) { | |
| var firstMessage = thread.getMessages()[0]; | |
| var name = "Email - " + thread.getFirstMessageSubject(); | |
| var match = firstMessage.getRawContent().match(/\n(Message-ID:)(.*)/i); | |
| if (!match) { | |
| Logger.log("Cant find message id") | |
| Logger.log(firstMessage.getRawContent()) | |
| continue | |
| } | |
| var header = match[2].trim(); | |
| //var notes = 'Gmail Link - https://mail.google.com/mail/u/' + gmail_address + '/#all/' + firstMessage.getId() + '\n\n'; | |
| var notes = 'Gmail Link - https://mail.google.com/mail/u/' + gmail_address + '/#search/' + encodeURIComponent('Rfc822msgid:' + header) + '\n\n'; | |
| notes += 'From - ' + firstMessage.getFrom() + '\n\n'; | |
| notes += firstMessage.getPlainBody() + '\n\n'; | |
| var due_on = new Date(); | |
| due_on.setDate(due_on.getDate() + daysDue); | |
| payload = {"name": name, | |
| "notes": notes, | |
| "projects[0]": asana_project, | |
| "due_on": due_on.toISOString().substring(0,10), | |
| "assignee": "me", | |
| "tags[0]": asana_tag | |
| } | |
| var result = UrlFetchApp.fetch('https://app.asana.com/api/1.0/tasks', { | |
| "headers" : {"Authorization" : "Bearer " + asana_api_key}, | |
| "method" : "post", | |
| "payload": payload | |
| }); | |
| if (result.getResponseCode() === 201) { | |
| thread.removeLabel(labelObject); | |
| thread.addLabel(processedLabelObject); | |
| } | |
| } | |
| } | |
| function list_projects() { | |
| var result = UrlFetchApp.fetch('https://app.asana.com/api/1.0/projects', { | |
| "headers" : {"Authorization" : "Bearer " + asana_api_key}, | |
| }); | |
| Logger.log("Listing Projects") | |
| result_obj = JSON.parse(result.getContentText()) | |
| for each (project in result_obj.data) { | |
| Logger.log(project.gid + ' - ' + project.name) | |
| } | |
| } | |
| function list_tags() { | |
| var result = UrlFetchApp.fetch('https://app.asana.com/api/1.0/tags', { | |
| "headers" : {"Authorization" : "Bearer " + asana_api_key}, | |
| }); | |
| Logger.log("Listing Tags") | |
| result_obj = JSON.parse(result.getContentText()) | |
| for each (project in result_obj.data) { | |
| Logger.log(project.gid + ' - ' + project.name) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment