How to route calls using the built in JavaScript API interceptor

If you want to use more complicated dial plans than the ones you can set in the Dial plan configuration page, then the built in JavaScript API interceptor is the tool that you are looking for. With it's help you can specify yourself how the calls should be routed.

This guide is going to show how you can use the interceptor and what programming tools are available for its configuration. For this purpose I recommend that you look through the JavaScript API routing interceptor reference before you begin with this guide.

Step 1: Login to Ozeki Phone System

First of all, open Ozeki Phone System and log in with your username and password.

login page
Figure 1 - Login page

Step 2: Open the page of JavaScript API

After you have logged in to Ozeki Phone System, select the Productivity menu's JSP API submenu, as you can see on Figure 2:

js api
Figure 2 - JS API

Step 3: Open the routing interception

Click on the Routing Interception on the left side of the JavaScript API page.

routing interception
Figure 3 - Routing interception

Step 4: Set the dial plan interceptor

You will find a default example configuration in the dial plan interceptor page. This guide is going to help you to understand it better, by showing you some basic examples of JavaScript API interceptor, before explaining the default example configuration.

The first example is going to be the 'Destination' class, which is used for routing a call to a specific extension as Figure 4 and Code example 2 shows:

how the destination class works
Figure 4 - How the destination class works

		return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '2000', 10);

Code example 1 - Destination(callerId, dialedNumber, extensionId, ringTime) class example

With this single line, the Dial plan interceptor will rout every call to the extension "2000". The Destination class's parameteres are the callerId, dialedNumber, extensionId and the ringTime.

If you would like to use the original Dial plan settings when some conditions are met, then you can do it with the following code:

return null;

Code example 2 - Return null example

Routing interceptors are being called before the Dial Plans. With returning from the interceptor with a "null" as value, you indicate to the PBX, that the call can be passed to the Dial Plans (if there is any), becouse the interceptor has nothing to do with it.

To set a simple "if" condition, you can check the following code:

if(routeInfo.CallerId == '500')
  return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '2000', 10);

Code example 3 - If condition example

As you can see, all of the calls from extension "500" will be routed to extension "2000", regardless of the original dialed numbers.

Before you begin to study the default example code (which you would find in Ozeki Phone System by default as well), it's highly recommended to understand the JavaScript API interceptor reference first.

The default example code routes the calls by country codes:

/**
* Example to route incoming calls by country code
*/

//Get the country code information from the callerid.
var phoneCountryCode = CountryCodeService.getCountryCodeByPhoneNumber(routeInfo.CallerId);

//If no country code information found, route to 9997
if(phoneCountryCode == null) {
	return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9997', 10);	
}

console.log('Country code settings found');
//If the phone number originates from Germany, route to extension 9999.
if(phoneCountryCode.Alpha2CountryCode == 'DE') {
    console.log('routing to 9999');
	return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9999', 10);
}
//If the phone number originates from the United States, route to extension 9998.
else if(phoneCountryCode.Alpha2CountryCode == 'US') {
    console.log('routing to 9998');
	return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9998', 10);
}

//You can see a full list of ISO alpha 2 country codes here:
//http://www.nationsonline.org/oneworld/country_code_list.htm
//Or you can use the 'Country' property for the full country name
//Or the 'Language' property for the primary language in the specified country.

//Route to 9997 by default
console.log('Using default route destination 9997');
return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9997', 10);

Code example 4 - Routing by country code

The country code is being stored in a newly created implicit type variable (which means, it's type will be determined by the compiler). To get the country code, the interceptor is using the "CountryCodeService" class's "getCountryCodeByPhoneNumber()" method. This method gets the country code by the phone number, and since the interception would like to get the country code of the dialed party, it will use the "routeInfo.CallerId" as its parameter.

  • If the country code equals to "null", the call will be routed to extension "9997", and the interceptor is done with processing the current call.
  • If the country code is not equal to "null", than it will be logged to the console, as "Country code settings found", and the interceptor will check the followings:
    • If the value of "phoneCountryCode.Aplha2CountryCode" equals to "DE", that means the call is from Germany, and it will be routed to extension "9999",
    • else, if the value exuals to "US", that means it comes from the United States of America, and the call will be routed to extension "9998".
    • In the case of any other country codes, the call will be routed to extension "9997".

The second part of the code, which is commented out by default:

/**
* Example to route incoming calls by extension
*/

//Get the extension information from the dialed number.
var extension = ExtensionInfoService.getExtensionById(routeInfo.DialedNumber);

//If no extension found, route to 9997.
if(extension == null) {
	return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9997', 10);
}
//If the called extension is a SIP extension
else if(extension.ExtensionConnectionType == 'Extension' && extension.ExtensionType == 'SIP extension') {
	//If the called extension is 9998, route to 9999 instead.
	if(extension.ExtensionId == '9998') {
		return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9999', 10);
	}
	//If the called extension is 9999, route to 9998 instead.
	else if(extension.ExtensionId == '9999') {
		return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9998', 10);
	}
} 

//Route to 9997 by default
return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9997', 10);

Code example 5 - Routing by extension

Similar to the first example, this code begins its job with getting information about the dialed extension, with the help of the "ExtensionInfoService" class's "getExtensionById()" method. This method gets the extension by its ID, so the interception uses the "routInfo.DialedNumber" as its parameter.

  • If the information about the extension is equals to "null", the interceptor routes the call to extension "9997".
  • If the dialed number is a SIP extension, the interceptor will check the following options:
    • If extension "9998" is being called, the call will be routed to extension "9999",
    • else, if extension "9999" is being called, the call will be routed to extension "9998".
  • In any other cases, the call will be routed to extension "9997".

The last part of the example code, which is also commented out by default:

/**
* Example to route incoming calls by user.
*/

//Get the user information from the dialed number.
var user = UserInfoService.getUserByDevice(routeInfo.DialedNumber);

//If no user found, route to 9997
if(user == null) {
	return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9997', 10);
}

//If the called extension belongs to testuser1, route to 9998 instead.
if(user.Username == 'testuser1') {
	return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9998', 10);
}

//Route to 9997 by default
return new Destination(routeInfo.CallerId, routeInfo.DialedNumber, '9997', 10);

Code example 6 - Routing by user

First, the interceptor gets information about the dialed number's user, with the help of the "UserInfoService" class's "getUserByDevice()" method. This method gets the user of a selected device, so the interceptor uses the "routInfo.DialedNumber" parameter, ot get the dialed number's user.

  • If no user has been found, the call will be routed to extension "9997".
  • If the user's Username is "testuser1", than the call will be routed to extension "9998".
  • In any other cases, the call will be routed to extension "9997".

To save these settings click on the "Save" button at the bottom of the page. With this, you have allowed the example call routing configuration and you can test it with three extensions: Register two SIP devices with optional numbers and one with the extension number "9997". Call up one of the devices from the other and because the local extensions dont have country codes, the call will be routed to extension "9997".

If you have any questions or need assistance, please contact us at info@ozekiphone.com

More information