Search code examples
twiliotwilio-api

Trying to delay a second call until the first is answered, confused by results from "statusCallback"


Note: The code below works correctly. The cause of the behavior I was experiencing was because the agent phone number is a Twilio number that is immediately answered (transitioning the call status to In-progress) and forwarded to a SIP address. Once I put the SIP address directly in for the agent, the script works as expected.

I'm leaving this post here in case someone else has a similar issue where another process is causing unexpected behavior elsewhere in the code.

I'm working on a process that will accomplish the following:

  1. Make an outbound (from Twilio) call to an agent and add the agent to a conference.
  2. Wait for the agent to answer the call.
  3. Deliver a "hint" message to the agent about the customer to be added to the conference.
  4. Make an outbound call to the customer, and add the customer to the conference.

The C# .NET code I have, which works for steps 1 and 4, is:

// Initiate First Call

var call = CallResource.Create(
  to: new Twilio.Types.PhoneNumber( "+" + agent ),
  from: new Twilio.Types.PhoneNumber( "+" + callerid ),
  callerId: callerid,
  twiml: new Twilio.Types.Twiml(
    "<Response><Dial answerOnBridge='true' hangupOnStar='true'>"
    + "<Conference startConferenceOnEnter='true' beep='false'>"
    + agent+member + "</Conference>"
    + "</Dial></Response>" ),
  statusCallback: new Uri( "https://callrouting-2700.twil.io/conference" ),
  statusCallbackEvent: new List<string> { "initiated","ringing","answered","completed" },
  statusCallbackMethod: Twilio.Http.HttpMethod.Get
);

// Initiate Second Call

call = CallResource.Create(
  to: new Twilio.Types.PhoneNumber( "+" + member ),
  from: new Twilio.Types.PhoneNumber( "+" + callerid ),
  callerId: callerid,
  twiml: new Twilio.Types.Twiml(
    "<Response><Dial answerOnBridge='true'>"
    + "<Conference beep='false'>" + agent+member + "</Conference>"
    + "</Dial></Response>" )
);

To implement steps 2 and 3 above, I assumed that I could move the second call initiation to a separate function called by the statusCallback URL. In this script, I would check the CallStatus and add the second call when the first call's status changes to In-progress.

The problem I'm having is that the call status of the first call quickly moves from Initiated to Ringing to In-progress even before the agent answers the call.

2023-02-02 19:58:29 UTC Execution started...
2023-02-02 19:58:29 UTC Initiated
2023-02-02 19:58:29 UTC Execution ended in 60.63ms using 98MB
2023-02-02 19:58:29 UTC Execution started...
2023-02-02 19:58:29 UTC Ringing
2023-02-02 19:58:29 UTC Execution ended in 2.46ms using 102MB
2023-02-02 19:58:30 UTC Execution started...
2023-02-02 19:58:30 UTC In-progress
2023-02-02 19:58:30 UTC Execution ended in 2.45ms using 102MB

I assume the issue is that I can't add answerOnBridge='true' to the CallResource.Create, which will delay the In-progress status until the call is answered.

Is there a solution to delaying the second call until the first is answered?


Solution

  • See the note at the beginning of the post for resolution.