0

I am using Twilio JS SDKs in my Angular application to call my customers. I have a requirement to show the status of the call on the UI(Ringing/Inprogress/completed). For that I have seen some documents to use StatusCallBack web hooks, but how will I be notified in the UI if the web hook gets executed and return the TwiML. Please suggest if any alternatives.

This is my Angular code:

    this.device.connect(function (connection) {
      connection.on('warning', function (warningName, warningData) {
        // alert(warningName);
        if (warningName == "low-bytes-received" || warningName == "low-bytes-sent") {
          myscope.NetworkIssue();
        }
      });
      connection.on('ringing', function (param1) {
        // alert(warningName);
        debugger;
      });
      connection.on('accept', function (param2) {
        // alert(warningName);
        debugger;
      });
});

This is my TWI ML App code:

var response = new VoiceResponse();
            var dial = new Dial(callerId: twilioNumber);
            if (phoneNumber != null)
            {
                dial.Number(phoneNumber);
                dial.AnswerOnBridge = true;
            }
            response.Append(dial);
            return Content(response.ToString(), "application/xml");
Aditya
  • 31
  • 6

1 Answers1

0

Twilio developer evangelist here.

You can actually get the status of the call including the difference between ringing and in progress now.

You can see the events you can receive in the documentation here. To enable the ringing state, you need to include the enableRingingState option when you setup the device:

Twilio.Device.setup(token, { enableRingingState: true });

You also need to ensure that you set the answerOnBridge attribute to true in your <Dial>.

When you have that setup, once you make an outbound call you will receive the events "ringing", "accept" and then "disconnect" to correspond to your ringing/in progress/completed requirements.

Edit

In your TwiML you are setting answerOnBridge incorrectly. You need to pass it as part of an options object to the Dial method, along with the callerID. Like this:

var response = new VoiceResponse();
var dial = response.dial({
  callerId: twilioNumber,
  answerOnBridge: true
});
if (phoneNumber != null) {
  dial.Number(phoneNumber);
}
return Content(response.ToString(), "application/xml");

Edit 2

You are using the callback version of device.connect which is deprecated. But it is also too late to setup your event handlers on the connection object. Instead, when you call device.connect to place the outgoing call, you will get a connection object returned and you should setup the listeners then.

To avoid confusion, you should also use device.on('connect', handler) to listen for connection events.

Something like this:

var connection = this.device.connect(connectOptions);

connection.on('warning', function (warningName, warningData) {
  // alert(warningName);
  if (warningName == "low-bytes-received" || warningName == "low-bytes-sent") {
    myscope.NetworkIssue();
  }
});
connection.on('ringing', function (param1) {
  // alert(warningName);
  debugger;
});
connection.on('accept', function (param2) {
  // alert(warningName);
  debugger;
});

this.device.on("connect", function() {
  console.log("Now connected");
})
philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks for your reply. I have tried what you suggested ,but in outgoing call case device.connect event is getting triggered only after person lifts the call. And ringing ,accept events are not getting triggered. Can you please suggest on this. – Aditya Apr 23 '21 at 07:35
  • I see your comment in the answer. When you set up your access token, you provide a TwiML application, which includes a webhook URL for voice calls. In that application is where you set the TwiML and it is on that you should set answerOnBridge. – philnash Apr 23 '21 at 07:38
  • Yes ,I did that but in outgoing call case device.connect(function(connection)){} event is getting triggered only after person lifts the call. ringing ,accept events are not getting triggered at all. Can you please suggest on this. – Aditya Apr 23 '21 at 17:21
  • Can you share the code you are using to make the outgoing call and listen for the events please? I think you may be listening to the events on the device object, but you should be listening on the connection object. – philnash Apr 25 '21 at 07:03
  • this.device.connect(function (connection) { connection.on('warning', function (warningName, warningData) { // alert(warningName); if (warningName == "low-bytes-received" || warningName == "low-bytes-sent") { myscope.NetworkIssue(); } }); connection.on('ringing', function (param1) { // alert(warningName); debugger; }); connection.on('accept', function (param2) { // alert(warningName); debugger; }); }); – Aditya Apr 25 '21 at 07:24
  • This is my TWIML app code : var response = new VoiceResponse(); var dial = new Dial(callerId: twilioNumber); if (phoneNumber != null) { dial.Number(phoneNumber); dial.AnswerOnBridge = true; } response.Append(dial); return Content(response.ToString(), "application/xml"); – Aditya Apr 25 '21 at 07:26
  • Can you add any code to your question, please, it's impossible to read in comments. Thanks! – philnash Apr 25 '21 at 07:26
  • Sure I will do that. – Aditya Apr 25 '21 at 07:28
  • Let me know when you have. – philnash Apr 25 '21 at 07:43
  • can you please check once now – Aditya Apr 25 '21 at 07:55
  • Ok, you were not setting `answerOnBridge` correctly on the ``. I've edited my answer with how you should do it. – philnash Apr 25 '21 at 08:04
  • I have tried by changing as you suggested. But no luck. Still this.device.connect(function (connection) {}); is getting called only after user lifts the call. Is that the expected behavior? – Aditya Apr 25 '21 at 08:23
  • OK, so the function you pass to `device.connect(handler)` will only be called once the device fully connects, which is after the ringing event. Also, the `device.connect(handler)` form is deprecated and you should use `device.on("connect", handler)`. See my second edit for the full answer. – philnash Apr 25 '21 at 08:31
  • Super!! This is working as expected and thanks for your patience in resolving this. 1)Here "ringing" event handler is getting called immediately after connecting to the user even that phone is switched off. 2) Call connected sound is coming only user lifts the call. these expected behaviors only? – Aditya Apr 25 '21 at 09:04
  • I’m not sure what you’re asking now. Perhaps you should ask a new question, and make it clear what is happening and what you expect. – philnash Apr 25 '21 at 09:24
  • Actually i have a requirement to save the exact call status in my repository like 1)Not connected - When phone is switched off or Busy 2)UnAnswered - Call connected, But user did not pick up the call 3)Answered - Call picked up the user. – Aditya Apr 25 '21 at 09:45
  • i have published new question also. you can check that here.https://stackoverflow.com/questions/67251982/how-to-get-the-exact-status-of-the-twilio-outbound-call – Aditya Apr 25 '21 at 09:51
  • Then you should use the action and statusCallback webhooks on the `` which will give you all that information. https://support.twilio.com/hc/en-us/articles/223132267-Tracking-the-Status-of-an-Outbound-Twilio-Voice-Call – philnash Apr 25 '21 at 09:52
  • By using this how can I get the status to my Angular application where I have my Twilio client so whenever I disconnect the call I will save the status of the call with other request parameters by calling API. – Aditya Apr 25 '21 at 10:16
  • If you need to send the status of the call to an API, can you do that from the webhook too? – philnash Apr 25 '21 at 10:30
  • But I have some other request parameters also which I need to save with the status. So I am expecting that to get it to my Twilio client so after disconnecting the call I will save all the details at once with the status. – Aditya Apr 25 '21 at 10:37
  • But you always have the Call Sid in the parameters. So you should be able to collect the data around the CallSid, even if it comes from different sources. And honestly, the webhook is the best place to get the final call status from. – philnash Apr 25 '21 at 10:38
  • Can I achieve what u mentioned above like this? As soon as the status ca back webhook calls can I modify the on going call resource with some custom parameters where I will send "current call status" – Aditya Apr 25 '21 at 10:38
  • No, because you’re receiving this callback after the dialled call is over. – philnash Apr 25 '21 at 10:39
  • Sure. I will try as you suggested and will get back to you. Thank you so much for your support. – Aditya Apr 25 '21 at 10:45
  • Hi @philnash, I followed what you suggested to get the event handler calling for call status changes. But I observed if the user did not cuts the call it is playing busy tone from that carrier and after that Twilio is not disconnecting that call. Call Is getting initiated for the second time also. Is this because of Timeout I set for dial attribute? – Aditya Apr 26 '21 at 08:14
  • Continuation to the above comment : After the second time only disconnect is getting called. – Aditya Apr 26 '21 at 08:23
  • That is definitely going to need a new question. Can you ask a new question, show the relevant code, explain what is happening and what you would expect to happen, and then tag me here. – philnash Apr 26 '21 at 10:01
  • Hi @philnash , this is the new question which I posted. https://stackoverflow.com/questions/67271628/twilio-out-going-call-is-getting-triggered-in-loop – Aditya Apr 26 '21 at 18:02
  • Hi @philnash, i have challenges in implementing the hold feature. Can you please help in that as well. https://stackoverflow.com/questions/67288361/twilio-call-hold-is-not-working-as-expected – Aditya Apr 27 '21 at 18:01