2

I'm using Twilio.Device in an Angular app and I need to display a message if the call failed due to an invalid number. I know that you can call Twilio's REST API to get the call status, but is there a way to get the status without making that extra call?

For example, I was hoping that the connection object that you get back in the disconnect handler would give you the status, like this:

Twilio.Device.disconnect(function(connection) {
  if (connection.status === 'failed') {
    // display error message
  }
});

However, that's either not possible or I'm not looking in the right place.

Another idea I had is when I set debug to true, I can see this helpful log when making a call with an invalid number:

[Connection] Received HANGUP from gateway
[Connection] Disconnecting...

But is there any way to access that HANGUP event?

Thanks in advance!

chinaowl
  • 532
  • 5
  • 15

2 Answers2

1

I'm not sure exactly which you'd need, but in the accept or connect methods, try listening for the 'hangup' or 'error' events which are emitted by the Connection class:

Twilio.Device.connect(function(connection) {
  connection.on('hangup', function (err) {
    console.log(err)
  })
})
Mitch Lillie
  • 2,217
  • 18
  • 25
  • So I tried setting an error, disconnect, and hangup handler on the connection object like in your example. As far as I can tell, an error does not trigger when the phone number is invalid, and the connection object that the disconnect handler returns doesn't seem to have any call status data. And the hangup handler doesn't seem to be doing anything? I also couldn't find any documentation on the hangup event. – chinaowl Sep 09 '16 at 20:38
0

Twilio developer evangelist here.

You can actually get all the details that you would normally need the Twilio REST API for on the Twilio.Connection object. Just take a look at the parameters attribute, it contains all the normal Twilio voice request parameters including CallStatus.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Oh, interesting! I'm logging the `connection` object that I get from the `disconnect` handler in the console, but all I can see is this: `parameters { CallSid: "CAdf1291d2e3ed44813515a36f70a30f89" }` – chinaowl Sep 08 '16 at 18:04