javascriptgoogle-apps-scripttwiliohttp-status-code-404

Twilio Recording Fetch returns not found


ADDING SOME BACKGROUND INFO HERE:

I have a google apps script that emails me a Twilio recording when I receive a voicemail in Twilio Studio. Basically after I prompt the caller to record a voicemail I run Http Get request to my Google Web App with a few parameters about the call including the recording url which I would like to log on a spreadsheet and email to me, a basic voicemail-to-Email idea.

When I'm trying to fetch the recording resource in google apps script with UrlFetchApp to add as an attachment in the email I get the following error The requested resource /2010-04-01/Accounts/ACxxxxxxxxx/Recordings/RExxxxxxxxx was not found.

What's interesting is that when I hardcode the Twilio recording url into the function it works, but when the recording url is passed in as a parameter it gets this error. I logged the url to the spreadsheet before the point of the error and the recording url comes through.

Any help is appreciated.

function doGet(e){

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])
}
}


Solution

  • 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])
    }
    }