4-2で作成したカスタム CCP の機能追加例として、Mute/Unmute ボタンを追加します。
<tr>
<td>
<button type="button" id="ansCall" value="Answer Call">Answer Call</button>
</td>
<td>
<button type="button" id="dropCall" value="Drop Call">Drop Call</button>
</td>
<td>
<button type="button" id="mute" value="Mute">Mute</button>
</td>
<td>
<button type="button" id="unmute" value="Unmute">Unmute</button>
</td>
</tr>
var muteButton = document.getElementById("mute");
var unmuteButton = document.getElementById("unmute");
subscribeToContactEvents 関数と間違えないようにご注意ください。
function subscribeToAgentEvents(agent) {
// Subscribing to Streams API Long Polling Agent events
window.myCPP.agent = agent;
agent.onRefresh(handleAgentRefresh);
agent.onRoutable(handleAgentRoutable);
agent.onNotRoutable(handleAgentNotRoutable);
agent.onOffline(handleAgentOffline);
agent.onStateChange(handleAgentStateChange);
// Set the DOM button elements behaviour
$("#setReady").click(() => {
goAvailable();
});
$("#setOffline").click(() => {
goOffline();
});
$("#dropCall").click(() => {
disconnectContact();
});
$("#ansCall").click(() => {
acceptContact();
});
$("#mute").click(() => {
goMute();
});
$("#unmute").click(() => {
goUnmute();
});
}
muteButton.style.backgroundColor = '#FFFFFF';
muteButton.style.color = '#000000';
unmuteButton.style.backgroundColor = '#FFFFFF';
unmuteButton.style.color = '#000000';
muteButton.disabled = true;
unmuteButton.disabled = true;
muteButton.style.backgroundColor = '#FF0000';
muteButton.style.color = '#FFFFFF';
unmuteButton.style.backgroundColor = '#FF0000';
unmuteButton.style.color = '#FFFFFF';
muteButton.disabled = false;
unmuteButton.disabled = false;
function acceptContact() {
// Streams API call to Accept the Incoming Contact
window.myCPP.contact.accept({
success: function () {
muteButton.style.display ="inline-block";
unmuteButton.style.display ="none";
console.log('Accepted via Streams');
},
failure: function () {
console.log('Failed to establish connection via Streams');
}
});
}
function disconnectContact() {
// Streams API call to Drop a Connected Contact
window.myCPP.contact.getAgentConnection().destroy({
success: function () {
muteButton.style.display ="inline-block";
unmuteButton.style.display ="inline-block";
console.log('Contact disconnected via Streams');
},
failure: function () {
console.log('Failed to disconnect the contact via Streams');
}
});
}
function goMute() {
window.myCPP.agent.mute();
muteButton.style.display ="none";
unmuteButton.style.display ="inline-block";
console.log("muted");
}
function goUnmute() {
window.myCPP.agent.unmute();
muteButton.style.display ="inline-block";
unmuteButton.style.display ="none";
console.log("unmuted");
}
このラボで更新したファイルのサンプルはこちらです。