Asterisk Click to Call from Webpage using the AMI Commands
Are you trying to figure out how to set up click to call from a webpage using the Asterisk AMI? Well look no further, this is an easy to follow guide on exactly how to do it.
So before we start a couple of things you have to know about my environment. First I am hosting the webpage on the Asterisk server for the click to call so I have Apache installed and running, second I have port 80 open on the Asterisk server firewall so I can allow external requests.
So first thing you need to do is configure your Asterisk manager config file with a username who can originate the call.
/etc/asterisk/manager.conf [general] displaysystemname = yes enabled = yes webenabled = yes port = 5038 [clickadmin] secret=asdf1234S3 permit=127.0.0.0/255.255.255.0 ; Authorization for various classes read = system,call,log,verbose,command,agent,user,config write = system,call,log,verbose,command,agent,user,config
Once you create a user and password, in this case I have the username “clickadmin” then you can start writing the php code.
/var/www/html/clickcall/index.php $extension = $_REQUEST['internalnum']; $dialphonenumber = $_REQUEST['outboundnum']; $timeout = 10; $asterisk_ip = "127.0.0.1"; $socket = fsockopen($asterisk_ip,"5038", $errno, $errstr, $timeout); fputs($socket, "Action: Login\r\n"); fputs($socket, "UserName: clickadmin\r\n"); fputs($socket, "Secret: asdf1234S3\r\n\r\n"); $wrets=fgets($socket,128); echo $wrets; fputs($socket, "Action: Originate\r\n" ); fputs($socket, "Channel: SIP/$extension\r\n" ); fputs($socket, "Exten: $dialphonenumber\r\n" ); fputs($socket, "Context: dial-outbound\r\n" ); // very important to change to your outbound context fputs($socket, "Priority: 1\r\n" ); fputs($socket, "Async: yes\r\n\r\n" ); $wrets=fgets($socket,128); echo $wrets;
So that webpage will respond to url post of http://[ip address]/clickcall/index.php?internalnum=101&outboundnum=4075551234
It will first place a phone call to the extension number, in the example case would be 101. Once extension 101 answers that call it will then place the outbound leg of the call to phone number 4075551234.
I noticed that the calls are getting stuck when I hang-up and the script won’t work a second time right away. Any clue why? Also I’m getting this error, though it does work occasionally.
[2015-09-02 22:50:35] ERROR[3666]: utils.c:1454 ast_careful_fwrite: fwrite() returned error: Broken pipe
Looks like there might be a syntax error, it could be due to doing a copy and paste.