You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法在同一封邮件中发送Twilio语音邮件音频与转录文本求助

问题:Twilio语音邮件音频与转录文本无法同时正确发送至同一邮件

背景

最初参考Twilio指南实现了语音邮件转发,但需要通过SendGrid发送附带录音附件的邮件,找到相关文章满足了附件需求后,添加转录文本时出现异常。

问题现象

  • 将recordVoicemail组件的语音转录回调URL设置为forwardVoicemail函数地址后,该函数会执行两次:第一次event.TranscriptionText为undefined,第二次文本才正常显示
  • 若移除Studio流程步骤,直接从转录回调URL运行函数,邮件能显示转录文本,但音频文件为空

Studio流程

Twilio 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 12:32:24