如何使用javascript和php在浏览器中接听来电 [英] How to answer incoming call in browser using javascript and php

查看:34
本文介绍了如何使用javascript和php在浏览器中接听来电的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

connection.on('incoming', function(conn) {}) 未调用函数.

我正在尝试在浏览器中实现来电.我试过的是一个javascript代码

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("incoming 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();
    });

}

对于拨出电话,我必须添加 Twiml App 拨出电话 URL 的 URL 并使用 rest API 拨打电话以点击 twiml 页面.

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

mydomain.com/Welcome/incoming_call

来电网址代码为:

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

use Twilio\Jwt\ClientToken;
use Twilio\Rest\Client;
use Twilio\TwiMl;
use Twilio\TwiML\VoiceResponse;

    file_put_contents('request.log', "\n" . "incoming call - : " . json_encode($_REQUEST)  . "\n", 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("incoming 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();
});

我是否也需要在 Twiml App 中添加来电 URL 或只添加电话号码来电 URL?

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.

推荐答案

首先,在 twiml 应用程序中,您必须为后端 api 添加 url,其中包含您的 php 代码,并将该 twiml 应用程序添加到您的传入号码.

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.

如果您没有在代币生成中使用身份属性,请检查此网址: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

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

这篇关于如何使用javascript和php在浏览器中接听来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆