Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Last active July 18, 2023 13:00
Show Gist options
  • Save spudtrooper/ca951c44490c7f3c06f8f060255a8669 to your computer and use it in GitHub Desktop.
Save spudtrooper/ca951c44490c7f3c06f8f060255a8669 to your computer and use it in GitHub Desktop.
Appscript to poll your email for a message indicating a check in. Read the comments.
/*
* Purpose:
* To check up on you, in case you have a pet or are travelling.
*
* How it works:
* - You will add a calendar invite every day or more than that
* with a link to send a email to yourself with a sentinel
* string (i.e. `TAG` below).
* - This script will periodically check your inbox for a message
*. with `TAG`, if it doesn't an email will go to a set of
* recipients below, i.e. `ALARM_MESSAGE_EMAIL_ADDRESSES`
*
* Installation:
*. - Fill in the TODOs below
* - Create a trigger to run this script periodically -- say 15 minutes.
* Every time it runs it will check your inbox for a message that has `
* TAG` in it.
* - Create a calendar invite twice a day to email yourself with a
* link like the following:
*
* mailto:<your-email>@gmail.com?subject=<TAG>
*
* So, for Jeff with `TAG` of `STELLA_CHECK` it would be:
*
* mailto:jeffpalm@gmail.com?subject=STELLA_CHECK
*/
// ------------------------------------------------------------------------------------------
// Constants: Configure these values. Each constant's name should describe what it's used for.
// ------------------------------------------------------------------------------------------
/** The number of hours that is acceptable from your last check in. */
const ALARM_THRESHOLD_HOURS = 15;
/** The subject of the email that is sent when a check in fails. */
const ALARM_MESSAGE_SUBJECT = 'Please check on TODO';
/** The body of the email that is sent when a check in fails. */
const ALARM_MESSAGE_TO_SEND = 'Please check on TODO';
/** The recipient addresses of the email that is sent when a check in fails. */
const ALARM_MESSAGE_EMAIL_ADDRESSES = [
'TODO+TODO@gmail.com',
];
const TAG = 'TODO';
// ------------------------------------------------------------------------------------------
// Don't change anything below this.
// ------------------------------------------------------------------------------------------
function checkEmail() {
let threads = GmailApp.search('subject:"' + TAG + '"');
let minDiffHours = ALARM_THRESHOLD_HOURS + 1;
threads.forEach(t => {
let msgs = t.getMessages();
msgs.forEach(m => {
let date = m.getDate(),
diffMillis = Math.abs(new Date() - date),
diffHours = diffMillis / 60 / 60 / 1000;
Logger.log('date: ' + date + 'minDiffHours: ' + minDiffHours + ' diffHours: ' + diffHours);
minDiffHours = Math.min(diffHours, minDiffHours)
});
});
Logger.log('Final: minDiffHours=' + minDiffHours + ' ALARM_THRESHOLD_HOURS:' + ALARM_THRESHOLD_HOURS);
if (minDiffHours > ALARM_THRESHOLD_HOURS) {
Logger.log('raising alarm');
raiseAlarm();
} else {
Logger.log('no alarm');
}
}
function raiseAlarm() {
ALARM_MESSAGE_EMAIL_ADDRESSES.forEach(addr => {
GmailApp.sendEmail(addr, ALARM_MESSAGE_SUBJECT, ALARM_MESSAGE_TO_SEND);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment