1

I'm building a call system with Twilio Twiml where a user calls to my Twilio number and needs to enter a code to be attended:

callsip.php

<?php 
   echo header('content-type: text/xml');
   echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<Response>
 <Play>https://mywebsite.com/welcome.mp3</Play>
 <Gather numDigits="11" action="myfile.php">
    <Say voice="alice">Please enter your code</Say>
 </Gather>
</Response>

After enter the code, the call goes to Twilio clients:

myfile.php

<?php 
  echo header('content-type: text/xml');
  echo '<?xml version="1.0" encoding="UTF-8"?>';
  $code = $_POST['Digits'];
?>
<Response>
  <Dial timeout="20" record="record-from-answer" recordingStatusCallback="https://mywebsite.com/record.php" recordingStatusCallbackEvent="in-progress completed absent">
        <Client>
            <Identity>myuser</Identity>
            <Parameter name="code" value="<?php echo $code; ?>"/>
        </Client>
    </Dial>
</Response>

I want user listen a hold music while is waiting for an answer, I tried adding a Enqueue tag in myfile.php:

<Response>
<Enqueue waitUrl="https://mywebsite.com/hold_music.php">support</Enqueue>
...
</Response>

hold_music.php

<?php 
 echo header('content-type: text/xml');
 echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<Response>
  <Play loop="0">https://mywebsite.com/hold_music.mp3</Play>
</Response>

But I'm a bit lost, the hold music is playing but Twilio clients are not being called.

How can I fix it?

I'd like your help.

NekoLopez
  • 579
  • 1
  • 9
  • 28

2 Answers2

0

Adding music on hold to your call flow requires a media resource to play the music while the dialed party answers. One way to accomplish that is to enable Agent Conference in your Twilio console here, and add the initial caller to that conference as part of the Gather action URL logic, then Create an Agent Conference Participant using that ConferenceSID with Early Media set to False.

Note, you will not be able to dial using the original CallersID, unless that number is a Verified CallerID (you can also use a Twilio number in your account as the outbound CallerID). Also, make sure to account for cases where the dialed party doesn't answer, so the original caller is not left on the conference, listening to music forever.

Alan

Alan
  • 10,465
  • 2
  • 8
  • 9
  • Thank you for your answer, but is there another option without create a conference? – NekoLopez Feb 17 '20 at 16:39
  • No, you need some way of placing the first leg in some state, where you can play music on hold, and using a conference is the ideal way of handling that. An alternate possibility would be to enqueue the call and then dial the unique queue (once the dialed party answers). You would need to use the REST API Calls Resource to dial the other party and execute the TwiML to dial the queue. – Alan Feb 17 '20 at 20:19
  • I believe it is possible by making several REST calls. The solution can be found here: https://stackoverflow.com/questions/41321365/play-music-while-waiting-an-answer-in-twiml-dial – Jeff Vdovjak Feb 19 '20 at 01:30
0

On hold_music.php you can create call like https://www.twilio.com/docs/voice/make-calls

$call = $twilio->calls
               ->create("+14155551212", // to
                        "+15017122661", // from
                        ["url" => "http://demo.twilio.com/docs/voice.xml"]
               );

print($call->sid);

And than on answer url you can join both call. means

<Response>
   <Dial>
     <Queue url="about_to_connect.xml">support</Queue>
   </Dial>
</Response>

it's work for me