C# .NET VoIP developers 101

Part 2: C# example on recording VoIP calls, call routing

Get more information about how to do special tasks using much more advanced functionalities developed in C#.NET. This guide demonstrates how to log calls, how to record the conversations and what you need to do for call routing.

Download

C# example on recording VoIP calls, call routing
  1. Download Call Recording example: OPSCallRecording_source.zip
  2. Download Dynamic Call Routing example: OPSRoutingInterception_source.zip
  3. Download Configuration Modification example: OPSConfiguration_source.zip
  4. Reference manual: http://www.ozekiphone.com/examples/doc/

Introduction

On the previous page the first part of C#.NET VoIP developers 101 could be read. That guide presented the prerequisites of doing basic tasks, such as SMS sending/receiving or making/receiving a call using your own C# application. This article focuses on far more advanced features, like call logging, call recording and call routing.

Part 1: C# commands on sending SMS, making VoIP calls

Connecting to PBX

First of all, make an OpsClient department. You can sign in, if you import the Login function. It has three parameters: first the IP address of the server, the second is the username and the third is the password. You can check whether the login was successful by looking at the return value of Login function. You can check in to the PBX as the default admin user. You can add new users at the Connections menu, Office users menu item, selecting the Add user option. Whether a user is able to sign in through API, depends on the value of his user profile. You can create more user profiles by using a Security form at the PBX features menu, Preferences menu item. Enabling is possible using the Allow this user to configure the PBX through the API option.

1. Log calls

To log calls after a successful login, subscribe to the SessionCreated event, which changes call receiving. VoIPEventArgs type parameter contains the active calls. Through the Item attribute of this parameter you can reach the call, which caused the event. With this, you can query the information about the call or connect AudioSender and AudioReceiver. These make it possible to talk to clients separately at the same time, call logging and playing an audio file.

void Initialize()
{
	var opsClient = new OpsClient();
	var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345");

	opsClient.SessionCreated += new EventHandler<Ozeki.VoIP.VoIPEventArgs<OPSSDK.ISession>>(opsClient_SessionCreated);
}

static void opsClient_SessionCreated(object sender, Ozeki.VoIP.VoIPEventArgs<OPSSDK.ISession> e)
{
	Console.Clear();
	Console.WriteLine("SessionID: " + e.Item.SessionID);
	Console.WriteLine("Call direction: " + e.Item.CallDirection);
	Console.WriteLine("Caller: " + e.Item.Caller);
	Console.WriteLine("Callee: " + e.Item.Callee);
	Console.WriteLine("Ring duration: " + e.Item.RingDuration);
	Console.WriteLine("State: " + e.Item.State);
	Console.WriteLine("State duration: " + e.Item.StateDuration);
	Console.WriteLine("Start time: " + e.Item.StartTime);
	Console.WriteLine("Talk duration: " + e.Item.TalkDuration);
}
	

Code 1 - C# code to call logging

2. Record calls

Call recording happens with the SessionCreated and SessionsCompleted events. SessionCreated events start when you receive a call. At this time you connect an AudioReceiver to the call, which you can check by importing the ConnectAudioReceiver function. With the first parameter you can choose which voice you would like to record, second parameter is a Recorder object. It can be MP3StreamRecorder when you create an MP3 file, or WaveStreamRecorder if data is stored in a wav file. You can listen in to a certain call by connecting a speaker, and it is also possible to have a say in the call - as a third party - by connecting your microphone. The constructor of both classes is waiting for the path of the file. You can start your recording by the StartStreaming method. When the call is over, SessionCompleted event starts. Stop the previously started Streaming at SessionCreated. Using the Dispose() method of the recorder object.

public class Program
{
    public void static void Main(string[] args)
    {
        OpsClient opsClient = new OpsClient();
        var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345");

        opsClient.SessionCreated += new EventHandler>(opsClient_SessionCreated);
        opsClient.SessionCompleted += new EventHandler>(opsClient_SessionCompleted);
    }

    static void client_SessionCreated(object sender, Ozeki.VoIP.VoIPEventArgs e)
    {
	    try{
			var mp3Recorder = currentCalls.GetOrAdd(session, (s) => new MP3StreamRecorder(string.Format("{0}_{1}_{2}.mp3", session.CallerId, session.Destination, session.SessionID)));
			session.ConnectAudioReceiver(CallParty.All, mp3Recorder);
			
			//attach speaker to call
			//var speaker = Speaker.GetDefaultDevice();
			//session.ConnectAudioReceiver(CallParty.All, speaker);
			//speaker.Start();
			
			//attach microphone to call
			//var microphone = Microphone.GetDefaultDevice();
			//session.ConnectAudioSender(CallParty.All, microphone); // you can select which call party can hear your voice with the CallParty parameter
			//microphone.Start();
			
			mp3Recorder.StartStreaming();
		}catch(Exception ex)
		{
			Console.WrinteLine(ex.Message);
		}

    }

    static void client_SessionCompleted(object sender, Ozeki.VoIP.VoIPEventArgs e)
    {
        MP3StreamRecorder recorder;
        if(currentCalls.TryGetValue(session, out recorder))
        {
            recorder.Dispose();
            session.DisconnectAudioReceiver(CallParty.All, recorder);
        }
    }
}
	

Code 2 - C# code to call recording

3. Call routing

Besides Dial plan, PBX provides another opportunity to forward calls. You have to import the SetCallRoutingInterceptor() method of OpsClient departement. This method has one parameter, which implements the IcallRoutingInterceptor interface. To implement the function, create a definition. This function is called the GetDestination() method. The function gets the data of the call in a function. The return value is a Destination object, which contains the parameters of the call forward. While creating, the Destination class waits for the identity of the call, the target number, identity of the target extension and the number of times the number was dialled. If the parameters of dialledNumber and the extensonId are null, the call will be declined. The sample program sets "do not disturb" state and declines every call, when *90 is dialled. You can cancel this if you dial *91. If you do not want to forward the call, you can give back a null value, and the call will be handeled by the rules of dial plan.

public void Initialized()
{
	OpsClient opsClient = new OpsClient();
	var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345");

	opsClient.SetCallRoutingInterceptor(new MyCallRoutingInterceptor());
}

class MyCallRoutingInterceptor : ICallRoutingInterceptor
{
	public static List DnDExtensions = new List();

	public OPSSDKCommon.Model.Route.Destination GetDestination(OPSSDKCommon.Model.Route.RouteParams routeParams)
	{
		if (routeParams.Destination.DialedNumber == "*90" && !DnDExtensions.Contains(routeParams.CallerInfo.Owner))
		{
			DnDExtensions.Add(routeParams.CallerInfo.Owner);
		}

		if (routeParams.Destination.DialedNumber == "*91" && DnDExtensions.Contains(routeParams.CallerInfo.Owner))
		{
			DnDExtensions.Remove(routeParams.CallerInfo.Owner);
		}

		if (DnDExtensions.Contains(routeParams.Destination.DialedNumber))
		{
			return new OPSSDKCommon.Model.Route.Destination(routeParams.CallerInfo.Owner, null, null, 10);
		}

		return null;
	}
}
	

Code 3 - C# code to routing calls

4. Add, modify and remove configurations

Modification of configurations is available from API, too, not just from user surface, so it becomes possible to automatize the modifications of the configurations. In case our company has already had a Company Management System it is difficult to handle the data of the employee in more places. With the aid of the APIs, it is possible to integrate the PBX to the company management system, having decreased the time of the registration and management of the employees with it. The API contains three methods that can be used to control the configurations of the PBX. Configurations can be queried by the opsClient.GetConfig() method from the given server. The OPSSDKCommon.Model.Config.BaseCofig class -and every class originated from this- can be given as a parameter at the GetConfig. All configurations stored in the PBX can be queried by giving the BaseCofig class. If we are specifically interested in certain types, that configuration class should be given as T generic parameter. Configuration classes can be found under the namespace OPSSDKCommon.Model.Config. Modification of the configurations is available by using the opsClient.ModifyConfig method. If the configuration with the given ID can not be found on the server, it returns with false value. Creating a new configuration is available by using the opsClient.AddConfig method. If the configuration with the given ID already exists on the server, the method returns with false value. In the example below I am going to show how to add, query and delete a SIP account with the aid of using the C# API, but it is also possible from HTTP API. For further information, click here.

using OPSSDKCommon.Model.Config;

public class Program
{
    public void static void Main(string[] args)
    {
        OpsClient opsClient = new OpsClient();
        var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345");

	 // var configs = opsClient.GetConfig(); Get all configuration
        // var configs = opsClient.GetConfig() Get all SIP account configuration

	var username = "200";
	var displayName = "Don Jon";
	var password = "200";
	var authname = "200";	
	var enabled = true;
	
	var sipAccount = new SIPAccountConfig(username, enabled, displayName, authName, null, password, true);
	
	if(opsClient.AddConfig(sipAccount))
	   Console.WriteLine("Configuration successfully added.");
	else
	   Console.WriteLine("Configuration already exists.");

	Console.WriteLine("Please press enter to modify SIP account configuration.");
	Console.ReadLine();

	if(opsClient.ModifyConfig(sipAccount))
	   Console.WriteLine("Configuration successfully modified.");
	else
	   Console.WriteLine("Configuration does not exists.");

	Console.WriteLine("Please press enter to remove SIP account configuration.");
	Console.ReadLine();

	if(opsClient.RemoveConfig(sipAccount))
	   Console.WriteLine("Configuration successfully removed.");
	else
	   Console.WriteLine("Configuration does not exists.");

	Console.ReadLine();
    }
}
	

Code 4 - C# code to managing configurations

The following list shows the outside line and extension types that can be configured (created, modified or removed) using .NET API.

  • EmailConfig
  • ISDNConfig
  • PSTNConfig
  • CallQueueConfig
  • ConferenceRoomConfig
  • EchoSoundConfig
  • OzmlExtensionConfig
  • RingGroupConfig
  • SIPAccountConfig
  • UserConfig
  • VoiceMailConfig
  • WebphoneExtensionConfig

Part 1: C# example on sending SMS, making VoIP calls

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

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