App Script – Automated Daily SMS

The idea of this script is to pull data from a website with the UrlFetchApp, slice the html that I wanted to send out as a text message, and them email all my contacts in a particular contact group.

I send SMS through email by using a cool trick I’ll be posting on soon.

function getPostAndEmail() {
  // gets content from home page
  var response = UrlFetchApp.fetch("http://rickhotz.com/").getContentText(); 
  
  // slices out latest post
  var sliceS = response.indexOf('')+18;
  var sliceE = response.indexOf('');
  var post = response.slice(sliceS,sliceE);
  
  // slices out bible verse
  var sliceS = post.indexOf('

')+3; var sliceE = post.indexOf('

'); var verse = post.slice(sliceS,sliceE); // slices out prayer var sliceS = post.lastIndexOf('

')+3; var sliceE = post.lastIndexOf('

'); var prayer = post.slice(sliceS,sliceE); // logs prayer and verse Logger.log(verse + ' ' + prayer); // gets all contacts in 'Daily Prayer' and sends daily message var contacts = ContactsApp.getContactGroup('Daily Prayer').getContacts(); for (var i = 0; i < contacts.length; i++ ) { var addresses = contacts[i].getEmails(); var address = addresses[0].getAddress(); // sends email GmailApp.sendEmail(address, '', verse + ' ' + prayer); } }

This portion of the code automatically joins users to the sms mailing list. I run a few filters on my mail (within Gmail) that automatically tags any message with the phrase #join or #remove and apply the appropriate label. This script runs on hourly interval and grabs the email address from those labels, add the address to my contacts as an anonymous user and removes the label from the email. You can set it up to add a name to the contact, but it was not needed in my scenario.


function joinList() {
  // run once to get threads and thread count
  var threads = GmailApp.getUserLabelByName('join').getThreads();
  var threadCount = GmailApp.getUserLabelByName('join').getUnreadCount();
  // run once to get group
  var group = ContactsApp.findContactGroup('Daily Prayer');
  
  for (var j = 0; j < threadCount; j++) {
    // gets messages in thread
    var messages = threads[j].getMessages();
    
    // marks thread as read
    threads[j].markRead();
    
    // gets first message in thread
    var message = messages[0];

    // gets 'from' field and sliceS out email address    
    var contactEmail = message.getFrom();
    var sliceS = contactEmail.indexOf('');
    var email = contactEmail.slice(sliceS,sliceE);
    
    // creates an array of all the address in 'Daily Prayer' list
    var contacts = ContactsApp.getContactGroup('Daily Prayer').getContacts();
    var addressArray = []; 
    for (var i = 0; i < contacts.length; i++ ) {
      var addresses = contacts[i].getEmails();
      var address = addresses[0].getAddress(); 
      addressArray.push(address);           
    }
    
    // checks array duplicates
    if (addressArray.indexOf(email) == '-1.0') {
      // adds new contact
      ContactsApp.createContact('anon', 'subs', email).addToGroup(group);
    }
  }
}

function removeList() {
  // run once to get threads and thread count
  var threads = GmailApp.getUserLabelByName('remove').getThreads();
  var threadCount = GmailApp.getUserLabelByName('remove').getUnreadCount();
  
  for (var j = 0; j < threadCount; j++) {
    // gets messages in thread
    var messages = threads[j].getMessages();
   
    // marks thread as read
    threads[j].markRead();
    
    // gets first message in thread
    var message = messages[0];
    
    // gets 'from' field and sliceS out email address
    var contactEmail = message.getFrom();
    var sliceS = contactEmail.indexOf('');
    var email = contactEmail.slice(sliceS,sliceE);
    
    // removes contact from group 'Daily Prayer'
    var contacts = ContactsApp.getContactGroup('Daily Prayer').getContacts();
    for (var i = 0; i < contacts.length; i++ ) {
      var addresses = contacts[i].getEmails();
      if (addresses[0].getAddress() == email) {
        contacts[i].deleteContact();
      }
    }
  }
}

Hope this helps someone else out there.

Blessings,
Ryan


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s