C# .NET VoIP developers 101
Part 1: C# example on sending SMS, making VoIP calls
Read this short guide and learn how to do basic tasks with the Ozeki Phone System, from your C# .NET application. This guide explains how to receive and make voice calls, how to send and receive SMS messages and helps you get started to build more advanced projects.
Download
C# example on sending SMS and making VoIP calls- Ozeki_VoIP_CSharp_Example_Source.zip (11.5MB)
- Ozeki_VoIP_CSharp_Example_Exedemo.zip (11.5 MB)
- http://www.ozekiphone.com/examples/doc/
Part 2: C# example on logging and recording VoIP calls, call routing and configuration management
1. What you need
- Visual Studio 2010 or newer
- Ozeki Phone System installed on your PC (Download now)
- A connected softphone
- A connected SMS service
2. Get started
- Create an API extension in the Ozeki Phone System
- Create a C# project and add OPSSDK.dll (You can find OPSSDK.dll at
C:\Program Files\Ozeki\Ozeki Phone System\Examples.NETAPI\OPSSDK.dll). - You need to add it as a reference to your C# project
After OPSSDK.dll has been added, it will appear in the references section of your project (Figure 2).
3. Receive incoming voice calls
You need to create a new instance from the OpsClient type then login to your Ozeki Phone System. To sign in, you need to specify the IP address of the server (on which your PBX has been previously installed), your username and the corresponding password. The return value of the Login function indicates whether the login was successful or not.
After that, you need to get an API Extension with the GetAPIExtension function, which needs one parameter, the identifier of the extension. If this extension does not exist, the GetAPIExtension function will return null. If the extension exists, you can subscribe to the IncomingCall event of the API Extension.
In the event handler you can access the call object in the VoIPEventArgs<ICall>'s Item property. Now you can accept, reject or forward this call object with the appropriate function. In addition, you could subscribe to the events of this object and any media handlers (MediaHandler) can also be attached.
void Initialize() { var opsClient = new OpsClient(); var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345"); var apiExtension = opsClient.GetAPIExtension("4324") apiExtension.IncomingCall += IncomingCall; } void IncomingCall(object sender, VoIPEventArgs<ICall> e) { var call = e.Item; call.Accept(); //call.Reject(); //call.Forward("432432"); }
4. Make outgoing voice calls
You need to create a new instance from the OpsClient, then login to your Ozeki Phone System. To sign in, you need to specify the IP address of the server (on which your PBX has been previously installed), your username and the corresponding password. The return value of the Login function indicates whether the login was successful or not.
After that, you need to get an API Extension with the GetAPIExtension function, which needs one parameter, the identifier of the extension. If this extension does not exist, the GetAPIExtension function will return null. If the extension exists, you can create a call through the API Extension. For this, you need to use the CreateCall method, then subscribe to the events of the call. The CallStateChanged event indicates that the call status has been changed. You could get the actual state from the EventArg's Item property. The CallErrorOccured event shows that an error has occured.
Many different media handlers can be attached to the call. By using the ConnectAudioReceiver method, you can attach a speaker, and the ConnectAudioSender method could be used to attach a microphone to the call. With the Start method you could start calling the callee.
void Initialize() { var opsClient = new OpsClient(); var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345"); var apiExtension = opsClient.GetAPIExtension("4324"); var call = apiExtension.CreateCall("2343242"); call.CallStateChanged += CallStateChanged; call.CallErrorOccurred += CallErrorOccurred; // ConnectAudioReceiver could attach other handlers to a call, like // Speaker, Mp3Recorder or WavRecorder call.ConnectAudioReceiver(Speaker.GetDefaultDevice()); // ConnectAudioSender could also attach a // Microphone, an Mp3Player or a WavPlayer call.ConnectAudioSender(Microphone.GetDefaultDevice()); call.Start(); } void CallErrorOccurred(object sender, VoIPEventArgs<CallError> e) { Console.WriteLine("Error occurred in the call: {0}", e.Item); } void CallStateChanged(object sender, VoIPEventArgs<CallState> e) { Console.WriteLine("Current call state is: {0}", e.Item); }
5. Receive SMS message
You need to create a new instance from the OpsClient, then login to your Ozeki Phone System. To sign in you need to specify the IP address of the server (on which your PBX has been previously installed), your username and the corresponding password. The return value of the Login function indicates whether the login was successful or not.
After that, you need to get an API Extension with the GetAPIExtension function, which needs one parameter, the identifier of the extension. If this extension does not exist, the GetAPIExtension function will return null. If the extension exists, you can subscribe to the API Extension's MessageReceived event, which will indicate when a new message arrives. In the Message EventArg you can access the message content, or sender.
void Initialize() { var opsClient = new OpsClient(); var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345"); var apiExtension = opsClient.GetAPIExtension("4324"); apiExtension.MessageReceived += MessageReceived; } void MessageReceived(object sender, Message e) { // Here you could receive an EmailMessage, or an SMSMessage too. Console.WriteLine("Message received."); }
6. Send SMS message
You need to create a new instance from the OpsClient, then login to your Ozeki Phone System. To sign in you need to specify the IP address of the server (on which your PBX has been previously installed), your username and the corresponding password. The return value of the Login function indicates whether the login was successful or not.
After that, you need to get an API Extension with the GetAPIExtension function, which needs one parameter, the identifier of the extension. If this extension does not exist, the GetAPIExtension function will return null. If the extension exists, you can subscribe to the MessageSubmitted and MessageDelivered events. The MessageSubmitted event will raise when a message is about to send, and the MessageDelivered event will raise when the recipient receives the sent message. To send an SMS message you need to create an SMSMessage object, where you can provide the destination and the message content. If you need to identify each SMS, you could do that with the SMS object's MessageId property. Finally, with the SendMessage method you can send the created SMS object to the recipient.
void Initialize() { var opsClient = new OpsClient(); var result = opsClient.Login("ozekixepbx.ip", "admin", "abc12345"); var apiExtension = opsClient.GetAPIExtension("4324"); apiExtension.MessageSubmitted += MessageSubmitted; apiExtension.MessageDelivered += MessageDeliveder; var sms = new SMSMessage("+363023424", "Hello"); //sms.MessageId will appear in the Submit and the Delivery report. apiExtension.SendMessage(sms); } void MessageDeliveder(object sender, MessageResultEventArgs e) { Console.WriteLine("Delivery result: {0}", e.Result); } void MessageSubmitted(object sender, MessageResultEventArgs e) { Console.WriteLine("Submit result: {0}", e.Result); }
7. Create a more advanced project
The Ozeki Phone System offers a lot more options for C#.net developers. You can interact
with existing calls, control and configure the PBX, you can introduce new communication
techniques and media formats.
For a complete list of .NET API commands, check out:
http://www.ozekiphone.com/examples/doc/
Part 2: C# example on logging and recording VoIP calls, call routing and configuration management
If you have any questions or need assistance, please contact us at info@ozekiphone.com
Dig deeper!
People who read this also read...
- How to connect your Android, Apple or Windows Phone to Ozeki Phone System
- How to setup SMS service in Ozkei Phone System XE
- How connect VoIP, PSTN or ISDN telephone network to your PBX
- How to add a Webphone to your website
More information
- Part1: C# example on sending SMS, making VoIP calls
- Part1: VB example on sending SMS, making VoIP calls