.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to initiate VoIP phone calls automatically with C# from your MySQL database

Posted By:Simon Robert VoIP       Posted Date: October 11, 2013    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

This article presents a simple C# example about how to initiate automated VoIP phone calls with the help of MySQL database server and C# programming language.


In this article, I give you an example about how you can make automated calls from a C# application and MySQL database. The solution described here requires the followings:

  •  Microsoft Visual Studio
  • MySQL Database Server
  •  Ozeki Phone System XE
  •  OzML Script
  • Softphone (e.g. Bria, X-Lite etc.)

Preparing your Database Server

In this example you will need two database tables. A table that stores the telephone numbers with some another information (phone numbers and the time of the calls), and another table that stores the text of the automated calls that will be read out loud. The first SQL script creates the ozmlout table:

CREATE TABLE `ozmlout` (
  `DialedNumber` varchar(40) NOT NULL,
  `Status` varchar(40) DEFAULT NULL,
  `Duration` int(10) DEFAULT NULL,
  `ScriptId` int(10) DEFAULT NULL,
  `RecordUrl` varchar(150) DEFAULT NULL,
  `StartTime` datetime DEFAULT NULL,
  `ScheduledTime` datetime DEFAULT NULL,
Code Example 1 - Create ozmlout table

The columns mean the followings:

  • Status: The actual condition of the call.
  • Duration: The duration of the call.
  • ScriptID: If the call is connected, an Extension will execute the OzML script with this ID from the ozmlscript table.
  •  RecordUrl: If there is a call recording amongst the commands, this parameter indicates where can the conversation be recorded.
  • StartTime: When the call was started
  • ScheduledTime: When the call is going to be initiated

Then create the second table. In this table there are going to be those OzML scripts, which are going to be executed after the call is being configured. The ozmlscripts column contains the OzML scripts that the Autodialer will play to the callee. The table can be created using the following SQL script:

CREATE TABLE `ozmlscripts` (
  `Ozml` varchar(10000) NOT NULL,
Code Example 2 - Create ozmlscripts table

Configuring the PBX

After you have created your database tables, you need to configure the PBX. In this article, we use the Ozeki Phone System XE as a PBX. It can be downloaded from http://www.ozekiphone.com/. Of course there are similar PBX, as well, but OzML is only used by this PBX.

Initial Steps & SQL OzML

Step1: Download and install Ozeki Phone System XE on your PC then open the http://localhost:7777/ address in your browser and login to the administration page.

Step2: On the Home screen, click on Add button that belongs to the Extensions section.

Step3: Select SQL OzML.

Step4: Fill in the signup data on the Database Connenction tab

Step5: Click on the Outgoing Calls tab and change the "Query template for outgoing call requests" to this:

SELECT Id, dialednumber, scriptid FROM ozmlout WHERE status='call' and scheduledtime < NOW()

If you are ready with the configuration of the OzML SQL, connect a Softphone to the PBX that will receive the automated calls. In this example we use X-Lite Softphone, but any other SIP based softphone can be used.

Using the Code

In this program you need to create three textboxes:

  •  One for the dialed numbers.
  • One for the time of the calls.
  • One for the text that will be read out

Figure 1 - Main screen of your autodialer program

The first part of the code shows how to connect to a MySQL database with your C# application. First you need to add MySQL to your references. In Visual Studio 2012, first right click on "References" and then select "Manage NuGet Packages.".

Figure 2 - Manage NuGet Packages

Then type "mysql connector" to the searching textbox, and Install the "MySQL.Data" from the result list.

Figure 3 - Install MySql.Data

After you have installed the necessary reference, you can connect to your MySQL database server using the following code:

MySqlConnection sql = new MySqlConnection();
sql.ConnectionString = "server=localhost;database=ozekipbx;userid=test;password=test;";

Code Example 3 - Create MySQL Connection

In the ConnectionString you need to provide the hostname or IP address of the MySQL server (in my case it is on the same computer where the autodialer application is developed), the database name and finally the username and password.

If you are done with connecting to the database server, you need to create an OzML script that contains the text that will be read out by the Ozeki Phone System XE. Here, you need to use the textbox of "Text of the Call" that will be between the <Speak> and </Speak> tags of the OzML script. This is the most simple example that can be read in OzML language. There a lot of other paramters of it. It is also possible to play a wav or mp3 file, send SMS or e-mail messages or handle DTMF signals.

string textOfCall = tbTextOfCall.Text;
string ozmlScript = "<Response>\r\n" +
"<Speak>\r\n" +
textOfCall + "\r\n" +
"</Speak>\r\n" +
Code Example 4 - Create OzML script

In this application, it is possible to provide more than one telephone numbers that will be called by the Autodialer system. If you give more phone numbers, you need to seperate them with a semicolon (;). The phone numbers will be split by the following code into an array.

// Split Dialed Numbers
string dialedNumbers = tbDialed.Text;
char[] delimiterChars = { ';' };
string [] dialedNumbersSplit = dialedNumbers.Split(delimiterChars);

Code Example 5 - Split dialed numbers

If you split the phone numbers and created your OzML script, you need to insert the OzML script into the ozmlscripts database table, and insert the phone numbers with script id into the ozmlout table. The following code shows how to insert the OzML script into the database and save the script id.

string insertOzml = "INSERT INTO ozmlscripts " +
"(scriptid, ozml) " +
"(NULL, '" + ozmlScript + "');";

cmd.CommandText = insertOzml;
int scriptid = (int)cmd.LastInsertedId;

Code Example 6 - Save OzML scripts into database

Now, you need to insert the phone numbers, the script id and the time when the calls have to be made.

foreach (string dialedNumber in dialedNumbersSplit)
string insertAutoCall = "INSERT INTO ozmlout " +
"(id, dialednumber, status, duration, scriptid, recordurl, starttime, scheduledtime) " +
"(NULL, '" + dialedNumber + "', 'Call', NULL, '" + scriptid + "', NULL, NULL, '" + tbScheduledTime.Text + "');";
cmd.CommandText = insertAutoCall;

Code Example 7 - Save the phone numbers into database

Using the Program

At first you have to check that the following programs are running:

  • MySql Server
  • Ozeki Phone System XE
  • A softphone (e.g. X-Lite)

If everything is running, open the created C# Autodialer application. In the first figure you can see how it looks. You only have to provide the phone numbers (seperated by a semicolon), the time of the callings and the message for the callees. Then click on Submit button, and the automated calls will be made in the given time.


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend