Remote BLF Hints


I have modified a script written by lgaetz (BIG THANKS!!) to poll hints from remote PBXs. The script is in PHP and I have gotten it to work but I think it needs some optimization. It takes a few seconds to perform the poll and I think it may have something to do with it constantly connecting and disconnecting the the AGI_AsteriskManager session. I did a test in a lab environment of about 10 remote extensions that have a 30/30 Mbps low latency connection between sites (metro ethernet) and it took about two seconds to run. The number of extensions polled does appear to impact run time (but not by too much).

Code is below:

/**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****  
* remotehintpoll-bulk.php
* Created by lgaetz 
* Modified by alpha202bd
* Licensed under GNU GPL version 2 or any later verison.
* Usage:
* This script is meant to be used on a FreePBX system running version 2.9 or higher. When
* executed, the script polls a remote Asterisk server, checks the status of a specific 
* extension on the remote server, and then updates a local hint to the same value. A 
* comma seperated list of remote extensions can be specified to poll multiple remote 
* extensions.
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** /
// User config param
// Remote server host/IP, AMI credentials and 
$remote_server = "";
$remote_name = "admin";
$remote_secret = "password";
$remote_extension = '101,102,103,104';
$remote_context = "from-internal";

// This is concatenated with the list of extensions to form the name of the hint. 
$str1 = "Custom:Remote";

// Explodes the comma delimited values.
$remote_extension_array = explode(',',$remote_extension);

// Connect to local machine with FreePBX bootstrap, requires FreePBX 2.9 or higher
if ([email protected]_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
// connect to remote Asterisk machine using AMI credentials and get status of Extension 103
$remote = new AGI_AsteriskManager();
// Runs through each delimited value 
foreach ($remote_extension_array as &$remote_ext) {
if ($remote->connect($remote_server, $remote_name, $remote_secret)) {
	$foo = $remote->ExtensionState($remote_ext, $remote_context);
else {
	echo "Can not connect to remote AGI";
//print_r($foo);  //for debug
// Based on value of remote extension status, change local custom device state to match
// edit $device to reflect name of local custom device as defined in extensions_custom.conf
// in the [from-internal-custom] section add a line similar to:
// exten => 103,hint,Custom:Remote103
// Make sure that the number does not conflict with something else
if($astman->connected()) {
	switch ($foo['Status']) {
		case -1:
			echo "Extension not found";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' UNKNOWN');
		case 0:
			echo "Idle";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' NOT_INUSE');
		case 1:
			echo "In Use";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' INUSE');
		case 2:
			echo "Busy";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' BUSY');
		case 4:
			echo "Unavailable";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' UNAVAILABLE');
		case 8:
			echo "Ringing";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' RINGING');
		case 9:
			echo "Ringing";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' RINGING');
		case 16:
			echo "On Hold";
			$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' ONHOLD');
		   echo "Extension not found";
		   $cmd = $astman->Command('devstate change '.$str1.$remote_ext.' UNKNOWN');
} else {
    echo "Can not connect to local AGI";

I am looking forward to hearing any input or suggestions on how to optimize this modification.

Please keep in mind I am by no means a programmer :wink:


1 Like

You’re not going to get much faster with ExtensionState, especially since this script is a poll script.

What you need is a push script. Which would run constantly on Machine B connected to the AMI on Machine A and would subscribe to the hint changing then push it to local Machine B (this is sort of how our Phone Apps BLFs work for phones that don’t support XML BLF keys). PHP is ok for something like this but is better suited for NodeJS. Anyways…

Unfortunately the script above is not push so you will never be able to get the speed you want out of it.

Asterisk has a way to doing this natively.

1 Like


I appreciate for the feedback. I had a feeling that was going to be the case.

The Phone Apps BLF you mention, will this work with phones other than Sangoma handsets? We use Yealink and Grandstream at the moment.



Indeed! I would love to learn about how to do that but the number of clients we have with multiple PBXs is limited at the moment. Definitely something to consider moving forward though…


Hello Everyone,

I see that has not been any activity on this thread for a few months but I have been banging my head all day with this. I have the script on the server, have added the line to the extenstion_custom file. When I manually run the script it always comes back IDLE. Any ideas?



How many phones are you polling? Are the phones you’re attempting to poll connected to a different FreePBX installation? Try having someone make a call on one of the polled handed handsets and then run the script. I suspect you will see something different. Also, try to add a BLF for that remote extension on a phone in your local office so you can see if it pulls a status. Depending on the phone make/model, you may see the remote extension light up (and stay that way until a subsequent polling tells it otherwise).

Best regards

Thanks, I plan to spend some time this weekend working on it. Just haven’t had the time to get back to it. What I did to test was a set up a softphone on my laptop to the remote server, called from my desk phone to the softphone, ran the script to pool the remote side and that extension still showed idle. Better luck this weekend.

I also am looking at using PJSIP for the site to site trunk, I am reading that device state and mwi is synced with a PJSIP trunk.

How did your testing go? Does PJSIP transfer device state like you thought it would?

I am looking for a solution to this as well. I have two sites that are connected via an IAX trunk, which is working great, but I want BLF hints to also traverse the connection.

I ran across this, which sounds promising. Does anyone have any experience with it?

Exchanging Device and Mailbox State Using PJSIP