Sending call to multiple clients via conference participants REST API

183 Views Asked by At

I am trying to switch our old call flow to the conference and I have ported most of the features to the conference. I am trying to handle incoming calls in cases where I have to ring to multiple agents and disconnect the call after one of them.

Right now I am doing something along the lines of::

Cutomer calls :: call is sent to webhook -> Get list of all active agents and send the twiml


<?xml version="1.0" encoding="UTF-8"?>
<Response>
   <Dial>
     <Client>
        <Identity>jane</Identity>
        <Parameter name="FirstName" value ="Jane"/>
        <Parameter name="LastName" value ="Doe" />
      </Client>
     <Client>
        <IdentityHarry</Identity>
        <Parameter name="FirstName" value ="Jane"/>
        <Parameter name="LastName" value ="Doe" />
      </Client>
    </Dial>
</Response>

This way I can ring both ( or more) clients and call drops after the first agent answers.

How I achieve something like this with conference? What I want is:: ->

Customer calls:: call is captured into webhook =>
 customer is dropped into the conference call with some music.
- Dial to clients :: (harry, jane) and connect them to the call.

Is there a way I can do this with via rest api to conference participants api?

1

There are 1 best solutions below

0
On

Yes, you can do this with the REST API. Your webhook should respond with:

<Response>
  <Dial>
    <Conference waitUrl="YOUR_HOLDING_MUSIC">YOUR_ROOM_NAME</Conference>
  </Dial>
</Response>

The only thing about using the REST API is that it doesn't perform the same as multiple <Client> elements. That is, when someone answers the phone, you have to cancel all the other calls. To do this, you will need to store the call SIDs that are created when you call the API, use status callbacks to know when a participant has connected, and then when you get the first status callback, cancel the other calls. So, something like this to make the calls and get the call sids:

def call_conference(client_name):
    client = Client(account_sid, auth_token)
    participant = client.conferences('YOUR_ROOM_NAME') \
                    .participants \
                    .create(
                         status_callback='https://myapp.com/events',
                         status_callback_event=['answered'],
                         from_='YOUR_TWILIO_NUMBER',
                         to='client:' + client_name
                     )
    return participant.sid

clients = ["jane", "harry"]
participants = map(call_conference, clients)

Then, when that status callback event fires, filter the call sid from the list of sids you have and cancel the other calls.