Built AGI Script for Least Cost Routing, don't know where to call it

In short, where in the lastest stable freePBX should I call an AGI script to:

  1. Not tear up existing macros (especially call recording)
  2. Set the trunk to use for outbound calling.
  3. Allow fallback to a SIP trunk by dialing 9 first.

The Long:

I have 128 Google Voice trunks in 128 different NPAs and need to:

  1. Record every call.
  2. Route outbound calls out the closest gv trunk based on the NPA dialed.
  3. Allow for 9 + 11 digits to pick a SIP trunk.

But with the “out of the box” setup of adding 128 outbound routes with the 128 trunks, if somebody calls an NPA down low in the list (like after order # 60 or so, Asterisk doesn’t complete the call. If they move the matching NPA up in the list, the call goes through so Asterisk is failing while checking soo many rules.

So I’ve written a AGI PHP script for that queries the dialed digits to return a trunk number, then runs the macro associated with that trunk to make the call

Right now I am overriding [outbound-allroutes-custom] with the following that I copied from extensions_additional.conf:

[email protected]:/etc/asterisk $ cat /etc/asterisk/extensions_override_freepbx.conf


[outbound-allroutes-custom]
exten => _.,1,NoOp(Running outbound_routes2.php)
exten => _NXXNXXXXXX,1,Macro(user-callerid,LIMIT,)
exten => _NXXNXXXXXX,n,Set(MOHCLASS=${IF($["${MOHCLASS}"=""]?default:${MOHCLASS})})
exten => _NXXNXXXXXX,n,Set(_NODEST=)
exten => _NXXNXXXXXX,n,Macro(record-enable,${AMPUSER},OUT,)
exten => _NXXNXXXXXX,n,AGI(outbound_routes2.php,1${EXTEN})

The script calls the correct trunk macro if one is found, and doesn’t if no match found.

Is there a better place to call the AGI script?

Calls are completing, but the customer is complaining that sporadically, it won’t record the calls and I’m getting the following logs on 99% of the calls:

res_agi.c: – <SIP/101-00000006>AGI Script outbound_routes2.php completed, returning 4

FreePBX records the majority of the calls even though 99% of the time I get back the “returning 4”.

Thanks a bunch,
Shanon