Skip to content

Instantly share code, notes, and snippets.

@aylarov
Created July 19, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aylarov/03393f30de431a7a430ab244f37e4a16 to your computer and use it in GitHub Desktop.
Save aylarov/03393f30de431a7a430ab244f37e4a16 to your computer and use it in GitHub Desktop.
VoxEngine Scenarios of Zoom bridge. ZoomBrindge creates Voximplant conference and connect it to Zoom conference + processes Call-Ins/Call-Outs
require(Modules.Conference);
var customData,
confName,
conference,
pstnCalls = [];
/**
* Handle StartConference request
*/
VoxEngine.addEventListener(AppEvents.Started, function (e) {
// We will send some parameters in customData
try {
customData = JSON.parse(VoxEngine.customData());
} catch (e) {
Logger.write("Something went wrong. Broken JSON. Life is pain. Please try again.");
}
/**
* Conference ID
*/
if (customData !== undefined && confName === undefined) confName = customData.conference;
/**
* Display Name
*/
if (customData !== undefined && customData.displayName !== undefined) displayName = customData.displayName;
else displayName = "Untitled";
// Create conference
conference = VoxEngine.createConference();
if (confName !== undefined) callZoomConference(confName);
});
/**
* Handle Call-In
*/
VoxEngine.addEventListener(AppEvents.CallAlerting, function (e) {
if (e.headers["X-conference"] !== undefined) {
// If we haven't set conference id yet (conference session started by inbound call instead of HTTP API call)
if (confName === undefined) {
confName = e.headers["X-conference"];
callZoomConference(confName);
}
e.call.addEventListener(CallEvents.Connected, function (ev) {
VoxEngine.sendMediaBetween(ev.call, conference);
});
e.call.answer();
}
});
/**
* Make outbound SIP call to Zoom
*/
function callZoomConference(conf) {
var call = VoxEngine.callSIP("sip:" + conf + "@zoomcrc.com", {
callerid: "voximplant",
displayName: displayName,
video: true
});
call.addEventListener(CallEvents.Connected, function (e) {
// When connected to Zoom - send media between our conference and Zoom
VoxEngine.sendMediaBetween(conference, call);
// Handle commands for PSTN Call-out
// To initiate call-out use the URL that looks as follows: media_session_access_url/CallOut/phonenumber
VoxEngine.addEventListener(AppEvents.HttpRequest, function (e) {
//Logger.write("HTTP REQUEST:");
//Logger.write(e.path);
var path = e.path.split("/"),
num;
if (path[path.length - 2] == "CallOut") {
num = path[path.length - 1];
handleCallOut(num);
return '{"result":"callingto:' + num + '"}';
}
});
});
// If the connection with Zoom has been lost - destroy the conference
call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
}
/**
* Make PSTN call-out
*/
function handleCallOut(num) {
var pstn = VoxEngine.callPSTN(num);
pstn.addEventListener(CallEvents.Connected, function (e) {
pstnCalls.push(e.call);
VoxEngine.sendMediaBetween(pstn, conference);
});
pstn.addEventListener(CallEvents.Disconnected, function (e) {
if (pstnCalls.indexOf(e.call) > -1) pstnCalls.splice(pstnCalls.indexOf(e.call), 1);
});
pstn.addEventListener(CallEvents.Failed, function (e) {
// Couldn't reach the callee
});
}
/**
* Conference gatekeeper
*/
var conference = "", inCall, confCall, callerId, displayName;
VoxEngine.addEventListener(AppEvents.CallAlerting, function(e) {
callerId = e.callerid;
displayName = e.displayName;
inCall = e.call;
e.call.addEventListener(CallEvents.Connected, onCallConnected);
e.call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
e.call.answer();
});
function onCallConnected(e) {
//enable dtmf processing
e.call.handleTones();
e.call.addEventListener(CallEvents.ToneReceived, handleToneEntered);
e.call.say("Hi, welcome to Voximplant Connector, please enter Zoom conference number", Language.US_ENGLISH_FEMALE);
}
function handleToneEntered(e) {
e.call.stopPlayback();
e.call.say(e.tone, Language.US_ENGLISH_FEMALE);
conference += e.tone;
if (conference.length == 9) {
confCall = VoxEngine.callConference(conference, callerId, displayName, {"X-conference": conference});
confCall.addEventListener(CallEvents.Connected, onConfCallConnected);
confCall.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
confCall.addEventListener(CallEvents.Failed, VoxEngine.terminate);
}
}
function onConfCallConnected(e) {
VoxEngine.sendMediaBetween(inCall, confCall);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment