chinaowl
chinaowl

Reputation: 532

Is there a way to know a Twilio call failed without calling the REST API?

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!

Upvotes: 2

Views: 565

Answers (2)

philnash
philnash

Reputation: 73090

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.

Upvotes: 0

Mitch Lillie
Mitch Lillie

Reputation: 2407

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)
  })
})

Upvotes: 1

Related Questions