Skip to content

Instantly share code, notes, and snippets.

@dhpollack
Last active September 9, 2021 04:56
Show Gist options
  • Save dhpollack/0f47db0d238db166405f to your computer and use it in GitHub Desktop.
Save dhpollack/0f47db0d238db166405f to your computer and use it in GitHub Desktop.
Save Google Voice Voicemails to Google Drive
/* Originally Written by Amit Agarwal amit@labnol.org */
/* Tutorial: http://www.labnol.org/?p=25153 */
/* Edited by David Pollack */
/* https://gist.github.com/dhpollack/0f47db0d238db166405f */
function savevm() {
var folder, folder_name = "vms";
var gmail_label = "gv";
var archive_label = "gv/processed";
/* Find Google Voice messages in Gmail */
var filter = "label:" + gmail_label + " -label:" + archive_label;
var threads = GmailApp.search(filter, 0, 5);
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 */
var archive = GmailApp.getUserLabelByName(archive_label) ?
GmailApp.getUserLabelByName(archive_label) : GmailApp.createLabel(archive_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");
var file_save_name = file_date + " - " + file_name[1] + ".mp3";
if (file_name && !DriveApp.getFilesByName(file_save_name).hasNext()) {
/* 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_save_name);
file.setDescription(msg.getPlainBody());
}
}
}
}
}
@puterboy
Copy link

puterboy commented Sep 9, 2021

This doesn't seem to work anymore... specifically, the trick of looking at "media/svm" just returns an empty file whereas it used to return the voice file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment