Skip to content

Instantly share code, notes, and snippets.

@leonascimento
Created September 17, 2018 21:07
Show Gist options
  • Save leonascimento/104e218959ad632f632df56ebb967f61 to your computer and use it in GitHub Desktop.
Save leonascimento/104e218959ad632f632df56ebb967f61 to your computer and use it in GitHub Desktop.
$(function () {
var speakerDevices = document.getElementById('speaker-devices');
var ringtoneDevices = document.getElementById('ringtone-devices');
var outputVolumeBar = document.getElementById('output-volume');
var inputVolumeBar = document.getElementById('input-volume');
var volumeIndicators = document.getElementById('volume-indicators');
log('Requesting Capability Token...');
$.getJSON('http://linen-uakari-3468.twil.io/capability-token')
.done(function (data) {
log('Got a token.');
console.log('Token: ' + data.token);
-
// Setup Twilio.Device
Twilio.Device.setup(data.token);
Twilio.Device.ready(function (device) {
log('Twilio.Device Ready!');
console.log(device);
document.getElementById('call-controls').style.display = 'block';
});
Twilio.Device.error(function (error) {
log('Twilio.Device Error: ' + error.message);
});
Twilio.Device.connect(function (conn) {
log('Successfully established call!');
document.getElementById('button-call').style.display = 'none';
document.getElementById('button-hangup').style.display = 'inline';
volumeIndicators.style.display = 'block';
bindVolumeIndicators(conn);
});
Twilio.Device.disconnect(function (conn) {
log('Call ended.');
document.getElementById('button-call').style.display = 'inline';
document.getElementById('button-hangup').style.display = 'none';
volumeIndicators.style.display = 'none';
});
Twilio.Device.incoming(function (conn) {
debugger
console.log("conn>>>", conn);
// var archEnemyPhoneNumber = '+551939578769';
// if (conn.parameters.From === archEnemyPhoneNumber) {
// conn.reject();
// log('It\'s your nemesis. Rejected call.');
// } else {
// accept the incoming connection and start two-way audio
conn.accept();
log("incoming connection from");
console.log('Incoming connection from ' + conn.parameters.From);
console.log('Incoming' + conn.parameters);
// }
});
console.log("data>>>", data);
setClientNameUI(data.identity);
Twilio.Device.audio.on('deviceChange', updateAllDevices);
console.log(Twilio);
// Show audio selection UI if it is supported by the browser.
if (Twilio.Device.audio.isSelectionSupported) {
document.getElementById('output-selection').style.display = 'block';
}
})
.fail(function () {
log('Could not get a token from server!');
});
// Bind button to make call
document.getElementById('button-call').onclick = function () {
// get the phone number to connect the call to
var params = {
To: '+5584998534236',
From: '+55551939578769',
phone: '+55551939578769'
};
console.log('Calling ' + params.To + '...');
Twilio.Device.connect(params);
};
// Bind button to hangup call
document.getElementById('button-hangup').onclick = function () {
log('Hanging up...');
Twilio.Device.disconnectAll();
};
document.getElementById('get-devices').onclick = function() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(updateAllDevices);
};
speakerDevices.addEventListener('change', function() {
var selectedDevices = [].slice.call(speakerDevices.children)
.filter(function(node) { return node.selected; })
.map(function(node) { return node.getAttribute('data-id'); });
Twilio.Device.audio.speakerDevices.set(selectedDevices);
});
ringtoneDevices.addEventListener('change', function() {
var selectedDevices = [].slice.call(ringtoneDevices.children)
.filter(function(node) { return node.selected; })
.map(function(node) { return node.getAttribute('data-id'); });
Twilio.Device.audio.ringtoneDevices.set(selectedDevices);
});
function bindVolumeIndicators(connection) {
connection.volume(function(inputVolume, outputVolume) {
var inputColor = 'red';
if (inputVolume < .50) {
inputColor = 'green';
} else if (inputVolume < .75) {
inputColor = 'yellow';
}
inputVolumeBar.style.width = Math.floor(inputVolume * 300) + 'px';
inputVolumeBar.style.background = inputColor;
var outputColor = 'red';
if (outputVolume < .50) {
outputColor = 'green';
} else if (outputVolume < .75) {
outputColor = 'yellow';
}
outputVolumeBar.style.width = Math.floor(outputVolume * 300) + 'px';
outputVolumeBar.style.background = outputColor;
});
}
function updateAllDevices() {
updateDevices(speakerDevices, Twilio.Device.audio.speakerDevices.get());
updateDevices(ringtoneDevices, Twilio.Device.audio.ringtoneDevices.get());
}
});
// Update the available ringtone and speaker devices
function updateDevices(selectEl, selectedDevices) {
selectEl.innerHTML = '';
Twilio.Device.audio.availableOutputDevices.forEach(function(device, id) {
var isActive = (selectedDevices.size === 0 && id === 'default');
selectedDevices.forEach(function(device) {
if (device.deviceId === id) { isActive = true; }
});
var option = document.createElement('option');
option.label = device.label;
option.setAttribute('data-id', id);
if (isActive) {
option.setAttribute('selected', 'selected');
}
selectEl.appendChild(option);
});
}
// Activity log
function log(message) {
var logDiv = document.getElementById('log');
logDiv.innerHTML += '<p>&gt;&nbsp;' + message + '</p>';
logDiv.scrollTop = logDiv.scrollHeight;
}
// Set the client name in the UI
function setClientNameUI(clientName) {
var div = document.getElementById('client-name');
div.innerHTML = 'Your client name: <strong>' + clientName +
'</strong>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment