Auto dialing a number

Hello everyone, this is my first post.

Currently using free PBX v2.11.0.42 in our office. Yes I know its old but has worked well and our early upgrade attempts went wrong and we had to wind back so stayed with it. We will be upgrading and reconfiguring everything soon.

Would like to know if this is possible…

On our company web based admin panel, a user can click a button and it will dial out a number via zoiper on their PC and headset via SIP - which was easy enough to do.

Is it somehow possible in FreePBX to make a web call via HTTP, JSONP etc from our company system to free pbx that could make the users extension number ring and when answered dial the number that was made in the http request?

We are trying to save (a) the time dialing a number makes (b) the number of wrong numbers people dial.

We use Panasonic KX-UT113 SIP phones with it and these work well.

Hope that makes sense.

I did find this post…

Yeah. Check out “callfiles” for a lot more information.

Ok so here is what i ended up with…

This is a script that I can call from a browser on the local network.

Obv ext and number have been changed so have usernames and passwords

<?php $extension = $_REQUEST['exten']; $dialphonenumber = $_REQUEST['number']; $timeout = 10; $asterisk_ip = ""; $socket = fsockopen($asterisk_ip,"5038", $errno, $errstr, $timeout); fputs($socket, "Action: Login\r\n"); fputs($socket, "UserName: abc123\n"); fputs($socket, "Secret: def456\n"); $wrets=fgets($socket,128); echo "Response 1: $wrets
"; fputs($socket, "Action: Originate\r\n" ); fputs($socket, "Events: off\r\n" ); fputs($socket, "Channel: SIP/$extension\r\n" ); fputs($socket, "Exten: $dialphonenumber\r\n" ); fputs($socket, "Context: from-internal\r\n" ); // very important to change to your outbound context fputs($socket, "Priority: 1\r\n" ); fputs($socket, "Async: yes\r\n\r\n" ); fputs($socket, "Action: Logoff\r\n\r\n" ); $wrets=fgets($socket,128); echo "Response 2: $wrets"; fclose($socket); ?>

In the manager_custom.conf I have the following added lines:

read = system,call,log,verbose,command,agent,user,config
write = system,call,log,verbose,command,agent,user,config

The script returns…

Response 1: Asterisk Call Manager/1.3
Response 2: Response: Success

Which looks like it does as expected, but the logs throw back

[2017-08-16 13:14:20] ERROR[8585] utils.c: fwrite() returned error: Broken pipe

Some other suggestions on the t’internet said it could be a copy and paste issue from windows (like new lines), so I re-wrote it all by hand.

Still returns the error

In your first echo, get rid of the embedded CR and use a \n.

I don’t remember if the username and password need \r and \n, but try that and see if you are getting further. Also, try running the program from the console and see what happens. You’re not really getting a lot of debug information from Asterisk.

Also, double check to make sure where your manager logins are coming from. I think there’s an Asterisk CLI command that lists the managers - make sure ‘abc123’ is in there.

thanks ill give this a go

This sounds like predictive dialling which would normally use something like

Appointment reminder can call al list, play a message and the users can press 1 to speak to an agent etc.

You can use call files with a custom context and do AMD which is hit and miss.

I want our agents to simply press a button on our software and their desk phone rings. When they pick it up it dials the number.

I have everything working in our software, I just need this script to dial the number.

The username and password are ok and are listed in the “manager_custom.conf” file. I did a core reload to make sure it recognised the changes.

If I change them to an unknown user, I get an error so that part appears ok.

If i replace the \r\n with \n it does the same thing.

Possibly the issue may lie with the “Context” command. Which context would I set it to to be sure?

I have “User Context” in the trunks and “context” in the extensions. In the extensions it is the default “from-internal”. In the trunks, each is set to a different name.

I have tried changing the context in the script to various combinations, including “from-internal”, “outbound-dialing” etc.

Did also find an article,

And the section “Context menu dial from Internet Explorer using PHP” has a similar script to connect. Even making lots of changes to my initial script makes no difference.

This may help:

Thanks lgaetz. This is very similar to the original script.

However modified in all manner of ways, still getting same issue… Thought this would have been a 10 minute job to get working.

Even direct via telnet I get this…

[email protected]:~ $ telnet 5038
Connected to
Escape character is ‘^]’.
Asterisk Call Manager/1.3

Then I enter the commands…

Action: Login
Events: off
Username: abc123
Secret: def456

Action: Originate
Channel: SIP/250
WaitTime: 30
Exten: 907123456789
Context: from-internal
Priority: 1
Timeout: 5000

And this is the result…

Response: Success
Message: Authentication accepted

Hmm… Sigh… scratches head

Sure I am missing something obvious.

The last command sent to AMI must be terminated by “fputs($socket, “Secret: def456\n\n”);” in your original post you only had “\n”

Appreciate that. I did try many combinations and that was poss the last one i tried.

this is one of the test scripts, but still not working… plus the telnet didn’t work either (even though it seemed pleased with itself when I passed the commands)

Even \n rather than \r\n in each case didn’t work either.

the logs show

ERROR[8585] utils.c: fwrite() returned error: Broken pipe

in all cases.

I call the script with

<?php $extension = $_REQUEST['exten']; $dialphonenumber = $_REQUEST['number']; $timeout = 10; $asterisk_ip = ""; echo "Dial $dialphonenumber from $extension
"; $errno=0; $errstr=0; $socket = fsockopen($asterisk_ip,"5038", $errno, $errstr, $timeout); fputs($socket, "Action: login\r\n"); fputs($socket, "Events: off\r\n"); fputs($socket, "Username: abc123\r\n"); fputs($socket, "Secret: def456\r\n\r\n"); fputs($socket, "Action: Originate\r\n" ); fputs($socket, "Channel: SIP/$extension\r\n" ); fputs($socket, "WaitTime: 30\r\n"); fputs($socket, "Exten: $dialphonenumber\r\n" ); fputs($socket, "Context: from-internal\r\n" ); // very important to change to your outbound context fputs($socket, "Priority: 1\r\n" ); fputs($socket, "Timeout: 5000\r\n" ); fputs($socket, "Action: Logoff\r\n\r\n" ); $wrets=fgets($socket,128); echo "Request Response: $wrets"; sleep(2); fclose($socket); ?>

Linux requires line termination be “\n” NOT “\r\n” , “\n\n” (a terminating ‘empty’ line) is the signal for AMI that the “action” is complete.

OK with some help from Igaetz and dicko it is finally working.

Sorry for a late reply only just had chance to jump back on the project…

Thanks for your help :wink:

Mind posting the working script?


Absolutely no problem. Here you go.

We now just call the script via AJAX from our intranet based customer service system with a button that says DIAL…

It has now stopped all incorrectly dialed numbers and our sales team save a load of time now not having to dial manually.


/**** **** **** **** **** **** **** **** **** **** **** **** **** ****
 * freepbx_call.php
 * This script is a compliment to the project at:
 * Instructions:
 *      Place a copy of this file in the same place as the call.php file
 *      from the Jolt repo. Modify the Jolt extension URL to call this file
 *      i.e.  http://pbx_address/freepbx_call.php
 * License:
 *    The original MIT license terms apply
 **** **** **** **** **** **** **** **** **** **** **** **** **** ****/


ini_set('display_errors', 1);

// load FreeBPX bootstrap environment, requires FreePBX 2.9+

if ([email protected]_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf'))

global $db;             // FreePBX asterisk database connector
global $amp_conf;       // don't think we need this
global $astman;         // phpagi class

// User Parameters
# set the context for dialling
$strContext = "from-internal";

# specify the amount of time (in milliseconds) you want to try calling the specified channel before hanging up
# make sure this time is shorter than the time it takes for voice mail to answer
$strWaitTime = "5000";

# specify the priority you wish to place on making this call
$strPriority = "1";

# specify the maximum amount of retries
$strMaxRetry = "1";

# Async setting
$strAsync = 'no';

// get arguments
$ext = $_GET['exten'];

// Taken from call.php, some of this is prob not necessary
elseif (isset($_GET['phone'])) 

//Clean up number
$number = filter_var($number, FILTER_SANITIZE_NUMBER_INT);
$number = preg_replace("/[^0-9,.]/", "", $number);
// Get a 2D array of all FreePBX extension details
$extension_array = core_users_list();

// Convert the 2d array to a 1d array of just extension numbers
foreach ($extension_array as $bar)
    $ext_list[] = $bar[0];

// Check if the extension number passed to the script is a valid system extension
if (in_array($ext,$ext_list))
    $strChannel = "local/".$ext."@$strContext";
	echo "Error. $ext is not a valid extension";
// Construct an array for dialling
$dial = array();
$dial['Channel'] = $strChannel;
$dial['Context'] = $strContext;
$dial['Exten'] = $number;
$dial['Priority'] = $strPriority;
$dial['Async'] = $strAsync;
$dial['Timeout'] = $strWaitTime;
$dial['CallerID'] = '"Click to dial" <'.$number.'>';
echo "Bridging extension $ext to number $number." ;

// using $astman class from bootstrap, originate the call

[fixed formatting - mod]

Sorry to revive this thread, but it seems this script no longer works, saying “Call to undefined function core_users_list()” on line 70.

Has something changed with FreePBX internals to break this?