<?php | |
/**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** | |
* remotehintpoll.php | |
* | |
* Created by Lorne Gaetz lgaetz@sangoma.com | |
* | |
* Latest version: https://gist.github.com/lgaetz/8545099 | |
* | |
* Licensed under GNU GPL version 2 or any later verison. | |
* | |
* Usage: | |
* This script is intended to be used on a local FreePBX system running version 2.9 or higher. When | |
* executed, the script polls a remote Asterisk server using remote Asterisk Manager Interface | |
* (AMI) credentials, checks the status of a specific extension number on the remote server, and | |
* then updates a local hint to the same value. | |
* | |
* Asterisk configuration: | |
* Define a dynamic hint on the local system of the same format as what is dialled to reach an | |
* extension on the remote system. If remote extensions are of the format 10XX and you use a dial | |
* prefix of 55 to dial remote extensions, the local dynamic hint is defined in extensions_custom.conf as: | |
* | |
* [from-internal-custom] | |
* exten => _5510XX,hint,Custom:Remote${EXTEN:2} // program BLF with e.g. 551001 | |
* | |
* If you don't dial a prefix to reach the remote extensions from the local pbx, the hint looks like: | |
* | |
* [from-internal-custom] | |
* exten => _10XX,hint,Custom:Remote${EXTEN} // program BLF with e.g. 1001 | |
* | |
* Version history: | |
* 2014-01-21 Initial commit for single extension | |
* 2017-06-22 Add support to poll a range of extensions and update a local dynamic hint | |
* | |
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****/ | |
// set to true to echo debug | |
$debug=true; | |
// Set the local device prefix for the local hint, must match what is defined in extensions_custom.conf for the hint | |
// the $device_prefix will be used to prefix the remote extension number for the hint | |
$device_prefix = "Custom:Remote"; | |
// Remote server host/IP, AMI credentials and | |
$remote_server = "hostname_placeholder"; | |
$remote_name = "name"; | |
$remote_secret = "password"; | |
$remote_context = "from-internal"; | |
// define range or remote extension numbers to poll, the fewer the better | |
// the range needs to match the dynamic hint noted above | |
$remote_extension_start='1000'; | |
$remote_extension_end='1010'; | |
// Connect to local machine with FreePBX bootstrap, requires FreePBX 2.9 or higher | |
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { | |
include_once('/etc/asterisk/freepbx.conf'); | |
} | |
// connect to remote Asterisk machine using AMI credentials and get status of Extension 103 | |
$remote = new AGI_AsteriskManager(); | |
if ($remote->connect($remote_server, $remote_name, $remote_secret)) { | |
for ($remote_extension=$remote_extension_start; $remote_extension<=$remote_extension_end; $remote_extension++) { | |
$foo[$remote_extension] = $remote->ExtensionState($remote_extension, $remote_context); | |
} | |
$remote->disconnect(); | |
} | |
else { | |
output("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()) { | |
for ($remote_extension=$remote_extension_start; $remote_extension<=$remote_extension_end; $remote_extension++) { | |
switch ($foo[$remote_extension]['Status']) { | |
case -1: | |
output("$remote_extension Extension not found"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' UNKNOWN'); | |
break; | |
case 0: | |
output("$remote_extension Idle"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' NOT_INUSE'); | |
break; | |
case 1: | |
output("$remote_extension In Use"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' INUSE'); | |
break; | |
case 2: | |
output("$remote_extension Busy"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' BUSY'); | |
break; | |
case 4: | |
output("$remote_extension Unavailable"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' UNAVAILABLE'); | |
break; | |
case 8: | |
output("$remote_extension Ringing"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' RINGING'); | |
break; | |
case 9: | |
output("$remote_extension Ringing"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' RINGING'); | |
break; | |
case 16: | |
output("$remote_extension On Hold"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' ONHOLD'); | |
break; | |
default: | |
output("$remote_extension Extension not found"); | |
$cmd = $astman->Command('devstate change '.$device_prefix.$remote_extension.' UNKNOWN'); | |
} | |
} | |
} else { | |
output("Can not connect to local AGI"); | |
} | |
function output($string){ | |
global $debug; | |
if ($debug) { | |
echo $string."\n"; | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Have you thought of adding the ability for multiple ranges? 1100-1150 2100-2150 |
This comment has been minimized.
This comment has been minimized.
@juesor check this script out https://gist.github.com/Ferbla/523b15b9e13275c3b7510be45643c225 |
This comment has been minimized.
This comment has been minimized.
OMG Ferbla that is awesome. All i had to do was create a data.csv with two column's. Layout was as follows. 1100,1120 Now i am watching 1100-1150 omitting 1121 and 1123 and 2100-2150 Thanks @Ferbla for thinking of this setup. |
This comment has been minimized.
This comment has been minimized.
I've 2 Asterisk 13 Servers, i want so sync with your script. But if i run it there are many errors...: Can you help me ? EDIT:
|
This comment has been minimized.
This comment has been minimized.
Did you create the user manage the remote systems? Settings > Asterisk Manager Settings. Add manager and set permit to the IP of the other server on both sides. Add that servers IP to the $remote_server $remote_server = "hostname_placeholder"; From there you need to create customer extensions context exten => _4000,hint,Custom:Remote${EXTEN} I also needed to toggle hints. In FreePBX Web GUI Go to Settings > Advanced Settings Dynamically Generate Hints Then all my phones now toggle ringing and talking. Hope this helps. |
This comment has been minimized.
Future feature:
The $remote_extension variable should accept a comma separated list of remote extensions which would be exploded (and trimmed!) into an array. Then using for_each, $foo becomes an array of values of remote extensions' statuses.