ASP.NET VoIP developers 101

With your ASP.NET application it is easy to use more advanced tasks with the Ozeki Phone System by using your . Get to know how to receive/make voice calls, how to receive/send SMS messages and get started to build more advanced communication projects.

Introduction

On the previous page the first part of ASP.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 ASP.NET application. This article focuses on far more advanced features, like call logging, call recording and call routing.

Part 1: ASP.NET commands on sending SMS, making VoIP calls

Connecting to PBX

First of all, create a new ASP.NET project to your application in your Visual Studio, as it can be seen in the part one.

1. Log calls

Make a Controller for logging calls (e.g. LogCallsController) and a simple view for it. After it, make a method what will receive the incoming HTTP requests. This method will receive all the parameters of the call and it will write into file the method below, for example:

public class LogCallsController : Controller
    {
        [HttpPost]
        public void LogCall(string notificationName, string callState, string callId, string caller, string callerId, string callee)
        {
            WriteToFile("c:\\LoggedCalls.txt",
                    notificationName + ";" + callState + ";" +
                    callId + ";" + caller + ";" + callerId + ";" +
                    callee + ";" + 
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
        }
    } 				

Code example 1 - Method for receive the CallChanged events from HTTP API

To log calls, you need to change your settings at PBX, Productivity menu, http API menu item where it is essential to give an URL and select which Controller (and method) will be called when the CallStateChanged event is triggered. Please change the yoursite.com text to that IP address where the sample applications are running. In this example you can add your own route of your application, controller and the receiving method:

Call changed URL: http://yoursite.com/LogCalls/LogCall

When you are ready with the steps above, log all of the CallStateChanges what is occurred in your PBX. If you would like to test this function, make a call in your PBX. Open the file what you have added at your LogCall method (c:LoggedCalls.txt), and you will see the logged events.

Of course, it is possible to log your call into a Database and you can create a View for checking the call events on a different page.

2. Record calls

To record the calls, make another Controller for the Recordings (e.g. RecordCallsController) and a simple view for it. Then, make a method what will receive the incoming HTTP requests. This method will receive all the parameters of the call, and if the callState is InCall the method will send back an OzML response to Record the call:

[HttpPost]
public ActionResult RecordCall(string notificationName, string callState, string callId, string caller, string callerId, string callee)
        {
            if (callState != "InCall")
                return Content("");

            return Content(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<Response>" +
                "<Record finishedUrl=\"http://yoursite.com/RecordCalls/CallRecorded" format="\mp3\"/>" +
                "</Response>", "text/xml");

        }			

Code example 2 - Receive the CallChanged events, and send back OzML response

To record your calls, you must change settings at PBX, Productivity menu, http API menu item where you need to give an URL and select which Controller (and method) will be called when the CallStateChanged event is triggered. In this example you can add your own route of your application, controller and the receiving method:

Call changed URL: http://yoursite.com/RecordCalls/RecordCall

As you can see, in the OzML response, and Record command you need to add a finishedUrl URL, what will called when the call record is completed. Use the URL for logging all of your recorded parameters, for example the route of the recorded voice. This URL can be another method in your RecordCalls Controller:

[HttpPost]
public void CallRecorded(string success, string duration, string recordUrl, string callID, string caller, string callee, string notificationName)
		{
			try
			{
				var db = Database.Open("CallDB");
				var insertQueryString =	"INSERT INTO RecordedCalls " +
										"(Success, Duration, RecordUrl, CallID, Caller, Callee, NotificationName, Date) " +
										"VALUES ('" + success + "', '" + duration + "', '" + recordUrl + "', '" + callID + "', '" + caller + "', '" + callee + "', '" + notificationName + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
				db.Query(insertQueryString);
				db.Close();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}			

Code example 3 - Save the parameters of the RecordCompleted event into Database

In method above you can see an example to logging of the data into a Database (with SQL Server Compact 4.0 Local Database, and WebMatrix.Data.dll). If you open the recordUrl in your browser, you will hear the recorded call. Of course you can log your data into a simple file.

3. Call routing

Besides Dial Plan, it is possible to give an ASP.NET method, so you can decide which extension answers the call when forwarding your calls. If you did not forward the call, it will be handle by the rules of Dial Plan. You can enter into the Route interception URL field the path of your method (e.g. http://yoursite.com/RouteCalls/RouteCall) in the Productivity menu's, HTTP API submenu. The following sample Controller and method will receive the parameters of the RouteInterception event and send back the OzML response with the Route command:

public class RouteCallsController : Controller
    {
        [HttpPost]
        public ActionResult RouteCall(string notificationName, string callerID, string originalDialedNumber, string lastDialedNumber, string lastCalledExtension, string lastCallState, string callerExtension)
        {
            var dest = lastDialedNumber;

            // In the weekends the calls are routed to the "weekend number"
            if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
                dest = "12345";

            // The officer with 883 telephone number is 
            // on day off, route all his calls to another officer
            if (lastDialedNumber == "1001")
                dest = "883";

            return Content(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Response>" +
                "<Route RingTime = \"30\" Destination=\"" + dest + "\"/>" +
            "</Response>","text/xml");
        }
    }				

Code example 4 - Method for override the Dialplan Rules

As you can see, you have the opportunity to add your own rules to control the call forwarding. For example if today is a Saturday or Sunday then route all calls to a number what is ‘online’ at the weekends. Or you can handle the day offs at your company. The choice is yours, use Route Command as you wish.

If you do not want to forward the call, enter an empty string, so the rules of dial plan work.

Part 1: ASP.NET example on sending SMS, making VoIP calls

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