Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

twilio - How to answer incoming call in browser using javascript and php

connection.on('incoming', function(conn) {}) function not called.

I am trying to implement the incoming call in the browser. What I tried is a javascript code

var number = $("#number").val();

params = {
    "PhoneNumber": number,  
    "CallerId": "+13604924000",
    "AgentName": "Noman Javed",
};

Twilio.Device.setup(token);

Twilio.Device.ready(function(device) {

    console.log('Ready');

    // ---------------------------------------------------
    // Explicitly create a new outgoing connection
    var connection = Twilio.Device.connect(params);

    console.log('PhoneNumber: ' + params.PhoneNumber);

    $('#hangup_btn_span').show();
    $("#hangup_btn").show();

    connection.on('error', function(error) {
        console.log(error);
    });

    connection.on('accept', function(conn) {
    
    });

    connection.on('incoming', function(conn) {
        console.log("incomming call connection object log");
        console.log(conn);
        console.log('Incoming connection from ' + conn.parameters.From);
        // accept the incoming connection and start two-way audio
        // conn.accept();
    });

}

For outgoing calls, I had to add the URL of the Twiml App outgoing call URL and dial the call using rest API to hit twiml page.

For incoming calls, I had add incoming call URL in the phone number incoming call URL that is

mydomain.com/Welcome/incoming_call

The incoming call urls code is:

header('Content-Type: application/xml');
// Load the required files
require APPPATH . 'libraries/vendor/autoload.php';

use TwilioJwtClientToken;
use TwilioRestClient;
use TwilioTwiMl;
use TwilioTwiMLVoiceResponse;

    file_put_contents('request.log', "
" . "incoming call - : " . json_encode($_REQUEST)  . "
", FILE_APPEND);    
?>
<Response>
   <Pause length="2"/>
   <Say voice="woman">Dialing number</Say>   
   <Dial callerId="<?php echo $_REQUEST['From']; ?>" >
        <?php echo $_REQUEST['To']; ?>        
   </Dial>
</Response>

How I will receive the call in the browser in this function with parameters

connection.on('incoming', function(conn) {
    console.log("incomming call connection object log");
    console.log(conn);
    console.log('Incoming connection from ' + conn.parameters.From);
    // accept the incoming connection and start two-way audio
    // conn.accept();
});

Do I need to add an incoming call URL in Twiml App too or just add in the phone number incoming URL?

Thanks in advance for helping and guiding.

question from:https://stackoverflow.com/questions/65879032/how-to-answer-incoming-call-in-browser-using-javascript-and-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, in the twiml app you have to add the url for your backend api where you have your php code and add that twiml app to your incoming number.

Now, while generating the token for your user you must have used an 'identity'. We need to use that identity value to connect the incoming call to the user. Something like below:

$response = new VoiceResponse();
$dial = $response->dial('');
$dial->client(IDENTITY_VALUE);

this dial->client will connect the incoming call to the browser.

If you have not used identity attribute in your token generation, check this url: https://www.twilio.com/docs/iam/access-tokens?code-sample=code-creating-an-access-token-voice-2&code-language=PHP&code-sdk-version=5.x


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...