在此处添加一些背景信息:
我有一个 Google Apps 脚本,当我在 Twilio Studio 中收到语音邮件时,它会通过电子邮件向我发送 Twilio 录音。基本上,在我提示呼叫者录制语音邮件后,我将
Http Get request
运行到我的 Google Web 应用程序,其中包含一些有关通话的参数,包括 recording url
,我想登录电子表格并通过电子邮件发送给我,这是一个基本的语音邮件 -发送电子邮件的想法。
当我尝试使用
UrlFetchApp
获取 google apps 脚本中的录制资源以添加为电子邮件中的附件时,出现以下错误 The requested resource /2010-04-01/Accounts/ACxxxxxxxxx/Recordings/RExxxxxxxxx was not found
。
有趣的是,当我将 Twilio
recording url
硬编码到函数中时它可以工作,但是当 recording url
作为参数传入时,它会收到此错误。我在出现错误之前将 url 记录到电子表格中,并且出现了 recording url
。
如有任何帮助,我们将不胜感激。
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])
}
}
在@Tanaike和@TheMaster的评论的帮助下,我发现找不到
recording resource
的原因是因为录制后速度太快了。
一个简单的修复方法是在脚本开头添加 2 秒的暂停,如下所示:
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])
}
}