How to change incoming CallerID

How to change incoming CallerID

(New material added March, 2009:) I should point out at the beginning that there is now an unsupported third-party module for FreePBX called Set CallerID, which "adds the ability to change the CallerID within a call flow." So, for example, you could create a Caller ID instance using this module, then make the destination of an Inbound Route that Caller ID instance. This basically accomplishes the same thing that is described below, but keeps you from having to mess around with adding contexts to extension_custom.conf.

Almost all of the examples below could be used with the Set CallerID module by doing the following: Set the CallerID Name to ${CALLERID(name)} so it will remain unchanged. Then set the CallerID Number to the target of the Set statement (the part after the = sign, but not including the close parenthesis). So, where in the first example below you see the line:

exten => _X!,1,Set(CALLERID(num)=0${CALLERID(num)})

If using the Set CallerID module you would take just the part in bold - 0${CALLERID(num)} - and use that as the CallerID Number.

If for some reason you don't wish to install the module, or it doesn't meet your requirements, the method described below still works.

(Original page starts here:) The following is an example of how to add a digit to incoming CallerID on a particular trunk - some people seem to want this because their phones will not do a proper callback if the leading digit(s) are missing.

Step 1: Go into the trunk for that provider (if not a Zap trunk-see below for Zap) and under Incoming Settings | USER details you will see a "context=" line - usually this will be "context=from-trunk". Note what is there now (if it is something other than "from-trunk") and change it to "from-trunk-custom", or add "-custom" to the end of whatever is there now

Step 2: Edit etc/asterisk/extensions_custom.conf (you can use nano, Midnight Commander's built-in editor, or other plain-text editor of your choice) and add the following at the bottom of the file:

[from-trunk-custom]
exten => _X!,1,Set(CALLERID(num)=0${CALLERID(num)})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)


Again, change the "from-trunk" in the first and third lines if you originally had something else in your trunk context. In the SECOND line, the 0 (following the =) is the digit to be added - if you want to add something other than 0, you'll have to change that.

Step 3: Reload or restart Asterisk

From CLI: reload (OR, if you want to restart Asterisk for some reason, you can use restart now, or restart when convenient if you don't want to drop calls. You could also do amportal restart from the Linux command line).

If you need to make this change for your Zap trunk, the procedure is slightly different:

Step 1: Edit etc/asterisk/zapata.conf (you can use nano, Midnight Commander's built-in editor, or other plain-text editor of your choice). You will see a "context=" line - usually this will be "context=from-zaptel". Note what is there now (if it is something other than "from-zaptel") and change it to "from-zaptel-custom", or add "-custom" to the end of whatever is there now.

Now continue with Steps 2 and 3 above, substituting "from-zaptel" (or whatever you found in the "context=" line in zapata.conf) in place of "from-trunk" in lines 1 and 3 of Step 2 (i.e., just replace "trunk" with "zaptel" in those lines).

Note that the variable ${CALLERID(num)} is only available in Asterisk 1.2 and later. If you need to manipulate it in other ways, see the section on String Handling Functions on the Asterisk variables page - for example, to always strip the first digit or character off of incoming Caller ID, you could probably use ${CALLERID(num):1} (adding the :1 to return all characters AFTER the character in the first position - this could also be used to strip a leading "+" if a provider adds that).

Here's another example, in which digits are both removed and added - in the FreePBX forum, colinjack wrote, "My SIP provider CallerID comes in the format +44123456789 but my database uses 0123456789 - so I needed to remove the '+44' and add a '0' to the callerid to allow it to query the user database." And he added this to extensions_custom.conf to accomplish this:

[from-trunk-custom]
exten => _X!,1,Set(CALLERID(num)=0${CALLERID(num):3:12})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)


Then he changed his sip trunk context statement to context=from-trunk-custom

One final example. In this case the provider was sending the CallerID number prefixed with a "+" (plus sign), but there was no assurance that this would always be true. In order to facilitate using the callback feature on certain phones, it was desired to strip the "+", but only for calls that appeared to be coming from points in the "North American Numbering Plan Area" (country code 1). So this code tests for "+1" at the start of the CallerID and if it is present, it strips only the first character (the "+"):

[from-trunk-custom]
exten => _X!,1,GotoIf($["${CALLERID(num):0:2}" != "+1"]?noplusatstart)
exten => _X!,n,NoOp(Changing Caller ID number from ${CALLERID(num)} to ${CALLERID(num):1})
exten => _X!,n,Set(CALLERID(num)=${CALLERID(num):1})
exten => _X!,n(noplusatstart),Goto(from-trunk,${EXTEN},1)


Once again the sip trunk context statement should be changed to context=from-trunk-custom

Just in case anyone wonders, you can use the same custom context (in extensions_custom.conf) for multiple trunks, as long as they originally had the same "context=" destination. Or, if you need to make different changes to the Caller ID from different trunks, then just make multiple custom contexts in extensions-custom.conf, and change the names slightly (e.g. you could call one from-trunk-add-0-custom and another from-trunk-strip-2-custom, or whatever - just make sure to use the same context name in the trunk "context=" line and in the header of your custom context).

Finally, for those using naftali5's Dialplan Injection module (which may not install or work correctly with the most recent versions of FreePBX), you can place these code fragments in a Dialplan Injection instead of extensions_custom.conf. Simply enter the lines beginning with the "Set" or "Goto" command, like this:

Set(CALLERID(num)=0${CALLERID(num)})
Goto(from-trunk,${EXTEN},1)


Then note the number of the dialplan injection (it will be printed in angle brackets next to the injection name in the list of injections), and in your trunk use context=injection-number where number is the number of the injection. For example, let's say you named the injection as "Change Caller ID", and after you created it you saw this in the right hand column: Change Caller ID <15> - in your trunk, you'd then use context=injection-15


Added comment by original author: There was a note that briefly appeared on this page that was mostly incorrect (because the person who wrote the note apparently missed the fact that the added from-trunk-custom context must be created by the user in etc/asterisk/extensions_custom.conf, which is NOT overwritten by FreePBX updates). The only part of that note that might have been valid was the idea that the line exten => _X!,n,Goto(from-trunk,${EXTEN},1) could perhaps be replaced by include => from-trunk. I make no comment on the validity of that because I have not tested it; however I did test the method I posted above and it did work. Also, I cannot offer an explanation for why some users seem to be having problems using this method with Trixbox; since Trixbox has been forked from FreePBX I suppose it may be possible that they are doing something different that causes the above not to work.