Skip to content

Instantly share code, notes, and snippets.

@codemonkey85
Created July 17, 2014 19:41
Show Gist options
  • Save codemonkey85/9867e8db11e4ee2e1f66 to your computer and use it in GitHub Desktop.
Save codemonkey85/9867e8db11e4ee2e1f66 to your computer and use it in GitHub Desktop.
Google Script – Save Voice Mail as MP3 in Google Drive
/* Written by Amit Agarwal amit@labnol.org */
/* Tutorial: http://www.labnol.org/?p=25153 */
var folder, folder_name = "Google Voice";
var archive, gmail_label = "MP3";
/* Find Google Voice messages in Gmail */
var filter = "from:voice-noreply@google.com -label:" + gmail_label;
var threads = GmailApp.search(filter, 0, 10);
if (threads.length) {
/* Google Drive folder where the MP3 files will get stored */
var folders = DriveApp.getFoldersByName(folder_name);
folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folder_name);
/* Gmail Label that is applied to processed voice mails */
archive = GmailApp.getUserLabelByName(gmail_label) ?
GmailApp.getUserLabelByName(gmail_label) : GmailApp.createLabel(gmail_label);
for (var x=0; x<threads.length; x++) {
threads[x].addLabel(archive);
var msg = threads[x].getMessages()[0];
/* Find the link to play the voice mail message */
var url = msg.getBody().match(/https?:\/\/www.google.com\/voice\/fm[^\"]*/gi);
if (url) {
/* Find the name of the voice sender (or their phone number) */
var file_name = msg.getSubject().match(/new voicemail from (.*) at /i);
/* Add the voice mail date to the file name */
var file_date = Utilities.formatDate(
msg.getDate(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm");
if (file_name) {
/* Extract the audio file and save as an MP3 file */
var mp3 = url[0].replace("/voice/fm/", "/voice/media/svm/");
var file = folder.createFile(UrlFetchApp.fetch(mp3).getBlob());
/* Save the voice mail transcript with the audio file */
file.setName(file_name[1] + " [" + file_date + "]" + ".mp3");
file.setDescription(msg.getPlainBody());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment