Customising Asterisk Dialplan for dummies

Today’s (this week’s!) project was to try to allow users to dial out from Asterisk, but prefix the phone number so they can dial out ‘as’ someone else. In our case we want to change the outgoing CLI and also the account code, so someone else on our A2Billing system gets billed for the call. The setup is that there are PA’s who take and make calls on behalf of different clients. The clients are to be billed for any calls a PA makes on their behalf.

The idea was to have a prefix per-client, starting *01* for client 1, *02* for client 2 etc. Whilst a database-linked solution would be better, I am not that good with asterisk dialplan yet, nor with PHP, so here is the down-and-dirty method.

One thing I learned whilst looking at the dialplan, a very useful feature is the dumpvars macro. You can add it into a context (such as the macro-dialout-trunk-predial-hook context in our case) to find out what the status of various variables are just where you are trying to program them. You call it like this:

exten => s,n,Macro(dumpvars)

Then go look in your asterisk logfile, do a search for dumpvars and you’ll find all your variables. The macro is also useful as a reference to what variables are called – I found it in extensions_custom.conf – YMMV.

If you are already using A2Billing on the same box as FreepPBX, you should have some custom stuff in extensions_custom.conf, including the macro-dialout-trunk-predial-hook section which sends the call to custom- freepbx-a2billing if the trunk matches “A2B/” – so don’t forget you will still need this! If your A2Billing is on a separate box, use IAX trunks and see my upcoming post about trunking with A2Billing.

One other requirement was that we only wanted some people to be able to do this. I tried creating a custom context but I’m not that good with Asterisk, so we ended up trying to match an accountcode of the outgoing caller, and skip the reprogramming if they aren’t in our allowed group. I tried to use a regex on the extension number, but I couldn’t find the actual extension number when I wanted it so accountcode seemed another good variable to use.

So I’ll just cut to the chase; here’s the code we ended up adding to extensions_custom.conf (the names have been changed to protect the guilty):

exten => s,1,NoOp(Outgoing number is ${CALLERID(dnid)})
exten => s,n,NoOp(CDR(Accountcode is ${CDR(Accountcode)})
exten => s,n,GotoIf($["${CDR(Accountcode)}" != "1344480313"]?skip)
exten => s,n,Gotoif($["${CALLERID(dnid):0:4}" = "*01*"]?FR01)
exten => s,n,Gotoif($["${CALLERID(dnid):0:4}" = "*02*"]?FR02)
exten => s,n,Goto(skip)

exten => s,n(FR01),NoOp(Call being charged to FR01)
exten => s,n,Set(CDR(Accountcode)=0727446623)
exten => s,n,Set(CALLERID(number)=01210000000)
exten => s,n,Goto(skip)

exten => s,n(FR02),NoOp(Call being charged to FR02)
exten => s,n,Set(CDR(Accountcode)=2823103263)
exten => s,n,Set(CALLERID(number)=01332000000)
exten => s,n,Goto(skip)

exten => s,n(skip),NoOp()
exten => s,n,GotoIf($["${OUT_${DIAL_TRUNK}:4:4}" = "A2B/"]?custom-freepbx-a2billing,${OUTNUM},1)
exten => s,n,MacroExit

UPDATE: On the second-to-last line, you need to remove ‘:2’ at the end, so that if for any reason you AREN’T using a trunk named “A2B/x” you don’t get stuck in a loop returning to line number 2 of the macro.
UPDATE2: After the first section, you need to add a Goto(skip) line so that if it doesn’t match anything, it will just skip and dial as normal; otherwise it will carry on to the first accountcode change which isn’t what you intented!
The above code is updated with these from-the-field updates.

1 thought on “Customising Asterisk Dialplan for dummies

Comments are closed.