Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active May 30, 2021 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaetz/e7dbbcd6004559c5b467a146a8b14cfc to your computer and use it in GitHub Desktop.
Save lgaetz/e7dbbcd6004559c5b467a146a8b14cfc to your computer and use it in GitHub Desktop.
; Name: lgaetz-boomerang
;
; Latest version: https://gist.github.com/lgaetz/e7dbbcd6004559c5b467a146a8b14cfc
;
; Description: this dialplan Stores the most recent local extension number to speak with the referenced CallerID Number
; To direct an inbound call based on the last extension they last spoke to, do astdb query on the key/family
; boomerang/${CALLERID(number):-10}
; if set, will give most recent local exten. Something like dynamic routes could be used to branch call flow
; based on the returned result
; license: GNU GPL3+
;
; Version history:
; 2021-05-29 First commit astdb writes rough but working
[from-pstn-boomerang]
; add hangup handler to inbound channel, trunks must use this context
exten => _.,1,Noop(Entering user defined context from-pstn-boomerang in extensions_custom.conf)
exten => _.,n,Set(CHANNEL(hangup_handler_push)=boomerang-tag-call,s,inbound)
exten => _.,n,Goto(from-pstn,${EXTEN},1)
[macro-dialout-trunk-predial-hook]
; add hangup handler to outbound call
; using this context is not ideal, the hangup handler is on the dialing channel, not the dialed channel.
; to work better needs to be spliced to func-apply-sipheaders
exten => s,1,Noop(Entering user defined context macro-dialout-trunk-predial-hook in extensions_custom.conf)
exten => s,n,Set(CHANNEL(hangup_handler_push)=boomerang-tag-call,s,outbound)
exten => s,n,MacroExit
[boomerang-tag-call]
exten => s,1,Noop(Entering user defined context boomerang-tag-call in extensions_custom.conf)
; exten => s,n,DumpChan()
; exten => s,n,Noop(CID ${CALLERID(number)})
; exten => s,n,Noop(DIALEDPEERNUMBER ${DIALEDPEERNUMBER})
; exten => s,n,Noop(DNIS ${CONNECTEDLINE(number)})
exten => s,n,GoToIf($["${DIRECTION}"="INBOUND"]?inbound)
exten => s,n,GoToIf($["${OUTNUM}"!=""]?outbound)
exten => s,n,Return ; give up
exten => s,n(inbound),Noop(Inbound call)
; using dialed peer number to determine what exten was the most recent to talk to this cid, format is "6002" chan_sip, and "6003/sip:6003@10.8.0.7:5060;ob" for chan_pjsip;
exten => s,n,set(extnum=${CUT(DIALEDPEERNUMBER,/,1)}) ; numeric extension number
; if extnum string exists as a local extension, write to astdb with final 10 digits of incoming CID number
exten => s,n,ExecIf($[${DB_EXISTS(AMPUSER/${extnum}/device)}]?Set(DB(boomerang/${CALLERID(number):-10})=${extnum}))
exten => s,n,Return
exten => s,n(outbound),Noop(Outbound call)
; if AMPUSER string exists as a local extension, write to astdb with final 10 digits of conected CID number
; again, not ideal only the original dialing extension is stored, not necessarily the most recent
exten => s,n,ExecIf($[${DB_EXISTS(AMPUSER/${AMPUSER}/device)}]?Set(DB(boomerang/${CONNECTEDLINE(number):-10})=${AMPUSER}))
exten => s,n,Return
@lgaetz
Copy link
Author

lgaetz commented May 29, 2021

2021-05-29 very much a work in progress, problems:

  • hangup handler on outbound calls is set on dialing channel, not dialed channel. If the call is transferred after dialing out, the orig extension is in database
  • values are stored in ASTDB without any date so can't purge as they get stale. Also query will result in any call, not necessarily a recent one
  • relies on connectedline to work for outbound, so not sure what will happen if not enabled in trunk config
  • I think to do this properly, need to use mysql and an AGI to read/write values. Could then add maintenance to clean up old entries and also use a query to only return results written over last xxx days.

@billsimon
Copy link

Rather than AGI, I think you could still keep it all in dialplan but add mysql with timestamps by using func_odbc. A single function in func_odbc with a read query and a write query might be enough.

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