Twilio.Device - Besides Connect - Answer?

JHinkle
JHinkle
edited October 4 in Voice


I'm referencing the following quick-start:


Specifically


File: quickstart.js - it holds the js code for the Twilio.Device.

Currently there is :  Twilio.Device.connect

which fires when the outbound call is connected.

I need to know when the call is "Answered" - to be able to distinguish between when the phone is ringing and when the call is answered by the person being called.

Is there CB function currently implemented? If so where?

If not - is there a way to acquire the SIP codes associated with the call?

What I'm trying to accomplish is to time between when the call is active and if (and when) a call is answered to automatically hangup prior to an answering machine answering the phone - and bail from this timing process if the call is actually answered.

Thanks

Additional: -- Use Twilio AMD when also using the Client SDK (browser call) - ....

I know that AMD has Twilio send a CB to the Server - did the Browser still receive the "Connect" or is the Browser NOW out of the picture?

Tagged:

Answers

  • Hi! Twilio Developer Educator here.

    First, I want to direct you to this Quickstart for the Voice SDK: https://github.com/TwilioDevEd/voice-javascript-sdk-quickstart-csharp (The "Client" quickstart uses an older version of the SDK. Version 2 was released this year with some slight differences and the "voice-javascript-quickstart-csharp" repo implements version 2.)

    The documentation here https://www.twilio.com/docs/voice/sdks/javascript reflects the most up-to-date functionality.

    `device.connect()` is a method that initiates a connection from the browser device to your TwiML App (which then points to an endpoint in your backend application to look for TwiML instructions). So there is no "connect" event on the browser device. The call object returned when device.connect() is invoked does have a .status() method to check for the Twilio Call's "ringing" versus "open" (https://www.twilio.com/docs/voice/sdks/javascript/twiliocall#callstatus), but the events emitted by the call object are related to the SDK, and not the lifecycle of the Twilio Call. And "open" doesn't differentiate between an answering machine and a human answering the phone.

    You could use AMD in your /voice endpoint where you create your TwiML instructions ... but it would take some extra work to send the related information back to your browser device.

    This may be a bit backwards from what you're envisioning, but you can use AMD in your /voice endpoint and make the AMD callback point to an endpoint that updates the TwiML of the call (https://www.twilio.com/docs/voice/api/call-resource#update-a-call-resource) you want to end (by sending a <Hangup /> instruction https://www.twilio.com/docs/voice/twiml/hangup).

    Once the Call has been ended by the <Hangup /> TwiML, the browser device's call object will emit a disconnect event and the voice connection will end.

  • JHinkle
    JHinkle
    edited October 4

    Thanks a lot, @bdelvalle .

    Your process flow was just what I needed. I think Twilio documentation does not go deep enough at times - I a never found the flow you presented documented.

    I have looked at the call.status() and it seems I can't get access to it on change (please excuse - I'm a C guy - not js).

    It seems I have to call it periodically to see any changes.

    Is there anyway to get a "on-change" event to occur?

  • bdelvalle
    bdelvalle
    edited October 5

    I don't think you can listen for an on change event on the call.status(), at least not with the SDK right now.

    But, in your /voice endpoint, when you are setting up your <Dial> verb, you can pass in some parameters for the outgoing call (https://www.twilio.com/docs/voice/twiml/number#examples-3). You would want to use the statusCallback param and the statusCallbackEvent param. (The statusCallbackMethod param is optional and defaults to a POST request.)

    var response = new VoiceResponse();
            var dial = new Dial();
            dial.Number("+12349013030", statusCallbackEvent: new []{Number
                .EventEnum.Initiated, Number.EventEnum.Ringing, Number.EventEnum
                .Answered, Number.EventEnum.Completed}.ToList(),
                statusCallback: new Uri("https://myapp.com/calls/events"),
                statusCallbackMethod: Twilio.Http.HttpMethod.Post);
            response.Append(dial);
    

    This will send a POST to (in this example) the /calls/events endpoint for each of the events you've specified. In this example it will send separate POST requests to the /calls/events endpoint when the call status is Initiated, Ringing, Answered, and then Completed .

    The request body of this POST request will have a CallStatus property with the event (e.g. "initiated" or "ringing").

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support