Twilio PHP SDK,进行出站呼叫,从用户接收DTMF输入并重定向呼叫

时间:2018-03-14 13:50:09

标签: php twilio twilio-api twilio-php twilio-click-to-call

我尝试使用Twilio PHP SDK从Twilio拨打电话。但我无法弄清楚如何从该调用中获取DTMF输入,然后根据该输入执行某些操作,例如如果按1,则将被叫号码连接到另一个号码。

这是我的外拨电话代码:

$call = $client->calls->create(
        $phone, "+1xxxx",
        array(
            "url" => "http://demo.twilio.com/docs/voice.xml",
            "method" => "GET",
            "statusCallbackMethod" => "POST",
            "statusCallback" => "http://xxxx.com/twilio/call_xxx",
            "statusCallbackEvent" => array(
                "answered"
            )
        )
    );

处理webhook请求的代码:

    //Use the Twilio PHP SDK to build an XML response
    $response = new Twiml();

    //If the user entered digits, process their request
    if(array_key_exists('Digits', $_POST)) {
        switch ($_POST['Digits']) {
            case 1:
                $dial = $response->dial();
                $dial->number($phone);
                break;
            default:
                $response->say('Goodbye.');
        }
    } else {

        //If no input was sent, use the <Gather> verb to collect user input
        $gather = $response->gather(array('numDigits' => 1));

        // use the <Say> verb to request input from the user
        $gather->say("This is xxx. $name just requested a voucher. Press 1 to connect with them now.");

        // If the user doesn't enter input, loop
        $response->redirect('/twilio/call_xxxx');
    }

    //Render the response as XML in reply to the webhook request
    header('Content-Type: text/xml');
    echo $response;

我在哪里弄错了?

1 个答案:

答案 0 :(得分:1)

根据您的代码,您在此处犯了错误:

"url" => "http://demo.twilio.com/docs/voice.xml",
"method" => "GET",

应该是:

"url" => "http://xxxx.com/twilio/call_xxx",
"method" => "POST",

获取呼叫流程的TwiML,并传递您正在寻找的Digits

对于statusCallback,如果需要,您必须使用不同的代码创建另一个端点(与跟踪呼叫状态有关)。

我建议,先让它在没有statusCallback的情况下工作(你可以暂时删除statusCallback ...行)。

相关问题