Search code examples
c#twiliocallforwarding

Call forwarding in Twilio from C#


I am using the latest version of the Twilio Rest API .NET nuget library and I would like to forward an existing voice call. I did find some existing code

[HttpPost]
public async Task<ActionResult> Connect(string from, string to)
{
    var response = new TwilioResponse();
    response.Say("Please wait while we contact the other party");
    response.Dial("+14085993263", new { callerId = "+1234567890" });
    return TwiML(response);
}

But the second parameter to the Dial method seems to no longer work. I also could not see how the above code would work given there are no Twilio authentication details. Does anyone have any example code using the latest nuget library on how to perform call forwarding from C# (It does not have to be a web method) I did find the Twilio example here https://www.twilio.com/docs/voice/tutorials/call-forwarding-csharp-mvc but to be honest this was very little help indeed. Thanks in advance Mark


Solution

  • Twilio developer evangelist here.

    The Dial method has updated to take all the parameters as named parameters. It's also best to create individual Say and Dial objects and use the Append method instead now. So now you need:

    [HttpPost]
    public async Task<ActionResult> Connect(string from, string to)
    {
        var response = new TwilioResponse();
        var say = new Say("Please wait while we contact the other party");
        var dial = new Dial(callerId: "+1234567890");
        var number = new Number("+14085993263");
        dial.Append(number);
        response.Append(say);
        response.Append(dial);
        return TwiML(response);
    }
    

    For more information, check the examples in the TwiML documentation.