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

Twilio Voice API需求:客户未接转语音信箱时播放预录音频

Solution for Adding Voicemail Greeting to Unanswered Twilio Calls

Hey there! Let's get this voicemail greeting sorted out for your Twilio call setup. You're already halfway there with your existing code that handles successful calls—now we just need to add logic to play a pre-recorded message when the customer doesn't answer or the call routes to voicemail.

Step 1: Update Your Initial Dial Configuration

First, tweak your Dial object to include key parameters that help Twilio notify your endpoint about the call outcome, and set a timeout for how long to wait for an answer:

var response = new VoiceResponse();
// Add timeout and enable machine/voicemail detection
var dial = new Dial(
    callerId: callerId,
    action: "https://ba2d9819.ngrok.io/voice/CallEnd",
    timeout: 30, // Wait 30 seconds for the customer to answer
    answeredBy: "human" // Detect if the call is picked up by a machine
);

if (Regex.IsMatch(to, "^[\d\+\-\(\) ]+$")) {
    dial.Number(to);
} else {
    dial.Client(to);
}

response.Dial(dial);

Step 2: Handle the Call Outcome in Your Action Endpoint

Twilio will send a POST request to your CallEnd endpoint once the Dial completes. We'll check two key parameters here to trigger the voicemail greeting:

  • CallStatus: Will be no-answer if the call timed out without being answered
  • AnsweredBy: Will be machine if Twilio detects the call was picked up by voicemail or an answering machine

Here's how to implement the logic in your CallEnd endpoint:

[HttpPost("/voice/CallEnd")]
public IActionResult CallEnd()
{
    var callStatus = Request.Form["CallStatus"];
    var answeredBy = Request.Form["AnsweredBy"];
    var response = new VoiceResponse();

    // Trigger the greeting if the call wasn't answered OR was picked up by a machine
    if (callStatus.Equals("no-answer", StringComparison.OrdinalIgnoreCase) || 
        answeredBy.Equals("machine", StringComparison.OrdinalIgnoreCase))
    {
        // Replace this URL with your pre-recorded audio file (MP3/WAV format)
        response.Play(new Uri("https://your-audio-host.com/voicemail-greeting.mp3"));
        
        // Optional: Add a recording step if you want customers to leave a message
        // response.Record(maxLength: 60, finishOnKey: "#", action: "/voice/SaveVoicemail");
    }

    return Content(response.ToString(), "application/xml");
}

Key Notes to Remember

  • Audio File Requirements: Ensure your pre-recorded greeting uses a Twilio-supported format (MP3, WAV) and is hosted at a publicly accessible URL (Twilio Assets is a great free option for hosting these files).
  • Debugging: Use Twilio's Console → Monitor → Events to verify that the CallEnd webhook is being called with the correct parameters if things aren't working as expected.
  • Timeout Adjustment: The timeout value can be adjusted based on how long you want to wait for the customer to answer before routing to the greeting.

内容的提问来源于stack exchange,提问作者Milind Anantwar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:03:06