Ozeki PBX VoIP client

Connect your applications through an API extension, with the help of OPS SDK, is also possible with Ozeki Phone System. With an API extension you are free to build call assistants, call centre clients, IVRs or any custom Voice or Messaging application. This guide shows you, how to make calls and connect applications to your PBX.

Download example from nuget.org: http://www.nuget.org/packages/ozeki.pbx.voip.client/

Prerequisites

Install nuget package manager

With Installing the NuGet Package Manager, you can download and easily update the project (OPS SDK and the corresponding sample program). The NuGet is available for Visual Studio 2010, 2012.

Install to Visual Studio 2010:

An extension can be installed into the Professional, Premium, and Ultimate editions. First of all select the ’Tools’ menu and ’Extension Manager…’. In Extension Manager select Online Gallery, and click on the ’Search Online Gallery’ field in the upper right corner. Then type in the NuGet word. After finding the ‘NuGet Package Manager’ extension, click to the ’Download’ button next to it, and follow the installation steps (Figure 1).

download nuget package manager
Figure 1 - Download the NuGet Package Manager

Install to Visual Studio 2012:

The NuGet is included in every edition (except Team Foundation Server) by default. Updates of NuGet can be found through the Extension Manager. You can check whether your copy of Visual Studio already has the NuGet extension, look for Library Package Manager in the Tools menu of Visual Studio. If your copy of Visual Studio has not got the Library Package Manager (NuGet) extension yet, you can install it by using the Extension Manager.

Download project

After you have installed the NuGet Package Manager, you can download the project by Package Manager Console. Select the ’Tools’ menu -> ’Library Package Manager’ -> ’Package Manager Console’ (Figure 2).

opening of package manager console
Figure 2 - Opening of the Package Manager Console

In the Package Manager Console execute the following command:

Install-Package ozeki.pbx.voip.client

Code Example 1 - Installing the project from Package Manager Console

In the Solution Explorer, you can see the added new OPSSDK Reference, and the two added class file (CallHandlerSample.cs, TestCall.cs).

Write the example

You have downloaded, and installed the project. For manage making and receiving calls, you can open the CallHandlerSample.cs file. In the constructor of the class, you will see the following lines:

public CallHandlerSample(string serverAddress, string username, string password, string apiExtensionId = null)
{
if (!TryCreateConnectToClient(serverAddress, username, password)) 
return;
if (!TrySetApiExtension(apiExtensionId)) 
return;
}

Code Example 2 - Constructor of the CallHandlerSample

When you create the CallHandlerSample class you have to add the address of PBX server, and a valid Username with a Password. The TryCreateConnectToClient method is for creating an OPSClient, subscribing to the connection events, and trying to login with the given parameters. The TrySetApiExtension method can be used to try to get API Extension object from Ozeki Phone System through OPSClient. If you added a concrete API ID then the method is trying to get this API extension object from Ozeki Phone System, elsewhere use the default SYSTEM API Extension. The method is subscribing to the API Extension events also.

Outgoing Calls

It is possible to execute the Call method, trougth CallHandlerSample calls, that method looks like this:

public void Call(string dialedNumber)
{
CreateCall(dialedNumber);
} 

private void CreateCall(string dialedNumber)
{
if (apiExtension == null)
return;

if (call != null)
{
Console.WriteLine("A call already in progress. Cannot handle another call until that.");
return;
}

call = apiExtension.CreateCall(dialedNumber);
SubscribeCallEvents();

Console.WriteLine("Outgoing call (" + call.OtherParty + ") started.");
call.Start();
}

Code Example 3 - Method for Call dialled number, and the CreateCall method

As you can see, if the apiExtension object is null, so you cannot create call. Else, the calling is executed by the Api Extension with the given dialedNumber parameter (if there is not another call in progress). After you started the call, the CallStateChanged event will be triggered. If the actual state is InCall, then the call is picked up on the other side. After connecting the microphone and the speaker to the call you can communicate with the other party. The actual state is Completed, which means that the call is ended, so unsubscribe the call events, disconnect the attached devices and set null value to the call object (for possibility to receive or make another calls later). For more information about CallStates please visit this page.

private void call_CallStateChanged(object sender, VoIPEventArgs e)
{
if (e.Item.IsInCall())
{
Console.WriteLine("In call with " + call.OtherParty + ".");
ConnectDevicesToCall();
}
else if (e.Item.IsCallEnded())
{
UnsubscribeCallEvents();
DisconnectDevicesFromCall();
Console.WriteLine("Outgoing call (" + call.OtherParty + ") ended.");

call = null;
}
}

Code Example 4 - Method for handling the CallStateChanged event

The CallErrorOccured event is the other event for the call. This can be triggered, when the call cannot be created (e.g. dialled number is not available, or the phone line is busy etc.) or an exception is occurred during the call.

Incoming Calls

When somebody calls the connected API Extension then the IncomingCall event will be triggered:

private void apiExtension_IncomingCall(object sender, VoIPEventArgs e)
{
if (call != null)
{
e.Item.Reject();
Console.WriteLine("A call already in progress. Cannot handle another call until that.");
return;
}

call = e.Item;

if (call.CallState == CallState.Ringing)
{
SubscribeCallEvents();
OnIncomingCallReceived(e.Item);
}
}

Code Example 5 - Method for handling the IncomingCall event

If another call is in progress, then the new incoming call will be rejected. Elsewhere, set the call object (e.Item) to this call Class field, subscribe the call events and forward the call object to the IncomingCallReceived Class event. With these steps, you can subscribe that event like this:

public static void Main(string[] args)
{
_callHandlerSample = new CallHandlerSample("localhost", "admin", "12345", "9000"); 

...

_callHandlerSample.IncomingCallReceived += callHandlerSample_IncomingCallReceived;

...
}

private static void callHandlerSample_IncomingCallReceived(object sender, VoIPEventArgs<OPSSDK.ICall> e)
{
Console.WriteLine("Incoming call (" + e.Item.OtherParty + ") accepted.");
e.Item.Accept();
}

Code Example 6 - Instantiation the CallHandlerSample, and accept the incoming call

All of the incoming calls are accepted as you can see in the example code above. You are free to choose, whether to reject the call, or transfer it (or do other actions).

Of course you can use the full SDK of Ozeki Phone System as you wish. The making and receiving call is just a simple sample from that.

Test the example

If you want to test your application, login to your Ozeki Phone System. Add Extension on the Home screen by selecting ‘Add’ button in the right hand side (Extensions) of the page (Figure 3):

add extension
Figure 3 - Add Extension

After that, select the API Extension from the list, and click to the ‘Install’ button (Figure 4):

installing api extension
Figure 4 - Installing API Extension

Then notice the ID of the API Extension, and click ‘OK’ to save it. After that, register a Soft Phone for testing purpose. If the registered Soft Phone is ready, open your Visual Studio, and install the project if you have not do that before. Open the TestCall.cs source file. Change the parameters of the methods to yours and also the called number:

_callHandlerSample = new CallHandlerSample("localhost", "admin", "12345", "9000");
_callHandlerSample.Call("1001");

Code Example 7 - Instantiate of CallHandlerSample, and create call

You can read some information about parameters in the constructor of the CallHandlerSample class. Set called number parameter of Call method to that number what your registered in your PBX before. After you have done with actualizing of parameters, you can run the project. Press F5 to do that. If your settings were correct, your Soft Phone (what you have registered into the PBX by manually) will be called. You can test the other way, by calling the API Extension with the test Soft Phone.

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

Dig deeper!
People who read this also read...

More information