79179852

Date: 2024-11-12 05:10:08
Score: 0.5
Natty:
Report link

After some help from the comments from @Tanaike and @TheMaster I discovered that it seems Twilio needs another 2 seconds for the resource to be available from its servers and the recording resource wasn't found since the script ran immediately after the recording was made.

A simple fix was adding a 2 second pause to the beginning of the script like this:

function doGet(e){

Utilities.sleep(2000); // This 2 second sleep solved the issue

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('vm');

try{
var p = e.parameter;
var from = p.from;
var received = p.received;
var u = p.recording;
var url = String(u);
var length = p.length;
var email = p.email;
var ext = p.ext;
var fromDashes = from.slice(-10,-7)+"-"+from.slice(-7,-4)+"-"+from.slice(-4);
var receivedDashes = received.slice(-10,-7)+"-"+received.slice(-7,-4)+"-"+received.slice(-4);

  //time script ran
const d = new Date();
const t = d.toLocaleString();
const split = t.split(',');
const time = split[1];

var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();

var theDate = curr_month + "-" + curr_date + "-" + curr_year;

ss.appendRow([theDate,time,receivedDashes,fromDashes,length,ext,email,url]); // this logs the full url

var fileName = "VM_" + fromDashes + "_" + theDate

var blob = UrlFetchApp.fetch(url).getBlob().getBytes();

blob = Utilities.newBlob(blob, 'audio/x-wav', fileName);

var message = {
  to: email,
  subject: "[New] Voicemail for "+ext,
  htmlBody: "<h3>You received a new voicemail from: " + fromDashes + "</h3>For: "+ext+"<br><br>Recieved at: "+receivedDashes+"<br>Length: "+length+"<br><br><a href=" + url + ">Listen to Voicemail</a>", //<br><br><a href=" + callBackUrl + ">Call "+fromDashes+"</a>",
  attachments: [blob]
};

MailApp.sendEmail(message);

}
catch(e){
  ss.appendRow([e])
}
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Tanaike
  • User mentioned (0): @TheMaster
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: jack