Skip to content

Instantly share code, notes, and snippets.

@bencollerson
Created October 10, 2012 03:06
Show Gist options
  • Save bencollerson/3862934 to your computer and use it in GitHub Desktop.
Save bencollerson/3862934 to your computer and use it in GitHub Desktop.
Google apps script to create an sms notification from a email tagged in gmail.
// Copyleft (ɔ) Ben Collerson {benc::benc::cc}
// Last modified 2012-10-10 14:17+10
// This code is based on this Google Apps scripting tutorial written by Romain Vialard.
// https://developers.google.com/apps-script/articles/gmail_filter_sms
// Notes on getting this code to work..
//
// In addition to following the steps from the link above the following steps must be done:
//
// 1. I used the label 'sms2me' instead of 'Send text' (I don't like spaces in labels)
// 2. To stop my default calendar getting clogged with fake SMS events I use a different
// calendar called 'sms2me'
// 3. I also use a label 'sms2me-sent' to archive old messages.
function sms2me() {
// Get gmail label that triggers SMS
var trigger_label = GmailApp.getUserLabelByName('sms2me');
// Get all threads tagged with 'sms2me'
var threads = trigger_label.getThreads();
// Current time for reference.
var now = new Date().getTime();
// Special hidden sms calendar, id is available through the calendar site.
var calendar = CalendarApp.getCalendarsByName('sms2me')[0];
for(i in threads){
// Insert text to be sent as SMS into title of a new event.
var event = calendar.createEvent(
threads[i].getFirstMessageSubject(),
new Date(now+60000),
new Date(now+60000)
);
// Apparently subsequent updates need to be done to series rather than just the event.
// Get the series of the event just created.
var event_series = calendar.getEventSeriesById(event.getId());
// Add the SMS reminder.
event_series.addSmsReminder(0);
// Put the processed message in the archive.
threads[i].moveToArchive();
}
// Remove the trigger label from the threads just processed.
trigger_label.removeFromThreads(threads);
// Once the SMS is sent, the trigger_label is removed, the thread is archived,
// label the treads as sent.
sent_label = GmailApp.getUserLabelByName('sms2me-sent');
sent_label.addToThreads(threads);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment