Craig
Craig

Reputation: 36816

Twilio record voicemail after waiting

I am trying to implement a voicemail but for some reason it hangs up immediately after 10 seconds and doesn't play the message. So far I have

[HttpPost]
public ActionResult Connect(string phoneNumber, string called)
{
    var response = new VoiceResponse();

    var dial = new Dial(callerId: _credentials.PhoneNumber, timeout: 10);
    if (phoneNumber != null)
    {
        dial.Number(phoneNumber);
    }
    else
    {
        int agentId = 1;
        dial.Client("support_agent", statusCallback: Url.Action("Status", "Call", new { Area = "PhoneSystem", agentId }, Request.Url.Scheme));
    }
    response.Dial(dial);

    return TwiML(response);
}

[HttpPost]
public ActionResult Status(string agentId, string dialCallStatus, string callStatus)
{
    // Need to pass the agent so we know who is called
    if (dialCallStatus == "completed")
    {
        var emptyResponse = new XDocument(new XElement("Root", ""));
        return new TwiMLResult(emptyResponse);
    }

    var response = new VoiceResponse();

    response.Say(
        body: "We can't answer your call right now, please leave a message" 
    );

    // Use <Record> to record the caller's message
    response.Record();

    // End the call with <Hangup>
    response.Hangup();

    return TwiML(response);
}

It does get to the Status method I can confirm, but the voice is never played.

Edit. I have changed the code to this

[HttpPost]
public ActionResult Connect(string phoneNumber, string called)
{
    var response = new VoiceResponse();

    int agentId = 1;
    var action = Url.Action("Status", "Call", new { Area = "PhoneSystem", 
      agentId }, Request.Url.Scheme);

    var dial = new Dial(callerId: _credentials.PhoneNumber, timeout: 10, action: new Uri(action));

    if (phoneNumber != null)
    {
        dial.Number(phoneNumber);
    }
    else
    {           
        dial.Client("support_agent");
    }
    response.Dial(dial);

    return TwiML(response);
}

Upvotes: 0

Views: 153

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

The statusCallback on the <Client> is to be used to get asynchronous callbacks about the state of the call. It won't allow you to continue the call.

Instead, you should use an action attribute on the <Dial> to set what the call should do once the dialled call is complete.

Let me know if that helps at all.

Upvotes: 1

Related Questions