Create Google Contacts from Google Voice Mail

Google Voice sends you an email notification where there’s a missed call or a voicemail for you. The email includes the caller’s name, the caller’s phone number and, in case of voicemails, the text transcript of the message. The message also includes a link to download the Google Voice MP3.

The Google Scripts scans all your Google Voice emails, parses the content uses regex and creates a new Google contact. The transcript of the voice message is set to the notes field of the new Google contact.

Link: Save Google Voice Messages to Google Drive

// Search Google Voicemail emails
function searchGoogleVoiceEmails() {
  var threads = GmailApp.search('from:voice-noreply@google.com', 0, 100);

  for (var t = 0; t < threads.length; t++) {
    var response = extractVoicemail_(threads[t].getMessages()[0]);

    if (response) {
      createContact_(response);
    }
  }
}

// Extract the caller's name, phone number and voice message transcription
function extractVoicemail_(msg) {
  var result = {
    'Message Date': msg.getDate(),
    'Message Subject': msg.getSubject(),
    'Message Body': msg
      .getPlainBody()
      .replace(/<[^>]+>/g, '')
      .replace(/\s+/g, ' '),
    Transcription: msg.getPlainBody(),
  };

  var trans = /transcript:(.*)?play message/i.exec(result['Message Body']);

  if (trans) result.Transcription = trans[1];

  //Voicemail from: John Q Public (202) 123-456 at 6:08 PM

  var match = /(Missed Call|Voicemail) from:([*\+\s\w]+)([\d\-\s\(\)\+]*)? at [\d\:\s]+[ap]m/i.exec(
    result['Message Body']
  );

  if (match) {
    result['Call Type'] = match[1];
    result['Contact Name'] = match[2].replace(/^+/, '');
    result['Contact Number'] = match[3].replace(/^+/, '');
  } else {
    return null;
  }

  return result;
}

// Create a new Google contact from Voicemail
function createContact_(result) {
  var contacts = ContactsApp.getContactsByPhone(result['Contact Number'], ContactsApp.Field.WORK_PHONE);

  if (contacts.length > 0) {
    return 'Contact Exists';
  } else {
    var contact = ContactsApp.createContact(result['Contact Name'], result['Contact Name'], result['Call Type']);

    contact.addPhone(ContactsApp.Field.WORK_PHONE, result['Contact Number']);
    contact.setNotes(result['Transcription']);

    return contact.getId();
  }
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻