Home > Code > C# > How to achieve auto answer for SIP telephony in C#

How to achieve auto answer for SIP telephony in C#

by Sacha Manji   on Apr 13, 2015   Category: C#   |  Views: 677    |  Points: 25   |  Starter 


I had a go at softphone development some months ago. It isn’t secret that I want to build a powerful IVR system to make the current call centers much more effective. Yes, there are many similar software available on the market for this purpose, but I believe in my idea that can turn call centers better than ever. I’m intended to give a taste of my project from time to time. First, I present a short and concise guide on how to implement auto answering to be able to accept a SIP phone call immediately.

Let’s look at the prerequisites:

- PBX: To be able to make and receive SIP voice calls, a phone system (e.g. Asterisk) is essentially needed. You need to add a new SIP account in your PBX for this console application.
- Visual Studio: This solution is based on a console softphone, so a new Visual C# Console Application is just enough.
- VoIPSDK.dll: I used prewritten VoIP components to implement the SIP telephony feature. The necessary .dll file can be found on this website: http://www.voip-sip-sdk.com/. It should be added to your references.
- Test phone: To test your application you can use any VoIP phone that supports video calling (e.g. Bria softphone).

After checking the prerequisites, take a look at the code. As it can be seen, just a few lines of C# code are enough to connect the application to a PBX and to build the auto answering. First of all, you need to implement the SIP registration. Thereafter, you need to create a softphone and a phone line object, then you need to specify the SIP account to be used for the phone line. These configurations are needed to be able to register to your PBX. After calling the RegisterAccount method the registration procedure starts, and the application will indicates its status due to the line_RegStateChanged method. The softphone_IncomingCall method is used to perceive the incoming calls, while the Auto_Answer_Method is responsible for accepting the incoming calls automatically. The call_CallStateChanged method is used to indicate the state of the call.

The C# code to implement auto answering for SIP telephony:


using System;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;

namespace Auto_Answer
{
class Program
{
static ISoftPhone softphone;
static IPhoneLine phoneLine;
static IPhoneCall call;

private static void Main(string[] args)
{
softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

var registrationRequired = true;
var userName = "717";
var displayName = "717";
var authenticationId = "717";
var registerPassword = "717";
var domainHost = "192.168.115.100";
var domainPort = 5060;

var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);

RegisterAccount(account);

Console.ReadLine();
}

static void RegisterAccount(SIPAccount account)
{
try
{
phoneLine = softphone.CreatePhoneLine(account);
phoneLine.RegistrationStateChanged += line_RegStateChanged;
softphone.IncomingCall += softphone_IncomingCall;
softphone.RegisterPhoneLine(phoneLine);
}
catch (Exception ex)
{
Console.WriteLine("Error during SIP registration: " + ex);
}
}

static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
{
if (e.State == RegState.NotRegistered || e.State == RegState.Error)
Console.WriteLine("Registration failed!");

if (e.State == RegState.RegistrationSucceeded)
Console.WriteLine("Registration succeeded - Online!");
}

static void softphone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
{
call = e.Item;
call.CallStateChanged += call_CallStateChanged;
call.Answer();
}

private static void Auto_Answer_Method()
{
Console.WriteLine("The auto answer feature comes into action.");
}

static void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
Console.WriteLine("Call state: {0}.", e.State);

if (e.State == CallState.Answered)
Auto_Answer_Method();
}
}
}


Test the program:

In order to demonstrate how this solution works, I provided valid SIP account details for this console application to be able to register to my PBX, and I also configured an other SIP account for my Bria softphone. After running the application, I dialled the telephone number of the console application by using Bria, and the console application accepted the incoming call automatically – without any human intervention.


Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts