In this post I will show how to implement “click to call” functionality for Asterisk written in C#, and using Asterisk manager API.
To use it you can launch the exe and put like argument the number to dial. Really simple but… works !
The code is subject to be improved and “beautified”.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; using System.Configuration; using System.Threading; namespace Click2Call { class Program { [STAThread] static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Error !"); return; } string m_numerotmp = args[0]; if (m_numerotmp.Length =0) { Console.WriteLine("Number incorrect"); return; } string m_numero = string.Empty; for (int i = 0; i < m_numerotmp.Length; i++) { int test = 0; if (int.TryParse(m_numerotmp.Substring(i,1),out test)) { m_numero = m_numero + m_numerotmp.Substring(i, 1); } } Console.WriteLine("calling..... " + m_numero); System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSectionGroup grpApp = config.GetSectionGroup("applicationSettings"); ClientSettingsSection secPropSet = (ClientSettingsSection)grpApp.Sections["EdiClick2Call.Properties.Settings"]; string m_Extension = secPropSet.Settings.Get("extension").Value.ValueXml.InnerText; string m_ippbx = secPropSet.Settings.Get("ippbx").Value.ValueXml.InnerText; string m_user = secPropSet.Settings.Get("login").Value.ValueXml.InnerText; string m_pass = secPropSet.Settings.Get("password").Value.ValueXml.InnerText; Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(m_ippbx), 5038); clientSocket.Connect(serverEndPoint); clientSocket.Send(Encoding.ASCII.GetBytes("Action: Login\r\nUsername: " + m_user + "\r\nSecret: "+m_pass+"\r\nActionID: 1\r\n\r\n")); int bytesRead = 0; do { byte[] buffer = new byte[1024]; bytesRead = clientSocket.Receive(buffer); string response = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine(response); if(Regex.Match(response, "Message: Authentication accepted", RegexOptions.IgnoreCase).Success) { clientSocket.Send(Encoding.ASCII.GetBytes("Action: Originate\r\nChannel: SIP/" + m_Extension + "\r\nExten: " + m_numero + "\r\nContext: from-internal\r\nPriority: 1\r\nCallerID: " + m_Extension + "\r\n\r\n")); Console.WriteLine("Calling......"); Thread.Sleep(1000); return; } }while(bytesRead != 0); } } }
/etc/asterisk/manager.conf
[<user>] secret = <password> deny=0.0.0.0/0.0.0.0 permit=127.0.0.1/255.255.255.0 permit=<LAN address> read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate write = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate writetimeout = 5000