John Smith
John Smith

Reputation: 478

Detecting when a Twilio REST API call is terminated

Using the Twilio REST API and Conference you can create a call between the caller and the rep that the rep can leave and re-enter by pressing * or 0.

When the rep presses * within a Conference the script on the reps end removes them from the Conference and takes them to a backend where they can find out what the customer needs, leaving the caller in an empty Conference until the rep re-joins by pressing 0.

If the rep hangs up within the Conference, the callback "$_POST['CallStatus'] = completed" is made. This lets the script know to end all open Conferences, thus redirecting any waiting callers to the main menu.

However if the rep hangs up while outside of the Conference (using the backend, while a caller remains waiting) the same callback is not made.

What variables would indicate that the REST API call to the rep has hung up? I thought it was $_POST['CallStatus'] or $_POST['DialCallStatus'] but I've listened for both but have not been able to produce an on-disconnect callback for the rep's end.

The following variables are sent to Calls.xml via the REST API:

From => $from

To => $to

Url => $script_url

StatusCallback => $callback_url

StatusCallbackEvent => array( "initiated", "ringing", "answered", "completed" )

$callback_url works and detects $_POST['CallStatus'] = in-progress but not completed. (unless the rep hangs up within the conference, then it works as intended)

CALL FUNCTION

class Call
{
    var $url    = 'https://api.twilio.com/2010-04-01/Accounts/XXXXXX/Calls.xml';
    var $from   = '+1XXXXXX';

    function dial( $number, $script, $callback = false )
    {
        $switch = 'http://XXXXXX.com/' . $script . '.php';

        $post = array( 'From' => $this->from, 'To' => $number, 'Url' => $switch );

        if ( $callback )
        {
            $post['StatusCallback'] = $callback;
            $post['StatusCallbackEvent'] = array( "initiated", "ringing", "answered", "completed" );
        }   

        $curl = curl_init();

        curl_setopt( $curl, CURLOPT_URL, $this->url );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $curl, CURLOPT_VERBOSE, 1 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
        curl_setopt( $curl, CURLOPT_USERPWD, 'XXXXXX:XXXXXX' );

        if ( $post )
        {
            $data = array();

            foreach ( $post as $property => $value )
            {
                array_push( $data, $property . '=' . $value );
            }

            curl_setopt( $curl, CURLOPT_POST, 1 );
            curl_setopt( $curl, CURLOPT_POSTFIELDS, implode( '&', $data ) );        
        }

        $page = curl_exec( $curl );

        curl_close( $curl );

        return $page;
    }
}

THE CALL

$call = new Call;
$call->dial( $number, $script, $callback );

Upvotes: 0

Views: 541

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

You get that callback with the completed status when the rep hangs up during the call due to the nature of the action attribute on the <Dial> verb. However, once you are outside of a <Dial> then you no longer have that.

There is a way to do this though! You need to set up to receive call progress events. You can register to receive webhooks for various events that a call goes through, from queued through to completed. You can register for these using the StatusCallback and StatusCallbackEvent parameters when generating a call from the REST API. You can then get a webhook when the call is over, even when you outside of a <Dial>.

Upvotes: 1

Related Questions