Monitor an extension's state to allow a script to be run

(D E) #1

I have the ability to mute the volume on my windows desktop with a script which is called from the FreePBX server. It works well.

But now I want to link the two things together, so that when my extension answers a call, the script is called and the volume muted. When the extension is free again, a different script should be called, and the volume unmuted.

I’ve Googled and searched this forum and I can’t find a way to do this - the closest I came was a PHP script which polled, but that was deemed to be too slow to be useful, as surely it’s a script which ‘pushes’ which would be most useful?

Many thanks - and no worries if this isn’t doable, it’s just the icing on the cake!

(Moussa) #2

For following will give you information about your pjsip/sip extension:

/usr/sbin/asterisk -x ‘pjsip show endpoint NNN’
/usr/sbin/asterisk -x ‘sip show peer NNN’

Adding grep “Not in use” (might be different string for sip extensions) will result an output if the extension is not in use and no output if the extension is in use

/usr/sbin/asterisk -x ‘pjsip show endpoint NNN’ | grep “Not in use”

You can then use the output to do what you want.

(D E) #3

Thanks so much, mousssa, but isn’t that something I’d call using (for example) cron? It won’t “push” the information I’ll have to poll for it? I think it might be too slow to catch the beginning and end of the call, if so? Or have I misunderstood? Thanks!
I was wondering if I could somehow hook into the SIP hints? They seem pretty instant.

(Moussa) #4

The result of /usr/sbin/asterisk -x ‘pjsip show endpoint NNN’ is quick. I used this in a cron job because I did not mind some delay.

I hope someone can add to this or comes with a better idea.

(Tom Ray) #5

It’s call AMI. You would need to create an AMI listener that would listen for AMI events and you could then monitor what is happening and run scripts based on those events.

(Lorne Gaetz) #6

Many hard phones have Action URL options (such as Sangoma S series) where you can config the phone itself to make URL calls when specific phone actions happen such as answer or hangup.

(D E) #7

Thanks Lorne. It’s a D50 and I can’t see any mention of action URLs in the phone’s web GUI

(D E) #8

Cool! Looks like this is already a finished product - and designed for the self same purpose I wanted it for! Funny

(D E) #9

Spoke too soon. The script doesn’t run, and is deemed to be waaaaay out of date.

(TheJames) #10


*not shown in preview, added license per terms of forums

(D E) #11

Thank you, I will have a go at doing something with these! So far a bit bewildered as they seem such minimalist files I don’t know where or how I’d integrate these or what they make possible, but I’ll definitely have a crack at them! :slight_smile:
thanks again

Somebody also mentioned a utility on this list MIGHT allow custom script execution. Would have to look into that too tomorrow.

Thanks all

P.S. It turns out I didn’t mean on/off HOOK, I mean on/off a call. Apparently there’s a difference and it’s a big one. Oops. :rofl:

(D E) #12

Progress is being made…slowly!!

So today I installed Git, cloned the ari-py repo, installed python-pip, upgraded setup tools, found the ari username and password in advanced settings aaaand I seem to have successfully made a connection to the ari…


Quite what I do now, and next, I don’t know, but tomorrow’s a new day!!

(The frequent disconnects/connects are due to my not realising that despite the error in my console "RequestsDependencyWarning: urllib3 (1.24.3) or chardet (2.2.1) doesn't match a supported version!" things were actually working and a connection was being made.

I guess I need to run ‘hello’ as a service, somehow, rather than a bash command…

I also need to tell ‘hello’ (somehow) to look for specific events and filter on them.


Thanks all!

(D E) #13

I’m not getting anywhere with the listener service/handler, I think that’s a bit beyond me at the moment, sadly.

HOWEVER, I am wondering if I can achieve 80% of what I wanted to with [this] from @lgaetz (Hooking for fun and income):

How do I perform a custom action on hangup?

Nothing to it. Using the techniques above, you would add a hangup handler to the channel. A hangup handler, as the name implies, is a subroutine that runs after the channel hangs up. A simple example:

The dialplan:

exten => s,1,Noop(Entering user defined context macro-dialout-one-predial-hook in extensions_custom.conf)
exten => s,n,Set(CHANNEL(hangup_handler_push)=lgaetz-do-this-on-hangup,s,1)
exten => s,n,MacroExit

exten => s,1,Noop(Entering user defined context lgaetz-do-this-on-hangup in extensions_custom.conf)
; additional lines 
exten => s,n,Return

Looks like I can hook on internal calls (and catch the hangups) for a given extension - so all I’d be missing with this approach is if the extension took any external calls?

Might work!?

(Tom Ray) #14

OK so this is a serious question. What is the actual issue with this user not being able to mute or pause their media player? This is a windows system so CRTL+SHFT+M will mute and unmute the speakers on the PC. So what is the actual problem?

I’m asking because you’ve spent a week across three mediums (here, IRC and Asterisk forum) trying to make this work. Describing the actual problem might prompt some more helpful solutions.

(D E) #15

Hiya Tom.

Don’t worry, it’s not been a solid week! :slight_smile: . Just on and off. But just saw an opportunity to make this user’s life easier with automation, which I always enjoy.

I haven’t found Ctrl,shift+M does the trick - we often have Chrome open when we’re working, most of our work revolves around an online database tool, and Chrome uses control shift and M to swap user.

So the actual ‘problem’ is user on freepbx system is disabled, struggles to simultaneously answer his extension AND mute the system audio in a timely fashion, I thought this could be scriptable, turns out it can, but not easily.

Anyhoo, I will test the hooking solution and see if that gets him most of the way there.

(system) closed #16

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