How to achieve auto answer for SIP telephony in C#

/ Published in: C#
Save to your folder(s)

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: 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.

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.

Copy this code and paste it in your HTML
  1. using System;
  2. using Ozeki.VoIP;
  3. using Ozeki.VoIP.SDK;
  5. namespace Auto_Answer
  6. {
  7. class Program
  8. {
  9. static ISoftPhone softphone;
  10. static IPhoneLine phoneLine;
  11. static IPhoneCall call;
  13. private static void Main(string[] args)
  14. {
  15. softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
  17. var registrationRequired = true;
  18. var userName = "717";
  19. var displayName = "717";
  20. var authenticationId = "717";
  21. var registerPassword = "717";
  22. var domainHost = "";
  23. var domainPort = 5060;
  25. var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
  27. RegisterAccount(account);
  29. Console.ReadLine();
  30. }
  32. static void RegisterAccount(SIPAccount account)
  33. {
  34. try
  35. {
  36. phoneLine = softphone.CreatePhoneLine(account);
  37. phoneLine.RegistrationStateChanged += line_RegStateChanged;
  38. softphone.IncomingCall += softphone_IncomingCall;
  39. softphone.RegisterPhoneLine(phoneLine);
  40. }
  41. catch (Exception ex)
  42. {
  43. Console.WriteLine("Error during SIP registration: " + ex);
  44. }
  45. }
  47. static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
  48. {
  49. if (e.State == RegState.NotRegistered || e.State == RegState.Error)
  50. Console.WriteLine("Registration failed!");
  52. if (e.State == RegState.RegistrationSucceeded)
  53. Console.WriteLine("Registration succeeded - Online!");
  54. }
  56. static void softphone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
  57. {
  58. call = e.Item;
  59. call.CallStateChanged += call_CallStateChanged;
  60. call.Answer();
  61. }
  63. private static void Auto_Answer_Method()
  64. {
  65. Console.WriteLine("The auto answer feature comes into action.");
  66. }
  68. static void call_CallStateChanged(object sender, CallStateChangedArgs e)
  69. {
  70. Console.WriteLine("Call state: {0}.", e.State);
  72. if (e.State == CallState.Answered)
  73. Auto_Answer_Method();
  74. }
  75. }
  76. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.