Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
; FreePBX Feature code prefix to allow spy/whisper/barge on
; the specified extension.
;
; Latest version:
; https://gist.github.com/lgaetz/78c4e114952e79596c1ed4123559d3d3
;
; Usage:
; Dialplan goes in the file:
; /etc/asterisk/extensions_custom.conf
; Dial local extension with 556 prefix to spy. While spying on
; active channel use the following dtmf input to toggle modes:
; dtmf 4 - spy mode
; 5 - whisper mode
; 6 - barge mode
;
; License: GNU GPL2
;
; Version History
; 2018-11-23 First commit by lgaetz
; 2019-03-05 update regex to prevent channels from returning multiple channel matches
; 2019-10-10 added support to loop thru all dialable devices for the extension, now supports Zulu/webrtc clients
; 2020-04-12 COVID-19 edition - supports prompt to request spyee extension
; 2020-07-23 add E option to ChanSpy application and add timeout
; 2021-03-08 add example line to block spying access to certain extensions
[from-internal-custom]
exten => 556,1,Noop(Entering user defined context from-internal-custom in extensions_custom.conf)
exten => 556,n,Read(spyee,please-enter-the&extension&number&followed_pound)
exten => 556,n,GoTo(targeted-chanspy,${spyee},1)
exten => _556.,1,Noop(Entering user defined context from-internal-custom in extensions_custom.conf)
exten => _556.,n,GoTo(targeted-chanspy,${EXTEN:3},1) ;strip off prefix
[targeted-chanspy]
exten => _.,1,Noop(Entering user defined context targeted-chanspy in extensions_custom.conf)
; exten => _.,n,ExecIF($["${EXTEN}"="100"]?Hangup) ; if you want to block spying access to certain extensions you can do that with this line, one per ext
exten => _.,n,Set(TIMEOUT(absolute)=3600) ; prevent hung channel by setting 1 hour timeout recommended if using infinite loop
exten => _.,n,Answer
exten => _.,n(once-upon-a-time),Wait(1)
exten => _.,n,set(spy_target=) ; initialize target var
; get list of dialable devices for extension
exten => _.,n,Set(DEVS=${DB(AMPUSER/${EXTEN}/device)}) ; & delimited list of devices
exten => _.,n,Set(DEVS=${STRREPLACE(DEVS,&,\,)}) ; comma delimited list of devices
; step thru each device and look for an active channel
exten => _.,n,While($["${SET(DEV=${POP(DEVS)})}" != ""])
exten => _.,n,noop(dev: ${DEV})
; using a regex of SIP/${EXTEN}- will match both SIP and PJSIP channels, and the trailing - character should
; help to ensure there is only a single match. If multiple channels are returned the chanspy application will fail
exten => _.,n,set(spy_target=${CHANNELS(SIP/${DEV}-)})
exten => _.,n,ExecIf($["${spy_target}"!=""]?ExitWhile) ; if an active channel is found exit the loop
exten => _.,n,EndWhile()
exten => _.,n,ExecIF($["${spy_target}"!=""]?ChanSpy(${spy_target},dnqE)) ; q option suppresses channel announce on barge
exten => _.,n,Hangup() ;comment this line with a semicolon to do infinite loop
exten => _.,n,GoTo(once-upon-a-time)
exten => _.,n,Hangup()
exten => h,1,Hangup()
exten => s,1,Hangup()
exten => T,1,Hangup() ; timeout destination
@ben-itse

This comment has been minimized.

Copy link

@ben-itse ben-itse commented Apr 10, 2020

Hi @lgaetz.. love your code! Big fan, actually.

One issue I am having after implementing it in a FreePBX15 server, is that when I dial 556, it says "All circuits are busy now, please try your call again later". However, if I dial 556+extension it works great. I am bringing this up because from the code it looks like it should prompt and wait for the user to input the extension it wants to listen to. Is this right, or am I missing something? Thank you in advance for your assistance.

Sincerely,

Ben from ITSE

@lgaetz

This comment has been minimized.

Copy link
Owner Author

@lgaetz lgaetz commented Apr 10, 2020

Hi @ben-itse

Thanks for the kudos. As written, this is ONLY a prefix. Head over to the FreePBX forum if you need help modifying it for your own purposes.
https://community.freepbx.org/

edit - feature added.

@nicolabertollini

This comment has been minimized.

Copy link

@nicolabertollini nicolabertollini commented Feb 17, 2021

Hi there, I am new to FreePbx and Asterisk and i don't know where should I paste the text above to activate Chanspy.
I want to listen to agents not through Fop2 but just by dialing 555 and the extension on the softphone. Can you please send me some info or a video how to do this? Thanks.

@lgaetz

This comment has been minimized.

Copy link
Owner Author

@lgaetz lgaetz commented Feb 17, 2021

Hi @nicolabertollini the content goes in extensions_custom.conf, but if you're new to FreePBX you should probably head over to the FreePBX forum for more complete instructions.

https://community.freepbx.org/

@nicolabertollini

This comment has been minimized.

Copy link

@nicolabertollini nicolabertollini commented Feb 17, 2021

Thank you.

@jonathanpisarczyk

This comment has been minimized.

Copy link

@jonathanpisarczyk jonathanpisarczyk commented Feb 18, 2021

Works Great! Thanks! Would this work between two different FreePBX servers over PJSIP trunks?

@lgaetz

This comment has been minimized.

Copy link
Owner Author

@lgaetz lgaetz commented Feb 18, 2021

I would expect it to work if the dialplan is on the server with the spied extension not on the dialing extension.

@adell444

This comment has been minimized.

Copy link

@adell444 adell444 commented Mar 4, 2021

Hi @lgaetz,

This code is terrific and works perfectly, thank you very much for posting.

How would I go about limiting which extensions/channels could be listened to? For example, I don't ever want extension 100 to have their calls listened/whispered/barged on. Is that possible?

Thank you!
Andrew

@lgaetz

This comment has been minimized.

Copy link
Owner Author

@lgaetz lgaetz commented Mar 4, 2021

Hi @adell444

Yeah, there should be ways to do that. Raise a new discussion at https://community.freepbx.org/

edit - gist updated 2021-03008 support added

@AlexAkimbo

This comment has been minimized.

Copy link

@AlexAkimbo AlexAkimbo commented Apr 8, 2021

Hi @lgaetz,

This code is terrific and works perfectly, thank you very much for posting.

How would I go about limiting which extensions/channels could be listened to? For example, I don't ever want extension 100 to have their calls listened/whispered/barged on. Is that possible?

Thank you!
Andrew

Look for the line:
; exten => _.,n,ExecIF($["${EXTEN}"="100"]?Hangup) ; if you want to block spying access to certain extensions you can do that with this line, one per ext

You have to uncomment this one and duplicate it for every extension you want to disable those options.

For example if you have extension s 1501, 1502 and 1503 for which you want to disable spy option you have to add following lines in [targeted-chanspy] section:
exten => _.,n,ExecIF($["${EXTEN}"="1501"]?Hangup)
exten => _.,n,ExecIF($["${EXTEN}"="1502"]?Hangup)
exten => _.,n,ExecIF($["${EXTEN}"="1503"]?Hangup)

Alex

@adell444

This comment has been minimized.

Copy link

@adell444 adell444 commented Apr 8, 2021

Look for the line:
; exten => _.,n,ExecIF($["${EXTEN}"="100"]?Hangup) ; if you want to block spying access to certain extensions you can do that with this line, one per ext

You have to uncomment this one and duplicate it for every extension you want to disable those options.

For example if you have extension s 1501, 1502 and 1503 for which you want to disable spy option you have to add following lines in [targeted-chanspy] section:
exten => _.,n,ExecIF($["${EXTEN}"="1501"]?Hangup)
exten => _.,n,ExecIF($["${EXTEN}"="1502"]?Hangup)
exten => _.,n,ExecIF($["${EXTEN}"="1503"]?Hangup)

Alex

Yeah that line was added in his March 8 revision after I opened this forum post about it. Works a charm.

@ben-itse

This comment has been minimized.

Copy link

@ben-itse ben-itse commented Jul 12, 2021

@lgaetz - hope all is well! Wanted to run something by you:

I wanted to change the prefix to be *222 specifically. I replaced 556 with *222 in all entries under [from-internal-custom]. It works but only when dialing the feature code and then I get prompted to enter the extension number followed by #.

However, it does not work when I try to speed dial the code. For example, if I want to spy on extension 204, and I dial *222204, it connects but then immediately hangs up. However, if I switch the prefix back to 556, then I can dial 556204 and it connects just fine.

Is the issue here the * ?

Any help/guidance will be greatly appreciated.

@lgaetz

This comment has been minimized.

Copy link
Owner Author

@lgaetz lgaetz commented Jul 12, 2021

You are changing the prefix from 3 to 4 characters, so you'll need to strip 4 characters from the dial string before going to targeted-chanspy

exten => _556.,n,GoTo(targeted-chanspy,${EXTEN:4},1)      ;strip off 4 digit prefix 

If that's not it, then start a thread at https://community.freepbx.org/ with details

@ben-itse

This comment has been minimized.

Copy link

@ben-itse ben-itse commented Jul 12, 2021

You are changing the prefix from 3 to 4 characters, so you'll need to strip 4 characters from the dial string before going to targeted-chanspy

exten => _556.,n,GoTo(targeted-chanspy,${EXTEN:4},1)      ;strip off 4 digit prefix 

If that's not it, then start a thread at https://community.freepbx.org/ with details

That's it! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment