Created
August 22, 2021 08:04
-
-
Save emwdx/4f7f715edc8c736da60c732d76f2c8aa to your computer and use it in GitHub Desktop.
ooo_autoreply_modular
This file contains 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
// Out of Office Autoreply - Google Script code by Evan Weinberg (Twitter: @emwdx) | |
// This code was based on the code for the google script autoreply post here on stack overflow: https://stackoverflow.com/questions/38955161/google-gmail-script-time-auto-reply | |
// Define the variables we want to be available over our entire program here: | |
var interval; | |
var daysOff; | |
var weekdayMessageDays; | |
var workingHours; | |
function setGlobalVariables(){ | |
interval = 5; // Set to the number of minutes between runs of this script. You set this also in the trigger settings through the stop-watch tab. | |
daysOff = [0, 6]; // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su | |
weekdayMessageDays = [0, 1, 2, 3, 4] | |
workingHours = [5,17]; // 0-24 | |
} | |
function getDate(){ | |
// A Date object has a bunch of information stored within it beyond just the date. | |
// It has lots of information about the current moment: date, time (hour, minute, second), day of the week, and some other useful things. | |
// When we create a new Date(), it asks the computer running the code to ask its internal clock for all this information. | |
return new Date(); | |
} | |
function getDay(date){ | |
// This function takes a date object as an input. | |
// This function returns a value. Specifically it gets the index of the day from the date object. 0 is Sunday, 1 is Monday, and so on. | |
// Since this function returns a value, it has an output. | |
return date.getDay(); | |
} | |
function getHour(date){ | |
return date.getHours(); | |
} | |
function isThisDayAWorkday(day){ | |
// An array can search itself for a particular value. | |
// If the element is in the array, the array.indexOf function will return the number slot (called the index) that the element is located in the array. | |
// If the array does not contain the value, .indexOf will return -1 to say that the element can't be found inside the array. | |
if(daysOff.indexOf(day) == -1){ | |
//If today's day value isn't in the daysOff array, it means today isn't a day off. This means that today is a workday, and this function should return true. | |
return true | |
} | |
// If today is in the daysOff array, today is a day off. This function should return false. | |
else{ | |
return false | |
} | |
} | |
function isOutsideWorkHours(currentHour){ | |
var isBeforeStartTime = (currentHour < workingHours[0]) | |
var isAfterEndTime = (currentHour >= workingHours[1]) | |
if (isBeforeStartTime || isBeforeStartTime){ | |
return true | |
} | |
else{ | |
return false | |
} | |
} | |
// This function has two inputs and one output! We can write a function that has multiple inputs. | |
// Most functions have a single variable as an output, but that variable can be almost anything. It is possible to store multiple bits of information in that variable. | |
function shouldSendAutoReply(notWorkday, isOutsideHours){ | |
if(notWorkday || isOutsideHours){ | |
return true | |
} | |
else{ | |
return false | |
} | |
} | |
function generateAutoreplyMessageText(day, hour){ | |
// The next lines set different messages for different situations. We do this inside this function because this function has the responsibility of generating the message to be sent out. | |
// It is good practice to define variables close to where they are used. This is called managing scope. | |
var weekdayMessage = "***This was sent by a robot!*** \n\n Hello, \n\n Thank you for your message. To maintain work/life balance, I am responding to email during school hours which are from 7:00 AM to 3:30 PM during the week. I will get back to you after 7:00 AM tomorrow. \n\n Thanks for understanding, \n\n EMW "; | |
var weekendMessage = "***This was sent by a robot!*** \n\n Hello, \n\n Thank you for your message. To maintain work/life balance, I am responding to email during school hours which are from 7:00 AM to 3:30 PM during the week. I will get back to you as soon as I can after 7:00 AM on Monday. \n\n Thanks for understanding, \n\n EMW "; | |
var mondayMessage = "***This was sent by a robot!*** \n\n Hello, \n\n Thank you for your message. To maintain work/life balance, I am responding to email during school hours which are from 7:00 AM to 3:30 PM during the week. I will get back to you as soon as I can later today. \n\n Thanks for understanding, \n\n EMW " | |
var sentMessage = "" | |
// Giving a blank initial value to the variable that is returned can be really helpful for debugging. | |
// If the fucntion returns this blank value, after all of your logic, it means that the logic is incorrect. | |
// This usually means you haven't accounted for all of the possible input values that could occur when your program runs. | |
//You can send two different messages for weekdays and weekends. | |
if(weekdayMessageDays.indexOf(day)!=-1){ | |
//Send the weekend message if it is Friday after the end of working hours. | |
if(day == 5 && hour >= workingHours[1]){ | |
sentMessage = weekendMessage | |
} | |
if(day == 1 && hour < workingHours[0]){ | |
sentMessage = mondayMessage | |
} | |
//Otherwise send the weekday message. | |
else{ | |
sentMessage = weekdayMessage | |
} | |
} | |
else{ | |
sentMessage = weekendMessage | |
} | |
// Returns the sent message variable | |
return sentMessage | |
} | |
function getStartTimeForUnreadSearch(date,interval){ | |
// The current time in seconds comes from rounding the value of the date (which is the number of milliseconds since January 1, 1970) divided by 1000. | |
// Can you figure out why we want to divide by 1000? | |
var currentTime = Math.floor(date.valueOf()/1000) | |
// When this function runs, we want to search for all messages that have come in over the past 'interval' minutes. | |
// To get this, we subtract 60 * interval to get the time on the clock when we want to start our search. | |
return currentTime - 60 * interval; | |
} | |
function getUnreadThreadsFromGmail(timeFrom){ | |
// This asks Gmail to find any threads that are unread and that have come in after 'interval' minutes have passed. | |
return GmailApp.search('is:inbox after:' + timeFrom); | |
} | |
function emailIsFromWork(thread){ | |
// There is some Javascript string magic happening here. This is my hack for getting the part of the message that contains the email address. | |
var senderEmailDomain = thread.getMessages()[0].getFrom().split("@")[1] | |
// Now we want to check if the email contains text from the work domain. This is done by calling another function inside this one. | |
var isFromWork = isEmailDomainWork(senderEmailDomain) | |
return isFromWork | |
} | |
function isEmailDomainWork(domainString){ | |
// This is how we check to see if the domain is from work. Notice that this is the SSIS email domain. | |
// If the text in the domainString contains the domain, the isFromDomain variable will be true. Otherwise it will be false. | |
var isFromDomain = /ssis.edu.vn*/.test(domainString) | |
return isFromDomain | |
} | |
function checkThreadsForWorkEmails(threads,messageToBeSent){ | |
// This is a for loop. It will repeat the code inside the braces {} a specific number of times. | |
// To be specific, this will repeat the code inside the braces for the number of unread threads that Gmail found in its search. | |
for (var i = 0; i < threads.length; i++) { | |
// Store each unread thread, one at a time, in a variable called currentThread. | |
var currentThread = threads[i] | |
// If the email is from work, send the reply message. | |
if (emailIsFromWork(currentThread)){ | |
currentThread.reply(messageToBeSent) | |
} | |
} | |
} | |
function autoReply() { | |
setGlobalVariables() | |
var date = getDate() | |
var day = getDay(date) | |
var hour = getHour(date) | |
var isWorkday = isThisDayAWorkday(day) | |
var isOutsideHours = isOutsideWorkHours(hour) | |
// The NOT operator ! switches a Boolean variable value of true to false and a false value to true. This is the easy way to check for a variable NOT being true. | |
// In the line below, we want to send an auto reply if it is not a workday. | |
var shouldSendAutoreply = shouldSendAutoReply(!isWorkday,isOutsideHours) | |
if (shouldSendAutoreply) { | |
var messageToBeSent = generateAutoreplyMessageText(day, hour) | |
var timeFrom = getStartTimeForUnreadSearch(date,interval) | |
var threads = getUnreadThreadsFromGmail(timeFrom) | |
checkThreadsForWorkEmails(threads,messageToBeSent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment