From 3efa032e32c5eb2ce325e7141f431b5eae32cee0 Mon Sep 17 00:00:00 2001 From: Daniel Wood Date: Sat, 5 Sep 2020 16:17:09 -0700 Subject: [PATCH] Create GVoice2Drive.js --- GVoice2Drive.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 GVoice2Drive.js diff --git a/GVoice2Drive.js b/GVoice2Drive.js new file mode 100644 index 0000000..8aaf496 --- /dev/null +++ b/GVoice2Drive.js @@ -0,0 +1,118 @@ +function GVoice2Drive() { + // User Adjustable Variables + var fileNamePrefix = "Voicemail_"; // Prefix applies to all MP3 files + var folder, folder_name = "Google Voice"; // Folder or path in Google Drive where MP3s are saved + var archive, gmail_label = "GV2Drive"; // Gmail label applied to processed voicemails + var enableDebugging = false; // Enables extra logging info for debugging purposes + + // Function to set file modified date to custom value + function DriveAPI_setFileModifiedDate(newModifiedTime, fileId) { + var url = "https://www.googleapis.com/drive/v3/files/" + fileId; + var params = { + method: "patch", + headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() }, + payload: JSON.stringify({ modifiedTime: newModifiedTime }), + contentType: "application/json", + }; + UrlFetchApp.fetch(url, params); + } + + // Create CRON trigger if none exists. + if (ScriptApp.getProjectTriggers()[0] == null) { + // Trigger every 5 minutes. + let timerMinutes = 5; + Logger.log('No Triggers found for this project, creating default %s minute timer.', timerMinutes); + // If you change the function name from GVoice2Drive, change below to catch the trigger. + ScriptApp.newTrigger('GVoice2Drive') + .timeBased() + .everyMinutes(timerMinutes) + .create(); + } + + // Find unprocessed Google Voice messages in Gmail + var filter = "from:voice-noreply@google.com -label:" + gmail_label; + + // Process up to 10 emails per call. + var threads = GmailApp.search(filter, 0, 10); + if (threads.length) { + + // Google Drive folder where the MP3 files will be saved + let folders = DriveApp.getFoldersByName(folder_name); + let 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++) { + + var msg = threads[x].getMessages()[0]; + + /* Find the link to play the voice mail message */ + regexp = /https\:\/\/www.google.com\/voice\/fm[^\"]*/; + var url = msg.getBody().match(regexp); + + if (url) { + Logger.log('URL: %s', url); + Logger.log('SUBJ: %s', msg.getSubject()); + + // Extract the name or number of the voice sender + regexp = /new voicemail from (.*)/i; + var subject = msg.getSubject().match(regexp); + + // Add the voice mail datetime to the file name + var file_date = Utilities.formatDate( + msg.getDate(), Session.getScriptTimeZone(), "yyyyMMddHHmm"); + + if (subject) { + // Strip non-alphanumerics + regexp = /[^0-9a-z]/gi; + let callerID = subject[1].replace(regexp, ''); + Logger.log('CallerID: %s', callerID); + + + // Extract the audio url and save it to Drive + var mp3 = url[0].replace("/voice/fm/", "/voice/media/svm/"); + var file = folder.createFile(UrlFetchApp.fetch(mp3).getBlob()); + + // Ignore "https://voice.google.com>", capture any text, ignore "play message\r\n\https://www.google.com/voice/fm" + regexp = /(?:\[\s]{2})([\s\S]*)(?:play\ message[\s]{1,2}\