This is a simple caller database lookup application

Caller Db Lookup

This section show how to do a database lookup on the telephone number of a caller or callee.

Note

The source code for this example can be found in the Examples\DbLookup directory

Caution

If you build this example from the given source code, you will need to change the reference to the AMIgo dll. Click on the project reference directory and delete the reference to AMIgo.dll and then click on Browse and select the AMIgo.dll file from the installation directory.

  • Create a new Windows Form Application in the .NET language of your choice(File | New | Project | C# / VB / C++ | Windows) and name it as you like.
  • Add a reference to the AMIgo.dll assembly. (Right click in the Solution Explorer and select 'Add Reference')
  • Open the AMIgo tab in the Toolbox Windows and drag an AMI_Client object on your project's main form.
  • Set the AMI_Client AstMngrHost, AstMngrPort, AstMngrUserName and AstMngrPassword for the desired Asterisk manager target site.
  • Set the AMI_Client AutoGetConfig property to false.
  • Drag an AMIgoStatusStrip on the form and set it's 'Dock' property to 'Bottom'.
  • From the Tollbox' AMIgo tab, drag a ListViewChannels object in the bottom panel of the Split Container and set it's 'Dock' property to 'Fill'.
  • Click on the AMI_Client and select the Event Tab from the properties windows, then double click on the NewCallEvent.
  • Edit Form1.cs to add the lines shown below to the events handlers and the DoDbLookup method.
  • Modify the connection strin to connect to the Nortwind or your own database.
  • Build the application and run it.

When the application is run, it will attempt to connect and login to the given site. The State shown in the status bar should be 'Logged'. From then on new inbound or oubound call will generate a NewCallEvent and the EventArgs will contains the CallerId number / Name which you can use to do the database lookup.

CopyC#
 1using System;
 2using System.Configuration;
 3using System.Data.SqlClient;
 4using System.Windows.Forms;
 5
 6namespace AMIgoDbLookup
 7{
 8  public partial class Form1 : Form
 9  {
10    /// <summary>A SQL Connection for our DB</summary>
11    SqlConnection sqlConnection;
12    /// <summary>A command object used to send SQL command to the SQLite DB</summary>
13    SqlCommand sqlCommand;
14    /// <summary>A SqlDataReader to get the Db record"</summary>
15    SqlDataReader reader;
16    //+====================================================+
17    public Form1()
18    {
19      InitializeComponent();
20    }
21    //+====================================================+
22    private void Form1_Load(object sender, EventArgs e)
23    {
24      string connection_str = "server=(local);integrated security=SSPI; database=Northwind";
25      //string connection_str = GetConnectionStrings();
26      sqlConnection = new SqlConnection(connection_str);
27    }
28    //+====================================================+
29    private string GetConnectionStrings()
30    { // This method returns the connection string set in the app.config file
31      ConnectionStringSettingsCollection settings =  ConfigurationManager.ConnectionStrings;
32      return (settings[0].ConnectionString);
33    }
34    //+====================================================+
35    // This is the AMI_Client NewCall event handler. 
36    private void AMI_Client1_NewCallEvent(object sender, AMIgo.NewCallEventArgs ev)
37    {
38      if ((ev.src_caller_id.Length >= 10) && (ev.dst_caller_id.Length <= 4)) 
39      {
40        // Inbound Call
41        DoDbLookup(ev.src_caller_id, true);
42      }
43      else if ((ev.src_caller_id.Length <= 4) && (ev.dst_caller_id.Length >= 10))
44      {
45        // Outbound call
46        DoDbLookup(ev.dst_caller_id, false);
47      }
48    }
49    //+====================================================+
50    // Do a Db lookup on the PhoneNumber
51    private void DoDbLookup(string PhoneNo, bool IsInbound)
52    {
53      string sql_cmd = "SELECT * from Customers WHERE Phone = '" + PhoneNo + "'";
54      sqlCommand = new SqlCommand(sql_cmd, sqlConnection);
55      try
56      {
57        sqlConnection.Open();
58        reader = sqlCommand.ExecuteReader();
59        while (reader.Read())
60        {
61          string cust_id = (string)reader[0];
62          string address = (string)reader[4];
63          string city = (string)reader[5];
64          string phone = (string)reader[9];
65          // Do something with the record data. 
66          // Ex. Create an HTML page, on an intranet HTTP Server ... etc
67        }
68      }
69      catch (Exception ex)
70      {
71        MessageBox.Show(ex.Message, "DbLookup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
72      }
73      finally
74      {
75        sqlConnection.Close();
76      }
77    }
78    //+====================================================+
79  }
80}