Skip to content

Instantly share code, notes, and snippets.

Created October 13, 2017 17:34
Show Gist options
  • Save anonymous/ed18c6a7a34503f038205d5893623a15 to your computer and use it in GitHub Desktop.
Save anonymous/ed18c6a7a34503f038205d5893623a15 to your computer and use it in GitHub Desktop.
Auto respond to share requests
function checkEmail() {
// get all emails that are currently starred
var starredThreads = GmailApp.getStarredThreads();
// edit the auto response below
var email = "dwees@newvisions.org";
var name = "David Wees";
var salutation = "Regards, David Wees, Formative Assessment Specialist for New Visions for Public Schools";
var htmlSalutation = "<p>Regards,</p><p>David Wees<br /> Formative Assessment Specialist for New Visions for Public Schools,</p>";
var subject = "Request to share: ";
var body = "Thank you for your interest in our curriculum project.";
body += "You have requested access to one of our documents. If you did this because you wanted to modify it for your class ";
body += "please feel free to make a copy of the document (File => Make a Copy) and edit it as needed.";
body += "\n\n";
body += "Requested document: ";
body += "\n";
var body2 = "\n\n";
body2 += "If you requested access to this document because you were unable to view it, please email " + email + " and let us know.";
body2 += "\n\n";
body2 += salutation;
var htmlBody = "<p>Thank you for your interest in our curriculum project.</p>";
htmlBody += "<p>You have requested access to one of our documents. If you did this because you wanted to modify it for your class ";
htmlBody += "please feel free to make a copy of the document (File => Make a Copy) and edit it as needed.</p>";
htmlBody += "<p>Requested document: ";
var htmlBody2 = "<p>If you requested access to this document because you were unable to view it, please email " + email + " and let us know.</p>";
htmlBody2 += htmlSalutation;
// loop through each of these starred threads
for (var i = 0; i < starredThreads.length; i++) {
// get the labels for this thread
var labels = starredThreads[i].getLabels();
// loop through the labels on this thread
for (var j = 0; j < labels.length; j++) {
// only look in emails that have the label "requesting-access"
if (labels[j].getName() === "Requesting Access") {
// get the message(s) in this thread. there should be only 1
var message = starredThreads[i].getMessages()[0];
var content = message.getRawContent();
// get the likely id of the requested document from the raw message
// var id = getIdFromUrl(message);
var id = getIdFromMessage(content);
// Logger.log(id);
// from the id, get the url and name of the document
var document = DriveApp.getFileById(id);
var docurl = document.getUrl();
var docname = document.getName();
body += docname + ": " + docurl + body2;
htmlBody += '<a href="' + docurl + '">' + docname + '</a></p>' + htmlBody2;
subject += docname;
var options = {
htmlBody: htmlBody,
replyTo: email,
name: name,
subject: subject
};
// now reply
message.reply(body, options);
// remove the star so we only reply once
message.unstar();
}
}
}
}
function getIdFromMessage(message) {
var url = message.match(/(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/).toString();
return getIdFromUrl(url);
}
function getIdFromUrl(url) {
return url.match(/[-\w]{25,}/).toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment