Headmaster's Busy/Free light

Hello

We have a manager/secretary style busy light on the outside of the manager’s door here, I’d love to be able to tie this into whether he’s on a call, (whether internal or external), I can handle the electronics side of things - but how would I get FreePBX to run a script if he’s on a call?

thank you all for your help! :slight_smile:

3 Likes

Not just on a call but when they are off a call too. You need to trigger to light up when the call is active and turn off when they aren’t on a call.

Also how does it toggle on/off anyways?

This script syncs BLF hint status between systems. You could modify it to only monitor the boss’ BLF hint.

1 Like

Thanks Lorne this looks like it could do what I want - is it something I’d call roughly every 30 seconds or so to try to be as up to date as possible? There’s nothing out there that would ‘push’ the BLF hint is there? Thanks again!

It uses a relay attached to an ESP32 connected to Node-Red

Cool. What are you doing to trigger it? You said you wanted to run a script, so does that work now if you execute the script from the PBX CLI? I mean at this point this is all curiosity on how you’re getting the signal from A to B.

1 Like

Output of Lorne’s script triggering an MQTT message to the ESP32 which in turns triggers the relay was my thinking.

1 Like

Hmm, got it working locally (on the same box as FreePBX) but no matter whether I’m on a call or not the extension still just shows as ‘idle’. I also had to amend the IP in Asterisk Manager Users because it didn’t like 127.0.0.1 I had to enter the LAN IP of the server.

Yet when I login and do core show hints when NOT on a call I get

22@ext-local : PJSIP/22&Local/9022@ State:Idle Presence:away Watchers 23

and when on a call I get

22@ext-local : PJSIP/22&Local/9022@ State:InUse Presence:away Watchers 23

So not sure what could be going wrong!

aha! I deleted a //next to print_r and I can see:

   [22] => Array
        (
            [Response] => Error
            [Message] => Permission denied
        )

so clearly even though I’m logged in there must be something else I need to do to obtain permission to get in?!

aha x 2 I’d disabled write access for the user, thinking this wouldn’t need it, but after enabling write access, it seems to work!

Brilliant, working now. So now I just need to work out how I’m going to call the script - do I setup a cron job to execute it every 30 seconds or so?

Looks like you didn’t edit script with legit AMI credentials. This bare bones is working as expected for me:

<?php

// Remote server host/IP, AMI credentials and 
$remote_server = "127.0.0.1";
$remote_name = "admin";
$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='5002';
$remote_extension_end='5002';

// 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);

and when i run from CLI:

[root@lorne14-pro tmp]# php remotehintpoll.php
Array
(
    [5002] => Array
        (
            [Response] => Success
            [Message] => Extension Status
            [Exten] => 5002
            [Context] => from-internal
            [Hint] => SIP/5002&PJSIP/905002&Custom:DND5002,CustomPresence:5002
            [Status] => 0
            [StatusText] => Idle
        )

)

[root@lorne14-pro tmp]# php remotehintpoll.php
Array
(
    [5002] => Array
        (
            [Response] => Success
            [Message] => Extension Status
            [Exten] => 5002
            [Context] => from-internal
            [Hint] => SIP/5002&PJSIP/905002&Custom:DND5002,CustomPresence:5002
            [Status] => 1
            [StatusText] => InUse
        )

)

Thanks Lorne - I’d been updating/editing my post throughout - seems to have been a permissions thing - got it working - just wondering now how often to call the script to get accurate info - ideally it would be pushed from Asterisk as that seems less aggressive than calling the script every few seconds?

No it wasn’t that, because a) I did, and b) it wasn’t throwing errors at me about (“Can not connect to remote AGI”), it was the Asterisk User Manager write settings it needed enabling. Not just read settings.

1 Like

I am not a programmer, just a hacker. I am sure there is a way to leverage the AMI (the connection to which you now have) to subscribe to an event and receive updates automatically.

edit - there is: Fun with AMI

As is so often the case with me, I’m making things harder than they need to be. This works as well, using the FreePBX AMI connection already established:

<?php

// 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');
}

if($astman->connected()) {
			$cmd = $astman->ExtensionState("5002", "from-internal");
} 

print_r($cmd);
1 Like

Thanks Lorne, this (your second, simpler script) does work, but I’m confused as to how it works because I didn’t need to input any username or password for this script unlike the first script. I know you mentioned it’s already established but in what sense is the AMI connection already established? Sorry to be a ninny.

Legit question, no need for apologies.

FreePBX interacts directly with asterisk using AMI for some actions. For example, when you click the red apply config button, one of the actions is to tell Asterisk to do a core reload. In advanced settings are AMI credentials and you will find corresponding creds in the manager.conf file. It is this AMI user that FreePBX uses.

Early in the script is the include line that allows the script to use FeeePBX resources.
https://wiki.freepbx.org/pages/viewpage.action?pageId=4620458

1 Like

Gotcha! I’m with you. So this wouldn’t work if it was on another machine on my LAN it works because it’s on the same server as my FreePBX instance. Makes total sense now. Thanks.

1 Like

Correct. The more complex version wouldn’t work on another (non freepbx) machine either, as it’s using the bootstrap for the remote connection as well.

Super cool!!

So I’ve been playing around on and off for last few hours and have made a few discoveries. A windows program called CallMonitor which is a bit buggy but which does successfully authenticate and show AMI info, it just never seems to respond to the specific task it’s supposed to do like show a caller ID window or open a URL.

I’ve also come across and installed Pami, I’ve found but not managed to get working a daemon listener called ami-push (as there’s seemingly zero documentation).
So far Pami seems like overkill but it’s the only thing I’ve got working. I also don’t know how to filter events with it or to act on them yet. But I’m making progress!

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.