ok, this works perfectly, but applies the header to all calls.
I can easily tweak that to check the outbound route name.
But what if Skyetel is down and the route fails over to my Twilio trunk, or VoIP.ms, etc?
Obviously they don’t care about that header and I would expect the calls to terminate anyway. But it just feels dirty to shoehorn that header in there always.
It would be nice to have something that is only applied on the trunk.
Edit: I say that mostly for others, because for my example, I am using this functionality for departments. I will have to apply this header based on the outbound route used, as there is only 1 trunk setup and I want 4 different departments tracked with this tenant functionality…
Here is my final solution. I welcome improvements.
Caveats:
The trunk name is Skyetel
NoOp Traces in Dialplan set to 1 in Advanced Settings
All outbounds routes start with known values to match against.
[macro-dialout-trunk-predial-hook]
; Much thanks to Lorne Gaetz with Sangoma for answering questions.
; https://community.freepbx.org/t/need-to-add-sip-header-to-outbound-calls-on-pjsip-trunk/60441/4
exten => s,1,NoOp(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf)
; Get trunk name. If it is Skyetel determine a tenant, if not, exit.
exten => s,n,ExecIF($["${OUT_${DIAL_TRUNK}_SUFFIX}"!=""]?Set(trunk_name=${OUT_${DIAL_TRUNK}_SUFFIX}):Set(trunk_name=${OUT_${DIAL_TRUNK}}))
exten => s,n,GotoIf($["${trunk_name}"=="@Skyetel"]?determine_tenant:exit_macro)
; Based on outbound route name, add a SIP header X-Tenant=X
exten => s,n(determine_tenant),NoOp(We are heading for the Skyetel Trunk, determine the X-Tenant based on outbound route)
; Set x_tenant = to tenant.stl by default.
exten => s,n,Set(x_tenant=tenant.stl)
; Change it if the call is on a Cape, Jeff City, or Quincy outbound route.
exten => s,n,ExecIf($["${OUTBOUND_ROUTE_NAME:0:4}"=="Cape"]?Set(x_tenant=tenant.cape))
exten => s,n,ExecIf($["${OUTBOUND_ROUTE_NAME:0:9}"=="Jeff City"]?Set(x_tenant=tenant.jeffcity))
exten => s,n,ExecIf($["${OUTBOUND_ROUTE_NAME:0:6}"=="Quincy"]?Set(x_tenant=tenant.quincy))
exten => s,n,NoOp(Based on the outbound route name of ${OUTBOUND_ROUTE_NAME}, the X-Tenant is being set to ${x_tenant}.)
exten => s,n,GoSub(func-set-sipheader,s,1(X-Tenant,${x_tenant}))
exten => s,n(exit_macro),MacroExit()