为了使用带有 Twilio 的笔记本电脑/计算机在浏览器中拨打电话,您需要Twilio 客户端库,还需要安装Twilio PHP 库。
这假设您已经创建了一个 Twilio 帐户并购买了一个具有语音和短信功能的 twilio 号码。我假设您已经安装了 Twilio PHP 库。
当您按下“makeCall”按钮时,系统会提示您使用麦克风。选择您用来与笔记本电脑通话的任何麦克风。戴上耳机,给自己打电话。很有意思!
做这个 :
找到您的Twilio 帐户 SID和主身份验证令牌。
创建一个TwiML APP并将语音请求 URL 指向
https://EXAMPLEmywebsite.com/voice.php
语音.php
<?php
include '../twilio/Twilio/autoload.php';
use Twilio\Twiml;
$response = new Twiml;
//THE PHONE NUMBER YOU HAVE PURCHASED
$TWILIO_CALLER_ID = '';
// get the phone number from the page request parameters, if given
if (isset($_REQUEST['To']) && strlen($_REQUEST['To']) > 0) {
$number = htmlspecialchars($_REQUEST['To']);
$dial = $response->dial(array
('callerId' => $TWILIO_CALLER_ID,
'record' => 'record-from-answer'
));
if($number == "English Queue" || $number== "Spanish Queue") {
$dial->queue($number);
}
elseif (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
$dial->number($number);
} else {
$dial->client($number);
}
} else {
$response->say("Thanks for calling!");
}
header('Content-Type: text/xml');
echo $response;
拨号盘.php
<!-- jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Twilio Client IMPORTANT -->
<script src="https://media.twiliocdn.com/sdk/js/client/v1.6/twilio.min.js"></script>
//display phone logs
<div id="log"></div>
//simple basic dialpad
<input id="phoneScreen" placeholder="enter phone number">
//dial button
<button id="makeCall" class="btn btn-success btn-dial">
<img src="https://img.icons8.com/ios/25/000000/ringer-volume.png">
</button>
//hang up button
<button id="endCall" class="btn btn-danger btn-dial">
<img src="https://img.icons8.com/ios/25/000000/end-call.png">
</button>
//include twilioClient.js
<script src="twilioClient.js"></script>
twilioClient.js
$(function () {
log('Connecting Agent Station...');
$.getJSON('./token.php').done(function (data) {
log('Agent Station Connected.');
//console.log('Token: ' + data.token);
// Setup Twilio.Device
Twilio.Device.setup(data.token);
Twilio.Device.ready(function (device) {
log('Phone Ready For Call');
});
Twilio.Device.error(function (error) {
log('Agent Station Error: ' + error.message);
//update call log
});
Twilio.Device.connect(function (conn) {
log('Success! Call Established.');
});
Twilio.Device.disconnect(function (conn) {
log('Call ended.');
});
//bind button to answer incoming call?
document.getElementById('answerCall').onclick = function () {
// get the phone number to connect the call to
var queueName = document.getElementById('queuename').value;
var params = {
To: queueName
};
log('Connecting agent to ' + params.To + '...');
Twilio.Device.connect(params);
};
setClientNameUI(data.identity);
})
.fail(function () {
log('Could not connect agent station to server!');
});
// Bind button to make call
document.getElementById('makeCall').onclick = function () {
// get the phone number to connect the call to
var params = {
To: document.getElementById('phoneScreen').value
};
log('Calling ' + params.To + '...');
Twilio.Device.connect(params);
Twilio.Device.connect(function (connection) {
//retrieve call sid
var callSid = connection.parameters.CallSid;
//retrieve phone number
var phonenumber = $('#phoneScreen').val();
});
};
// Bind button to hangup call
document.getElementById('endCall').onclick = function () {
log('Hanging up...');
Twilio.Device.disconnectAll();
};
});
// Activity log
function log(message) {
var logDiv = document.getElementById('log');
logDiv.innerHTML += '<p>> ' + message + '</p>';
logDiv.scrollTop = logDiv.scrollHeight;
}
令牌.php
<?php
include '../twilio/Twilio/autoload.php';
use Twilio\Jwt\ClientToken;
// choose a random username for the connecting user
$identity = "Caller".rand(9, 99999).round(microtime(true) * 1000).date('Y');
//twilio account sid
$TWILIO_ACCOUNT_SID = 'YOUR_TWILIO_ACCOUNT_SID';
//twilio auth token
$TWILIO_AUTH_TOKEN = 'YOUR_TWILIO_AUTH_TOKEN';
//twilio app sid
$TWILIO_TWIML_APP_SID = 'YOUR_TWIMl_APP_SID';
$capability = new ClientToken($TWILIO_ACCOUNT_SID, $TWILIO_AUTH_TOKEN);
$capability->allowClientOutgoing($TWILIO_TWIML_APP_SID);
$capability->allowClientIncoming($identity);
$token = $capability->generateToken();
// return serialized token and the user's randomly generated ID
header('Content-Type: application/json');
echo json_encode(array(
'identity' => $identity,
'token' => $token,
));