This example is a simple demonstration on how to integrate PBX functions in an application

Integration of PBX functions in your applications

Note

The source code for this example can be found in the Examples\Example3 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.

Here we have a simple Windows form application with textboxes to enter an Asterisk site and login information. and textbox to enter a source extension and a destination, which can be an external phone number or an extension, or a Local extension to btranch into the dialplan. The context is set to default

Note

This demonstrate how to create an AMI_Client object by programming. You could also just drop an AMI_Client on your form project.

CopyC#
 1using System;
 2using System.Windows.Forms;
 3using AMIgo;
 4
 5namespace Example3
 6{
 7  public partial class Form1 : Form
 8  {
 9    private AMI_Client AMI_Client1;
10    private AMIgoMsgDisplayForm DisplayForm;
11    //+=================================================+
12    public Form1()
13    {
14      InitializeComponent();
15    }
16    //+=================================================+
17    private void AMI_ErrorEventHandler(object sender, StringMsgEventArgs ev)
18    {
19      MessageBox.Show(ev.string_msg, "AMIgo Error");
20      AMI_Client1.Stop();
21    }
22    //+=================================================+
23    private void StateChangeEventHandler(object Sender, AMIgo.StateChangeEventArgs ev)
24    {
25      // If auto login just wait for the READY state
26      if (AMI_Client1.AutoLogin == true)
27      {
28        if (ev.state == AMI_Client.AMI_ClientState.READY)
29          // 
30          AMI_Client1.OriginateExtension(textBoxTechData.Text, textBoxContext.Text, textBoxPhoneNo.Text, "1", "");
31      }
32      else // else call Start and do things 'manually'
33      {
34        if (ev.state == AMI_Client.AMI_ClientState.RUNNING)
35          AMI_Client1.Connect();
36        else if (ev.state == AMI_Client.AMI_ClientState.CONNECTED)
37          AMI_Client1.Login();
38        else if (ev.state == AMI_Client.AMI_ClientState.LOGGED)
39          AMI_Client1.OriginateExtension(textBoxTechData.Text, textBoxContext.Text, textBoxPhoneNo.Text, "1", "");
40      }
41
42      if (ev.state == AMI_Client.AMI_ClientState.IDLE)
43        AMI_Client1.Dispose();
44    }
45    //+=================================================+
46    private void HangupEventHandler(object sender, AstChannelEventArgs ev)
47    {
48      AMI_Client1.Stop();
49    }
50    //+=================================================+
51    private void buttonOrig_Click(object sender, EventArgs e)
52    {
53      AstSite aSite = new AstSite("UnNamed", textBoxHost.Text, textBoxPort.Text, textBoxUserName.Text, textBoxPassword.Text, false);
54      AMI_Client1 = new AMI_Client(this, aSite);
55      // Try setting these properties to false;
56      AMI_Client1.AutoStart = true;
57      AMI_Client1.AutoLogin = true;
58      // We dont need to get the site whole config ...
59      AMI_Client1.AutoGetConfig = false;
60      // Setup a few Event handlers
61      AMI_Client1.StateChangeEvent += new AMIgo.StateChangeEventHandler(StateChangeEventHandler);
62      AMI_Client1.HangupEvent += new AstChannelEventHandler(HangupEventHandler);
63      AMI_Client1.ErrorEvent += new ErrorEventHandler(AMI_ErrorEventHandler);
64      // Create the raw manager message display form and show it.
65      DisplayForm = new AMIgoMsgDisplayForm(AMI_Client1);
66      DisplayForm.Show(); 
67      // If AutoStart is false we need to call Start();
68      if (AMI_Client1.AutoStart == false)
69        AMI_Client1.Start();
70    }
71  }
72}

Note

This example shows how to use both automated connection and login:

  • Leaving the default AutoStart and AutoLogin properties to true. Setup a StateChangeEvent handler and wait for the AST_READY before proceeding to send commands to the AMI.
  • Seting AutoStart and AutoLogin to false and using Start, Connect and Login method in the StateChangeEvent handler.