2

In my application, I need to do Twilio holding and retrieving back a call. I researched and got this link : https://www.twilio.com/docs/api/rest/change-call-state.

javascript

function holdCall() {  // hold a call
var callSid = connection.parameters.CallSid;

$.ajax({
    url: "http://www.domain.com/phone/phone_ajax.php",
    type: 'POST',
    data: {
        callSid: callSid
    },
    success: function(data) {
        console.log(data);
    },
    error: function() {

    }, 
    complete: function() {

    }

});
}

The ajax call will go to this page.

phone_ajax.php

require_once ( "http://www.domain.com/phone/phone_api/vendor/autoload.php");
use Twilio\Rest\Client;
use Twilio\Jwt\ClientToken;

// initialize

if ( $_POST['callSid'] ) {  // hold a call
    $client = new Client($twilioAccountSID, $twilioAuthenticationToken);
    $calls = $client->calls->read(
        array("ParentCallSid" => $_POST['callSid'])
    );
    // Loop over the list of calls and echo a property for each one
    foreach ($calls as $call) {
        // This will return child call sid e.g CA9ccxxxxxxxxxx
        $twilioCall = $client
        ->calls($call->sid)
        ->update(
            array(
                "url" => "http://demo.twilio.com/docs/voice.xml",
                "method" => "POST"
            )
        );

        echo $twilioCall->to;
    }
} 

I tried calling to my mobile phone, picked up the call and clicked Hold button. The call in my browser got ended and the call in my phone didn't ended up (I can hear hold music in my phone). When I again click on the Hold button in the dialpas, the call should be retrieved back. How can I achieve this?

Can anyone help me to do this? Thanks in advance.

Jenz
  • 8,280
  • 7
  • 44
  • 77

1 Answers1

3

Twilio developer evangelist here.

The problem here is that when you update the first call to redirect to the hold music that disconnects the other call and ends it.

This is likely because your TwiML ends after the <Dial> that connected the two calls in the first place. You can keep a call going by either adding more TwiML after the or using the action attribute.

If instead, your Twilio Client leg of the call had the following TwiML:

<Response>
  <Dial action="/holding">NUMBER_TO_DIAL</Dial>
</Response>

And the endpoint /holding looked like:

<Response>
  <Say>You have a caller on hold.</Say>
  <Redirect>/holding</Redirect>
</Response>

Then your call won't end. It will instead endlessly say "You have a caller on hold". You could implement this however you like though.

Now, instead of shipping the caller on the other end off to "http://demo.twilio.com/docs/voice.xml" you should place them in a queue to wait to be retrieved. So, you'd need another endpoint at say /place-on-hold which you would update the call to when the hold button is pressed. That would need the TwiML:

<Response>
  <Enqueue waitUrl="SOME_HOLD_MUSIC">ADMIN_ID</Enqueue>
</Response>

If you use the ID of the admin who put the user on call then if you have multiple users of the Twilio Client dialler then they will each have their own holding queue.

Finally, you need to Reconnect the callers. For this you need to redirect your admin out of their holding pattern using the REST API again and onto some TwiML that will dial their hold queue which will reconnect the callers. The TwiML would look like:

<Response>
  <Dial action="/holding">
    <Queue>ADMIN_ID</Queue>
  </Dial>
</Response>

This will dequeue the caller on hold and reconnect. Note we also include the action attribute so that the user can be placed on hold again.

Let me know if this helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • @Philnash..How can I do this with javascript client..Do I really need TwiML to do this? – Jenz Dec 20 '16 at 10:59
  • Well, if you're looking for hold music and other features of putting someone on hold then yes as Twilio Client is only in control of one side of the call. You could just [mute the Twilio Client](https://www.twilio.com/docs/api/client/connection#mute) end which will stop the caller from hearing the person using the Client but leave them in silence. – philnash Dec 20 '16 at 11:02
  • @Philnash..How can I call the twiML file? `$twilioCall = $client ->calls($call->sid) ->update( array( "url" => "redirect_twiml.xml", "method" => "POST" ) );` Is it like this? – Jenz Dec 20 '16 at 12:38
  • Yeah, that will redirect your call to the TwiML hosted at redirect_twiml.xml. – philnash Dec 20 '16 at 12:39
  • That's up to your system really. I assume your users that are making calls have to log in first. Can you send that through as a parameter from the interface to the redirect URL and use it there? – philnash Dec 20 '16 at 12:48
  • Holding and unholding is working initially..If I again click on the button it is not putting the call again on hold.. – Jenz Dec 21 '16 at 04:50
  • When you are Dialling into the Queue, are you giving the Dial an action attribute? Are you getting an error or one part of the call hanging up? – philnash Dec 21 '16 at 09:23
  • yup..I have added the action attribute..On clicking Hold, update will call holding twiml and again on clicking, update will call twiml containing Queue.. – Jenz Dec 21 '16 at 09:51
  • For the first time holding/unholding is working fine..But again when I press Hold, the call is not getting holded.. – Jenz Dec 21 '16 at 09:54
  • What are you experiencing? An error? Is one side hanging up? What _is_ happening? – philnash Dec 21 '16 at 10:05
  • @philnash I am facing two issue with the above setup. – Gautam Kumar Samal Apr 26 '22 at 19:43
  • First is that after first round of Hold and Resume, the second time hold isn't working. The second time I pressed hold, I heard a statement - We are sorry, some application error occurred. Good bye! at the sender's end and it disconnects. However the receiver end keeps playing the hold music. – Gautam Kumar Samal Apr 26 '22 at 19:45
  • Second is that when the receiver hangs up, The caller keeps repeating the action statement, never disconnects. Is there a way to gracefully close the connection when receiver disconnects? I assume the receiver can hang up either on call or on hold. – Gautam Kumar Samal Apr 26 '22 at 19:47
  • @GautamKumarSamal I see you've asked a question about this that is more specific and covers these comments. I'll try to get you an answer over there. – philnash Apr 26 '22 at 22:54