doulei3488 2011-08-18 15:47
浏览 60
已采纳

Twilio REST API顺序拨号

I really can't figure out why my code isn't working because Twilio's debugger isnt giving me errors so I don't know what to do...I am trying to make Sequential dialing in REST api using Twilio so it should keep calling numbers in order until one person picks up..Below is my code I have written so far. I am using sessions to keep track of the calls.

File Name:dial.php

<?php
session_start();  



    require 'Services/Twilio.php';
    $version = "2010-04-01";

$arr = array('4167641123','6478604321','9058553456');



    $sid = '....';
    $token = '...';
    $from = '....';
    $to = '416.....';
    $callback = 'www.site.com/dial.php';

    $client = new Services_Twilio($sid, $token, $version);

    //if this is our very first call then CallStatus should be empty so it means we can use the emptiness of this variable
    //to trigger our very first call
    if (!(isset($_REQUEST['CallStatus'])))
    {
    try {
        $call = $client->account->calls->create(
            $from,
            $arr[0],
            'http://demo.twilio.com/welcome/voice/',
            array('Timeout' => 1,
                  'StatusCallback' => $callback)
            );
            var_dump($call);
        } catch (Exception $e) {
            var_dump($e);
        }
}
// if the CallStatus variable is not empty then the else statement will execute
else
{
//if this part of code runs for the first time, it means this is our 2nd call because the 1st person did not pick up
//this means the second number in the array will be called
//each time this statement runs it adds a 1 to the index of the array but if the last index number called was the final and 
//last number in the array, then this statement wont run and instead session at the bottom if statement will be initialized to 0
//so that if this script is ran again it will start off from the first number in the array
if (!($_SESSION['X']>=count($arr)-1) && isset($_REQUEST['CallStatus']) && ($_REQUEST['CallStatus']=='failed'|| $_REQUEST['CallStatus']=='no-answer' || $_REQUEST['CallStatus']=='busy'))
{

$_SESSION['X']=$_SESSION['X']+1;

    try {
        $call = $client->account->calls->create(
            $from,
            $arr[SESSION['X']],
            'http://demo.twilio.com/welcome/voice/',
            array('Timeout' => 1,
                  'StatusCallback' => $callback)
            );
            var_dump($call);
        } catch (Exception $e) {
            var_dump($e);
        }
}
//initializes the session to 0 because if we have reached to this else statement,
//then it means the if statement above did not run and we have already called the last person
//in the phone number array so we are at an end and we must close the program
//by leaving session at 0 for the next trial to run properly
else
{

$_SESSION['X']=0;

}

}

I have this other file same, sequential diallnig written using php and twilio's Dial verb, the Dial verb's parameters allow it to pass the array index but in my case I don't know how to pass the array index parameter....any ideas?

<?php 

// Set the numbers to call
$numbers = array("<number to call 1>", "<number to call 2>", "<number to call n>");
$number_index = isset($_REQUEST['number_index']) ? $_REQUEST['number_index'] : "0";
$DialCallStatus = isset($_REQUEST['DialCallStatus']) ? $_REQUEST['DialCallStatus'] : "";

header("content-type: text/xml"); 

// Check the status of the call and 
// that there is a valid number to call

if($DialCallStatus!="completed" && $number_index<count($numbers)){ 
?>
    <Response>
    <Dial action="attempt_call.php?number_index=<?php echo $number_index+1 ?>">
        <Number url="screen_for_machine.php">
        <?php echo $numbers[$number_index] ?>
        </Number>
    </Dial>
    </Response>
<?php
} else {
?>
    <Response>
        <Hangup/>
    </Response>
<?php
}
?>
  • 写回答

1条回答 默认 最新

  • dongzhuo1930 2011-08-18 15:52
    关注

    Your code has syntax errors and such.

    For example SESSION['X']=0; is not doing what you might have thought it is doing. If that's a session variable, it should be written as $_SESSION['X']=0;.

    To spot these issues, enable error reporting to the highest level, then log errors to a file and then watch that file. It will give you some hints.

    See Error Handling and Logging Docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制