Twilio the evangelist is here.
It looks like this might be a good place to use some <Conference> s .
Allows you to identify the participants in your scenario: Agent1, Agent2, Field.
Let's say that the field calls Agent1. Instead of directly linking the two to <Dial> , you could <Dial> Field in <Conference> (allow ConferenceA call) and then use the REST API to initiate an outgoing call to Agent1. When they respond to <Dial> to the same <Conference> . The system will need to capture CallSid of both Agent1 and Field, as well as Sid from <Conference> , save them in some type of storage, which will be used later.
Using <Conference> in this scenario gives you more options to manipulate each branch of the call regardless of the other than you could if you used <Dial> to connect the field and agent1.
So, Agent2 calls the field. Agent2 would go through the same process, just the opposite. Agent2 is dialed to <Conference> (lets call it ConferenceB), and your system will use the REST API to call the field. When Field answers, they receive <Dial> ed in the same conference as Agent2. Again, the system will need to capture the CallSid of both Agent2 and Field, as well as the Sid from <Conference> , save them in some type of storage, which will be used later.
Now Field needs to be specified to the system in order to associate Agent2 with Agent1. To do this, you can use the <Dial> hangupOnStar attribute in TwiML, which you use Twilio when typing a field in ConferenceB. The verb <Dial> will look something like this:
<Dial hangupOnStar="true" action="[process_hangup_url]"> <Conference>ConferenceB</Conference> </Dial>
hangupOnStar tells Twilio to disconnect the caller (field) from whoever they are <Dial> ed (conference), but still makes a request to the URL specified in the action <Dial> attribute. This is important because when Field needs to tell the system to redirect Agent2 to ConferenceA using Agent1, and requesting the URL attribute in s-action allows the system to invite Field to see if it wants what it wants to do. Thus, Twilio can execute TwiML as follows:
<Response> <Gather action=[gather_handler]> <Say>Press 1 to connect this caller to another<Say> </Gather> </Response>
If the field presses one, the system (who knows all the CallSids for all parties participating here, as well as the conference facilities), can use the REST API to redirect Agent2 from ConferenceB and to ConferenceA.
This complicates the system a bit, but it should work for you.
Hope that helps