Lars
Lars

Reputation: 29

twilio - stop flow when call end

I have set up a twilio number. When a call comes in, the caller is welcomed with a welcome message, and then the call is forwarded to my cell phone. If the call is not answered, the call goes to a voice mail message, telling the caller to leave a message, and the the call goes to the voice mail. All that works just fine. But if the call is answered, and I hang up, the flow will not stop. It goes on to the voice mail message. Now my question is:

How do I stop the flow, when the call ends?

This is my code:

<?php
header('content-type: text/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Play>http://example.com/telephone/sounds/welcome-message.mp3</Play>
<Dial record="true" timeout="25">
<Number url="http://example.com/telephone/wisper.php">
+4581732211
</Number>
</Dial>
<Play>http://example.com/telephone/sounds/no-answer.mp3</Play>
<Record transcribe="true" transcribeCallback="http://twimlets.com/[email protected]"/>
</Response>

Upvotes: 2

Views: 822

Answers (1)

Akhil Balakrishnan
Akhil Balakrishnan

Reputation: 616

In your code, both Dial and Record is happening in the same TwiML file. So conditional execution is not possible.

To solve this,

1. Move the recording part to another file. (Say recording.php)

2. Then specify the url of the new file (recording.php) as action during Dial. When the dial is completed, twilio will make a request to this URL, and the execution will continue with the TwiML received from this URL.

3. Check the value request parameter DialCallStatus in recording.php. If the dialed call is answered, the value will be completed or answered (In case of conference). Route the call accordingly in recording.php

dial.php

<?php
header('content-type: text/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
    <Play>http://example.com/telephone/sounds/welcome-message.mp3</Play>
    <Dial record="true" timeout="25" action="http://example.com/telephone/recording.php">
        <Number url="http://example.com/telephone/wisper.php">
            +4581732211
        </Number>       
    </Dial>
</Response>

recording.php

<?php
header('content-type: text/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$dial_call_status = $_REQUEST['DialCallStatus'];
if($dial_call_status == "completed" || $dial_call_status == "answered"){
?>
    <Response>
        <Hangup/>
    </Response>
<?php
}else{
?>
    <Response>
            <Play>http://example.com/telephone/sounds/no-answer.mp3</Play>
            <Record transcribe="true" transcribeCallback="http://twimlets.com/[email protected]"/>
    </Response>
<?php
}
?>

Upvotes: 1

Related Questions