Skip to content

Instantly share code, notes, and snippets.

@rfprod
Created June 2, 2017 15:16
Show Gist options
  • Save rfprod/36577534458122285cb86f091ffbd176 to your computer and use it in GitHub Desktop.
Save rfprod/36577534458122285cb86f091ffbd176 to your computer and use it in GitHub Desktop.
Voximplant Scenarios
/*
* basic redirect to SIP
* listens for call event and redirects all calls to the same sip number
*/
VoxEngine.addEventListener(AppEvents.CallAlerting, function(event) {
var PBXoptions = {
server: {
ip: 'xxx.xxx.xxx.xxx'
},
sip: {
login: 'your-sip-login',
password: 'your-sip-password'
}
};
// incoming call event properties
var incomingCall = event.call; // Call: Incoming call that triggered the event
var callerId = event.callerid; // String: CallerID for current call
var customData = event.customData; // Optional String: Custom data that was passed from client with the call
var destination = event.destination; // String: Dialed number
var displayName = event.displayName; // String: Displayable name of the caller
var fromURI = event.fromURI; // String: CallerID with domain or SIP URI for incoming SIP call
var headers = event.headers; // Object: Custom SIP headers received with the call (ones starting with "X-")
var name = event.name; // String: The name of the event - "Application.CallAlerting"
var toURI = event.toURI; // String: Dialed SIP URI
// VoxEngine.callSIP(to, callerid, displayName, password, authUser, extraHeaders, video, outProxy)
// to - String: SIP URI to make call to
// callerid - String (optional): CallerID that will be displayed to called user. If not specified, "[email protected]" string will be sent, where "appname" is the Voximplant application name and "accname" is the Voximplant account name.
// displayName - String (optional): Name of calling user, that will be displayed to called user
// password - String (optional): Password for SIP Authentication
// authUser - String (optional): Username for SIP Authentication. If not specified, callerid is used as username for authentication
// extraHeaders - Object (optional): Optional custom parameters (SIP headers) that should be passed with call (INVITE) message. Parameter names must start with "X-" to be processed by application
// video - Boolean (optional): Specifies if call should have video support. Please note that price for audio-only and video calls is different!
// outProxy - String (optional) - Specifies outbound proxy
var sipOpts = {
to: 'sip:' + PBXoptions.sip.login + '@' + PBXoptions.server.ip,
callerid: callerId,
displayName: displayName,
password: PBXoptions.sip.password,
authUser: PBXoptions.sip.login,
extraHeaders: {},
video: false,
outProxy: null
};
var sipCall = VoxEngine.callSIP(sipOpts.to, sipOpts.callerid, sipOpts.displayName, sipOpts.password, sipOpts.authUser, sipOpts.extraHeaders, sipOpts.video, sipOpts.outProxy);
VoxEngine.sendMediaBetween(incomingCall, sipCall); // Start sending media from mediaUnit1 to mediaUnit2 and vice versa
VoxEngine.easyProcess(incomingCall, sipCall); // Adds all default event listeners to pass signaling information between two calls
/* comment this section if it is not required to release voximplant virtual telephone number after redirection */
var forwardOptions = {
onEstablishedCallback: function(incoming, outgoing) {
Logger.write(' >>> forwardCallToSIP, onEstablishedCallback()');
var key;
if (typeof incoming === 'object') { for (key in incoming) { Logger.write(' > incoming' + '[' + key+ ']: ' + incoming[key]); } }
else { Logger.write(' >>>>>> incoming call: ' + incoming); }
if (typeof outgoing === 'object') { for (key in outgoing) { Logger.write(' > outgoing' + '[' + key+ ']: ' + outgoing[key]); } }
else { Logger.write(' >>>>>> outgoing call: ' + outgoing); }
},
video: false
};
VoxEngine.forwardCallToSIP(forwardOptions.onEstablishedCallback, forwardOptions.video); // forward call to Sip
/* comment this section if it is not required to release voximplant virtual telephone number after redirection */
});
/*
* extended redirect to SIP
* listens to call event
* intercepts an incoming call which is already redirected from physical phone to virtual phone number
* redirects an incoming redirected call to sip account associated with original destination retrieved from original call headers
*/
VoxEngine.addEventListener(AppEvents.CallAlerting, function(event) {
var PBXoptions = {
server: {
ip: 'xxx.xxx.xxx.xxx'
},
sip: {
to: 'callee-sip-login',
login: 'your-sip-login',
password: 'your-sip-password'
}
};
// incoming call event properties
var incomingCall = event.call; // Call: Incoming call that triggered the event
var callerId = event.callerid; // String: CallerID for current call
var customData = event.customData; // Optional String: Custom data that was passed from client with the call
var destination = event.destination; // String: Dialed number
var displayName = event.displayName; // String: Displayable name of the caller
var fromURI = event.fromURI; // String: CallerID with domain or SIP URI for incoming SIP call
var headers = event.headers; // Object: Custom SIP headers received with the call (ones starting with "X-")
var name = event.name; // String: The name of the event - "Application.CallAlerting"
var toURI = event.toURI; // String: Dialed SIP URI
// resolve caller SIP number
var url = 'http://your-server.domain/api/resolve-sip-acc?';
url += 'callerId=' + callerId + '&';
url += 'customData=' + customData + '&';
// url += 'destination=' + destination + '&'; // bad destination, it is virtual number not the callee
url += 'destination=' + headers.Diversion.match(/[0-9+]/)[0] + '&'; // real destination, not the one with virtual number, which is strored in destination variable
url += 'displayName=' + displayName + '&';
url += 'fromURI=' + fromURI + '&';
url += 'name=' + name + '&';
url += 'toURI=' + toURI + '&';
url += 'token=xD9VCSF9yetRX7sXefZRCVh4MQQgCD44aBe2D2PA';
Net.httpRequestAsync(url).then(function(response) {
/*
* if this example response should contain stringified object ad text { id: 'callee-sip-id' }
*/
/* DEBUG response */
Logger.write(' >>>> resolve callee SIP number');
for (var key in response) { Logger.write(' > response' + '[' + key+ ']: ' + response[key]); }
var resJSON = JSON.parse(response.text);
if (!resJSON.id) {
event.call.startEarlyMedia();
event.call.say("There is no SIP account for this user", Language.US_ENGLISH_FEMALE);
} else {
Logger.write('resolved sip login: ' + resJSON.id);
PBXoptions.sip.to = resJSON.id;
// VoxEngine.callSIP(to, callerid, displayName, password, authUser, extraHeaders, video, outProxy)
// to - String: SIP URI to make call to
// callerid - String (optional): CallerID that will be displayed to called user. If not specified, "[email protected]" string will be sent, where "appname" is the Voximplant application name and "accname" is the Voximplant account name.
// displayName - String (optional): Name of calling user, that will be displayed to called user
// password - String (optional): Password for SIP Authentication
// authUser - String (optional): Username for SIP Authentication. If not specified, callerid is used as username for authentication
// extraHeaders - Object (optional): Optional custom parameters (SIP headers) that should be passed with call (INVITE) message. Parameter names must start with "X-" to be processed by application
// video - Boolean (optional): Specifies if call should have video support. Please note that price for audio-only and video calls is different!
// outProxy - String (optional) - Specifies outbound proxy
var sipOpts = {
to: 'sip:' + PBXoptions.sip.to + '@' + PBXoptions.server.ip,
callerid: callerId,
displayName: displayName,
password: PBXoptions.sip.password,
authUser: PBXoptions.sip.login,
extraHeaders: {
'X-NUMBER': callerId
},
video: false,
outProxy: null
};
var sipCall = VoxEngine.callSIP(sipOpts.to, sipOpts.callerid, sipOpts.displayName, sipOpts.password, sipOpts.authUser, sipOpts.extraHeaders, sipOpts.video, sipOpts.outProxy);
VoxEngine.sendMediaBetween(incomingCall, sipCall); // Start sending media from mediaUnit1 to mediaUnit2 and vice versa
VoxEngine.easyProcess(incomingCall, sipCall); // Adds all default event listeners to pass signaling information between two calls
/* comment this section if it is not required to release voximplant virtual telephone number after redirection */
var forwardOptions = {
onEstablishedCallback: function(incoming, outgoing) {
Logger.write(' >>> forwardCallToSIP, onEstablishedCallback()');
var key;
if (typeof incoming === 'object') { for (key in incoming) { Logger.write(' > incoming' + '[' + key+ ']: ' + incoming[key]); } }
else { Logger.write(' >>>>>> incoming call: ' + incoming); }
if (typeof outgoing === 'object') { for (key in outgoing) { Logger.write(' > outgoing' + '[' + key+ ']: ' + outgoing[key]); } }
else { Logger.write(' >>>>>> outgoing call: ' + outgoing); }
},
video: false
};
VoxEngine.forwardCallToSIP(forwardOptions.onEstablishedCallback, forwardOptions.video); // forward call to Sip
/* comment this section if it is not required to release voximplant virtual telephone number after redirection */
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment