Skip to content

Instantly share code, notes, and snippets.

@aylarov
Created March 7, 2016 09:53
Show Gist options
  • Save aylarov/be8f2ecee676307119af to your computer and use it in GitHub Desktop.
Save aylarov/be8f2ecee676307119af to your computer and use it in GitHub Desktop.
VoxImplant Web SDK: click to call
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://cdn.voximplant.com/voximplant.min.js"></script>
<script type="text/javascript">
var initialized = false,
loggedIn = false,
connected = false,
voxAPI = VoxImplant.getInstance();
// Add event listeners
voxAPI.addEventListener(VoxImplant.Events.SDKReady, handleSDKReady);
voxAPI.addEventListener(VoxImplant.Events.ConnectionEstablished, handleConnectionEstablished);
voxAPI.addEventListener(VoxImplant.Events.AuthResult, handleAuthResult);
function handleSDKReady() {
initialized = true;
logMessage("VoxImplant SDK ready");
// Connection with VoxImplant Cloud can be established now
voxAPI.connect();
}
function handleConnectionEstablished() {
connected = true;
logMessage("Connection established");
login();
}
function handleAuthResult(e) {
logMessage("AuthResult: "+e.result);
if (e.result) {
// Logged in successfully
loggedIn = true;
makeCall();
} else {
logMessage("Authorization failed. Please specify correct username and password");
}
}
function login(){
// Authorization required before we can use other functions
voxAPI.login("USERNAME@APPNAME.ACCNAME.voximplant.com", "USERPASSWORD");
}
function logMessage(msg) {
document.getElementById("log").innerHTML += msg + "\n";
}
function makeCall(){
// Specify your number here - Application Rule Pattern describes which VoxEngine scenario should be launched
var call = voxAPI.call("SOME NUMBER");
// Add event listeners for call events
call.addEventListener(VoxImplant.CallEvents.Connected, handleCallConnected);
call.addEventListener(VoxImplant.CallEvents.Failed, handleCallFailed);
call.addEventListener(VoxImplant.CallEvents.Disconnected, handleCallDisconnected);
}
function handleCallConnected() {
logMessage("Call Connected");
}
function handleCallFailed(e) {
logMessage("Call Failed. Code: "+e.code+" Reason: "+e.reason);
}
function handleCallDisconnected() {
logMessage("Call Disconnected");
}
function testCall() {
// Initialize SDK if not it's not initialized yet
if (!initialized) voxAPI.init({micRequired: true});
else {
// Establish connection with VoxImplant Cloud if it's not established yet
if (!voxAPI.connected()) voxAPI.connect();
else {
// Login if not logged in yet, otherwise - make a call
if (!loggedIn) login();
else makeCall();
}
}
}
</script>
</head>
<body>
<a href="javascript:testCall()">Make Call</a><br/>
<textarea style="height: 400px" id="log"></textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment