Bill Bill - 3 months ago 11
PHP Question

submitting POST data when you hang up on Twilio

I have an application that runs with Twilio. The idea is that you will call a phone number twilio will answer and give you menu options. Once you press a digit it will submit the post data and then hang up (That part all works correctly) The problem I'm having is finding out if there is a way to submit post data if the user just hangs up as soon as twilio answers... I found the following code on their documentation.

<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/gather_hints.xml -->
<Response>
<Gather action="/process_gather.php" method="GET">
<Say>Enter something, or not</Say>
</Gather>
<Redirect method="GET">
/process_gather.php?Digits=TIMEOUT
</Redirect>
</Response>


The problem with this is you need to wait until the end of the message for gather to time out. Is there a way to make it so as soon as starts, if the user hangs up it'll do something like go to the redirect tag?

Thanks!

Edit: So on the process_gather.php page, I can save the completed status as a variable?

$Completed = $_POST["completed"]; //which would set $Completed == 'completed'


and then in that page I can just basically say if if(!empty($Completed)) do something
(I have to work it into my logic, but I just want to make sure I get the general idea)

Answer Source

The best way to solve this is by using the StatusCallback URL. You can set this URL for inbound calls in the Number configuration in your account, or through an optional argument for outbound calls.

When the call completes, Twilio will POST (or GET) to the URL with all the same variables it passes during a normal request. The CallStatus variable will be set to completed.

If that URL is called before you get input from the user (to your `/process_gather.php' script), then they hung up.

Note: You can use cookies to manage session state within a call: all requests made by Twilio during the same call (and the StatusCallback request) will contain any cookies you send. This allows you to use a session variable to track call progress.

PHP Example for /proces_gather.php:

 <?php 
 session_start(); 
 // adds it to our session 
 $_SESSION['gather_result']=$_POST["Digits"]; 
 // then do whatever else you want your gather script to do.
 ?> 

PHP Example for /status_callback.php, which should be configured as the StatusCallback URL for the phone number in the Twilio Account screen:

<?php
session_start();
//check for gather_result
if(isset($_SESSION['gather_result'])){
  //user did enter digits
} else {
  //do whatever you want to do if the user entered no digits.
}
?>