Listen live calls on Twilio

345 views Asked by At

I'm trying to create an interactive dashboard that shows Twilio calls with the ability to listen to any ongoing calls. Is it possible? And what is the best approach I should take?

I saw two methods on the documentation which are Twiml Voice: and Twiml Voice: . Conference doesn't suit with my scenario because incoming calls are not ringing. And I couldn't find a way to listen to voice Streams on the documentation.

Note: Currently inbound calls are handled by Twilio function and WebSocket and outbound calls are handled by Twilio JavaScript SDK.

Thanks in advance.

1

There are 1 answers

2
jassent On

If you want to listen to a live Voicecall you could try the Stream as IObert suggests. Or, you must use the Conference feature and add yourself as a "Coach." Don't be put off by the name Conference. Think of it as a Voicecall with many more helpful features. You can create a conference from an outbound call like this:

string accountSid = "ACxxxxxx";
string authToken = "xxxxx";

TwilioClient.Init(accountSid, authToken);

var voiceresponse = new VoiceResponse();

var say = new Say("Setting up your conference.");

var dialConference = new Dial();
dialConference.Conference(
        name: "Your Conference"
        startConferenceOnEnter: true,
        endConferenceOnExit: true       
    );

voiceresponse.Append(say);
voiceresponse.Append(dialConference);

var twiml = voiceresponse.ToString();

var call = CallResource.Create(
            twiml: new Twilio.Types.Twiml(twiml),
            to: new Twilio.Types.PhoneNumber("E164 Number to Dial"),
            from: new Twilio.Types.PhoneNumber("E164 Your Twilio Number")           
        );

Then once you have a conference started, you can add the second call from either an inbound or outbound call. With an inbound call, use Twiml to send the call to the conference. With an outbound call, use the REST API to add a Conference Participant.

From the client side, using the Twilio Javascript Client, you would then connect your device to the Conference with the correct Conference Twiml parameters which is explained here:

https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceconnectconnectoptions

const device = new Device(token);

let call = await device.connect({ 
  params: {
    To: 'Your Conference'
  } 
});

I use a slightly different method...I use a button on the client that when you click it makes an outbound call to the "coach" using the Conference Participant REST API. The coach answers and is added with mute = true. The "coach" uses the Twilio Client for WebRTC but the same process will work with a "regular" phone line with a phone number. Using the Twilio Client in this way allows the coach to listen on their computer speakers which is convenient.

The harder part is you will need to keep track of which calls are active on the client side. Otherwise, you won't have an accurate list of which calls or conferences are active. Repeatedly polling the Twilio REST API for active calls is not recommended by Twilio and is super slow compared to using the webhooks to notify of call status changes.