无法在同一封邮件中发送Twilio语音邮件音频与转录文本求助
问题:Twilio语音邮件音频与转录文本无法同时正确发送至同一邮件
背景
最初参考Twilio指南实现了语音邮件转发,但需要通过SendGrid发送附带录音附件的邮件,找到相关文章满足了附件需求后,添加转录文本时出现异常。
问题现象
- 将recordVoicemail组件的语音转录回调URL设置为forwardVoicemail函数地址后,该函数会执行两次:第一次
event.TranscriptionText为undefined,第二次文本才正常显示 - 若移除Studio流程步骤,直接从转录回调URL运行函数,邮件能显示转录文本,但音频文件为空
Studio流程

函数代码
delayForwardVM
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay)); exports.handler = async (context, event, callback) => { const delay = event.delay || 5000; await sleep(delay); return callback(null, `Timer up: ${delay}ms`); };
forwardVoicemail
// Define Handler function required by all Twilio functions exports.handler = function(context, event, callback) { // Initialize SendGrid mail sending const sgMail = require('@sendgrid/mail'); var request = require('request').defaults({ encoding: null }); // Function to capture and save voicemail as mp3 function doCall(callback) { request.get({ url: event.url + '.mp3', headers: { "Authorization": "Basic " + new Buffer.from(context.ACCOUNT_SID + ":" + context.AUTH_TOKEN).toString("base64") } }, function(error, response, body) { callback(Buffer.from(body).toString('base64')); }); } // Run message capture function doCall(function(response) { // Build SendGrid mail request and define message params sgMail.setApiKey(context.SENDGRID_API_SECRET); const msg = { to: context.TO_EMAIL_ADDRESS, from: context.FROM_EMAIL_ADDRESS, subject: `Afterhours Voicemail from ${event.From}`, text: `See attachment to review the voicemail. Also, here is a transcription:\n\n ${event.TranscriptionText}`, attachments: [{ content: response, filename: "Voicemail.mp3", type: "audio/mpeg", disposition: "attachment" }] }; // Send the email sgMail.send(msg).then(response => { callback(); }); }); }
内容的提问来源于stack exchange,提问作者Bloodlvst
相关产品推荐
相关产品推荐

