Joe Torraca
Joe Torraca

Reputation: 2018

Twilio conditional StatusCallback

I'm currently working on a project involving Twilio, and part of the project involve taking an array of 4 numbers, and calling them sequentially until 1 of them picks up at which point it stops calling. Everything seems to be working, except for the stop calling part.

By using the StatusCallback method, even after the call is answered and confirmed (By pressing 1 on the keypad using a verb), it still proceeds to continue calling the other numbers. Is there a way to make it so that the StatusCallback only happens if the call was not answered?

<?php

$twilio = new Services_Twilio($AccountSID, $AccountToken);

$twilioPhone = ""; // Twilio number

$numbers = Array(
    trim($_GET["num1"]),
    trim($_GET["num2"]),
    trim($_GET["num3"]),
    trim($_GET["num4"])
);
$message = trim($_GET["msg"]);

$called = $_GET["phone"];
$run = 0;
if ($called) {
    $run = array_search($called, $numbers)+1;
}

if ($_GET['Digits']) {

    // Code to be run when the call is confirmed
    ?>
    <Response>
        <Say voice="alice">Okay, this number has been confirmed.</Say>
    </Response>
    <?
    exit;
} else {

    if (empty($_GET["automated"]) || $_GET["automated"] == null) {
        $paramString = "automated=1&num1=".$numbers[0]."&num2=".$numbers[1]."&num3=".$numbers[2]."&num4=".$numbers[3]."&msg=".$message;
        header("location: URL_HERE/index.php?".$paramString);
    } else {
        try {
            $call = $twilio -> account -> calls -> create(
                $twilioPhone,
                $numbers[$run],
                'http://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3E'.rawurlencode(stripslashes($message)).'%3C%2FSay%3E%3CPause%20length=%221%22%2F%3E%3CGather%20numDigits=%221%22%20action=%22URL_HERE%2Findex.php%22%20method=%22GET%22%3E%3CSay%3EPlease%20press%201%20to%20confirm%20you%20have%20recieved%20this%20message.%3C%2FSay%3E%3C%2FGather%3E%3C%2FResponse%3E',
                Array(
                    "timeout"=>"15",
                    "ifmachine"=>"hangup",
                    "StatusCallback"=>"URL_HERE/index.php?automated=1&phone=".$numbers[$run]."&num1=".$numbers[0]."&num2=".$numbers[1]."&num3=".$numbers[2]."&num4=".$numbers[3]."&msg=".$message
                )
            );
        } catch (Exception $err) {
            echo "Error: " . $err -> getMessage();
        }
    }

}
?>

Upvotes: 4

Views: 348

Answers (1)

Jon Gottfried
Jon Gottfried

Reputation: 429

Twilio Developer Evangelist here.

One way to accomplish this functionality is by using the FindMe Twimlet.

If you want to integrate it into your own application, I recommend checking out the Call Screening HowTo in Twilio's documentation. The Call Screening HowTo calls a list of numbers in order and only moves on to the next number if the user does not accept the call by pressing a key.

Upvotes: 1

Related Questions