Search code examples
phptwilioivrtwilio-twiml

Twilio <say> being cut off by PHP sleep()


I have a Twilio IVR that asks a caller for their account number. After the caller enters their account number at voice.xml the input is sent to begin.php. I am trying to send the caller back to voice.xml if their account number is not in my database. The code below will send them back as expected but they are redirected before I can inform them that their number was incorrect. I tried adding PHP sleep() but this seems to cut off the Twilio <say> tag as well...

if(mysql_num_rows($result) == 0){
  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  echo "<Response><Say>Sorry your account number was not found.</Say></Response>";
  sleep(3);
  header('Location: voice.xml');
}

Solution

  • You shouldn't handle redirection in your script. use the Twiml <Redirect> verb instead.

    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Say>Sorry your account number was not found.</Say>
        <Redirect>voice.xml</Redirect>
    </Response>
    

    This way, Twilio will announce the message to the caller, then redirects him to voice.xml.

    Hope it helps.