I am sure someone will find this useful… Here is the C# .Net version of this script.
I added the Event: off to the login so that I do not get unneeded events since we have a very busy phone system. We average about 2000+ calls a day through our asterisk server.
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace SynergyProviders
{
public class PhoneDialer
{
private string serverAddr = "192.168.0.233";
private string mgtUser = "";
private string mgtPass = "";
public PhoneDialer(string serverAddress, string mgtUsername, string mgtPassword)
{
serverAddr = serverAddress;
mgtUser = mgtUsername;
mgtPass = mgtPassword;
}
public void DialPhone(int usersExtension, string numberToDial)
{
try
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP))
{
var svrAddress = IPAddress.Parse(serverAddr);
var svrEndPoint = new IPEndPoint(svrAddress, 5038);
socket.Connect(svrEndPoint);
var byteData = new byte[128];
var byteResp = new byte[1024];
var cmdString = "Action: Login\r\n";
cmdString += string.Format("Username: {0}\r\n", mgtUser);
cmdString += string.Format("Secret: {0}\r\n", mgtPass);
cmdString += "Event: off\r\n\r\n";
socket.Send(Encoding.ASCII.GetBytes(cmdString));
socket.Receive(byteData);
cmdString = "Action: Originate\r\n";
cmdString += string.Format("Channel: SIP/{0}\r\n", usersExtension);
cmdString += string.Format("Exten: {0}\r\n", numberToDial);
cmdString += ("Variable: _SIPADDHEADER55=Alert-Info: Ring Answer\r\n");
cmdString += "Context: from-internal\r\n";
cmdString += string.Format("CallerId: \"Synergy Dialer\" <{0}>\r\n", usersExtension);
cmdString += "Priority: 1\r\n";
cmdString += "Async: yes\r\n\r\n";
socket.Send(Encoding.ASCII.GetBytes(cmdString));
socket.Receive(byteData);
socket.Close();
}
}
catch (Exception e)
{
throw new Exception(string.Format("Unable to dial number: {0}", e.Message) );
}
}
}
}
I use it in in my page by by putting it in a linkbutton. The code in the event is as follows.
var dialer = new SynergyProviders.PhoneDialer(szServerIp, szUsername, szPassword);
dialer.DialPhone(Convert.ToInt32(WebProfile.Current.PhoneInfo.PhoneExt), szNumberToDial);
have fun…
John