Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active May 22, 2025 20:36
Show Gist options
  • Save lgaetz/1d75bab3c975f0b6b54071a7c7c07d6d to your computer and use it in GitHub Desktop.
Save lgaetz/1d75bab3c975f0b6b54071a7c7c07d6d to your computer and use it in GitHub Desktop.
Get FreePBX AMPUSER from Asterisk channel
; Asterisk dialplan sub that takes an Asterisk channel name and returns the matching FreePBX AMPUSER or null if no matching user
;
; License GNU/GPL3+
;
; Latest Version: https://gist.github.com/lgaetz/1d75bab3c975f0b6b54071a7c7c07d6d
;
; Usage: When writing asterisk dialplan for fpbx, reference the subroutine and pass the channel name as ARG1
; Channel name format follows something like PJSIP/4002-0000004f
; Example dialplan:
; exten => s,n,Gosub(get-AMPUSER-from-asterisk-channel,s,1(${DYNAMIC_WHO_ACTIVATED}))
; exten => s,n,Noop(Found FreePBX AMPUSER: ${GOSUB_RETVAL})
;
; Version 2025-05-22 First commit
[get-AMPUSER-from-asterisk-channel]
exten => s,1,Noop(Attempting to match FreePBX user with Channel ${ARG1})
exten => s,n,ExecIf($["${ARG1}"=""]?Return()) ; return immediately if no channel name passed
; strip trailing random identifier and hypen from channel name to end up with just the dialing channel i.e. PJSIP/4002
exten => s,n,Set(DIALING_CHANNEL=${CUT(ARG1,-,1)})
exten => s,n,Set(DEVICES=${DB_KEYS(DEVICE)}) ; get a full commma delimited list of all dialable devices on system
; step thru the database dial entry for each dialable device looking for match to dialing channel
exten => s,n,Set(foo=)
exten => s,n,While($["${SET(DEV=${POP(DEVICES)})}" != ""])
; see if the dial string for the device matches the dialing channel part of the channel name
exten => s,n,ExecIf($["${DB(DEVICE/${DEV}/dial)}"="${DIALING_CHANNEL}"]?Set(foo=${DB(DEVICE/${DEV}/user)}))
exten => s,n,ExecIF($["${foo}"!=""]?ExitWhile)
exten => s,n,EndWhile()
exten => s,n,Return(${foo})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment