代理连接到客户端时,从Twilio获取呼叫参数,例如CallSid

时间:2015-07-09 16:42:25

标签: php angularjs twilio

在我的应用程序中使用PHP和angular / javascript:我的工作流程设置方式是代理自动连接到名为“支持”的队列。当客户端呼入时,它们将连接到支持队列中的第一个代理。当代理连接到客户端时,如何获取CallSid等呼叫参数。

这是前端代码:

$(document).ready(function(){
            Twilio.Device.setup("<?php echo $twilioObj->token->generateToken();?>");
            var connection=null;
            Twilio.Device.ready(function (device) {
                $('#status').text('Ready to start call');
            });
            Twilio.Device.incoming(function (conn) {
                if (confirm('Accept incoming call from ' + conn.parameters.From + '?')){
                    connection=conn;
                    conn.accept();
                }
            });
            Twilio.Device.offline(function (device) {
                $('#status').text('Offline');
            });
            Twilio.Device.error(function (error) {
                $('#status').text(error.message);
            });
            Twilio.Device.connect(function (conn) {
                $('#status').text("Successfully established call");
                toggleCallStatus();
                console.log(conn.parameters.CallSid);
            });
            Twilio.Device.disconnect(function (conn) {
                $('#status').text("Call ended");
                toggleCallStatus();
            });
            function toggleCallStatus(){
                $('#call').toggle();
                $('#hangup').toggle();
                $('#dialpad').toggle();
            }
            $.each(['0','1','2','3','4','5','6','7','8','9','star','pound'], function(index, value) { 
                $('#button' + value).click(function(){ 
                    if(connection) {
                        if (value=='star')
                            connection.sendDigits('*')
                            else if (value=='pound')
                                connection.sendDigits('#')
                                else
                                    connection.sendDigits(value)
                                    return false;
                    }
                });
            });
        });
        function call() {
            // get the phone number to connect the call to
            var value=$.trim($("#tocall").val());
            if( value== false){
                params = {"tocall": "queue"};
                Twilio.Device.connect(params);
            }else{
                params = {"tocall": $("#tocall").val()};
                Twilio.Device.connect(params);
            }

        }
        function hangup() {
            Twilio.Device.disconnectAll();
        }
        function transfer() {
        }
        function sms() {
            params = {'tosms': $('#tosms').val(), 'sms': $('input[name=smsRadio]:checked', '#smsRadios').val()};
            $.post( "twilioApps/sms", params );
        }
        function conference() {
            // get the phone number to connect the call to
            params = {"toconference": $("#toconference").val()};
        }

这是Twilio点击拨打的网址:

$twilioObj = new twilioPlugin;
        header('Content-type: text/xml');
        $mongo = new MongoClient();
        $dbsave= $mongo->selectDB('agentPortal')->selectCollection('test');
        foreach($_REQUEST as $a=>$b){
            $datas = array( $a => $b);
        }
        $dbsave->insert($_GET);

        //$dbsave->save($datas);
        // get the phone number from the page request parameters, if given
        if($_REQUEST['tocall'] == 'queue'){
    ?>
    <Response>
        <Dial action="http://example.com/angular2/twilioApps/callend">
            <Queue>
                support
            </Queue>
        </Dial>
    </Response>
    <?php
        }else{
            if(isset($_REQUEST['tocall'])) {
                $twilioObj->number = htmlspecialchars($_REQUEST['tocall']);
            }
            // wrap the phone number or client name in the appropriate TwiML verb
            // by checking if the number given has only digits and format symbols
            if (preg_match("/^[\d\+\-\(\) ]+$/", $twilioObj->number)) {
                $numberOrClient = "<Number>" . $twilioObj->number . "</Number>";
    ?>
    <Response>
        <Dial callerId="<?php echo $twilioObj->callerId; ?>" action="http://example.com/angular2/twilioApps/callend">
            <?php echo $numberOrClient; ?>
        </Dial>
    </Response>
    <?php
            } else {
                //$numberOrClient = "<Client>" . $twilioObj->clientName . "</Client>";
                //}
                /*
    */
    ?>
    <Response>
        <Enqueue>support</Enqueue>
    </Response>
    <?php
            }
        }

0 个答案:

没有答案