0
votes

I am using twilio amd with outbound call. Basically creating a call using callresource to one number. Now i want human detection during the call. Call is picked by human then i need to forward call to my system agent. So for this i am trying to get "AnswerBy" value in "answered" status call back, but "Answerby" is always null. Can you someone let me know how to get the "Answerby" during call in progress and then forward Call. Below is code sample i am using..

Creating Outbound call

        string accountSid = WebConfigurationManager.AppSettings["AccountSid"];
        string authToken = WebConfigurationManager.AppSettings["AuthToken"];
        TwilioClient.Init(accountSid, authToken);
        var statusCallbackEvent = new List<string>();
        statusCallbackEvent.Add("initiated");
        statusCallbackEvent.Add("answered");

        var call = CallResource.Create(
            url: new Uri("http://demo.twilio.com/docs/voice.xml"),
            to: new Twilio.Types.PhoneNumber("+1XXXXXXXXXXXXX"),
            from: new Twilio.Types.PhoneNumber(WebConfigurationManager.AppSettings["FromNumber"]),
            method: Twilio.Http.HttpMethod.Get,
            machineDetection: "Enable",
            statusCallbackEvent: statusCallbackEvent,
            statusCallback: new Uri("https://68456c0d.ngrok.io/TwilioCallback/CallResponse"),
            statusCallbackMethod: Twilio.Http.HttpMethod.Get


        );

        var response = call.Sid;

Answered status callback

string accountSid = WebConfigurationManager.AppSettings["AccountSid"];
        string authToken = WebConfigurationManager.AppSettings["AuthToken"]; ;
        TwilioClient.Init(accountSid, authToken);
        var response = new VoiceResponse();
        var call = CallResource.Fetch(pathSid: obj.CallSid);
        if (call.AnsweredBy?.ToLower() == "human")
        {
            //TO- DO
        }
        else
        {

            response.Say("hello world!", voice: "alice");
            response.Play(new Uri("http://demo.twilio.com/docs/classic.mp3"));
        }
1

1 Answers

0
votes

Twilio developer evangelist here.

If you want to respond when the call connects, you don't want to do so in the status callback. Instead you want to use the URL that you set in the request to create the call.

Currently when you are starting the call, you are still using the demo TwiML response:

    var call = CallResource.Create(
        url: new Uri("http://demo.twilio.com/docs/voice.xml"),

You need to update that url to point to your application. Then when the call connects Twilio will make a request to your application. This request will include all the call parameters, including the response from the answering machine detection. So, instead of making an API call to retrieve the call status, the incoming request body has everything you need. You can then use those parameters to affect your call in your response. For example:

public ActionResult Call(string AnsweredBy) {
  if (AnsweredBy.ToLower() == "human")
  {
     // do something
  }
  else
  {
     // do something else
  }
}

Let me know if that helps at all.