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.
- Printer-friendly version
- Login or register to post comments



Yet another example
I somewhat accidentally removed the previous comments from this page - I say "somewhat" because I was trying to delete extraneous "bump" messages and accidentally killed all the comments, however this is not entirely unfortunate because all of them, other than one reply by me, were from a single user asking for help with a Trixbox installation (he also posted his questions in this Trixbox forum thread), and since Trixbox has forked with FreePBX I cannot say what would work with their software. However, one of my responses to that user had given yet another example - the commenter wanted to add a leading "0" to the Caller ID only if the received Caller ID was exactly 9 digits long. This is a slightly modified but untested example that I gave in response:
[from-trunk-custom] exten => _.,1,GotoIf($[${LEN(${CALLERID(num)})} != 9]?not9digits) exten => _.,n,Set(CALLERID(num)=0${CALLERID(num)}) exten => _.,n(not9digits),Goto(from-trunk,${EXTEN},1)While I'm not 100% sure of my syntax (especially on the first line) I believe it would work. The point is that you can set up conditional modifications of the Caller ID number based on length of the received string, or contents of a substring (as in the final "[from-trunk-custom]" example in the article) or both, by using conditional statements within your custom dial plan fragment. For more information see this page on Asterisk Variables and in particular, scroll down to the section on String Handling Functions.
Can you help with changing the caller id
Hello
I'd like to modify the from-zaptel-custom so that if the callerid is not known, it is set as (for example) 0123456789, otherwise a zero is prepended to the number.
I have this so far, to add the leading zero
[from-zaptel-custom]
exten => _X.,1,Set(CALLERID(num)=0${CALLERID(num)})
exten => _X.,n,Goto(from-zaptel,${EXTEN},1)
Are you able to help?
try this:
[from-zaptel-custom]
exten => _X.,1,GotoIf($[${LEN(${CALLERID(num)})} = 0]?unknownCID:addZero)
exten => _X.,n(unknownCID),Set(CALLERID(num)="0123456789")
exten => _X.,n,Goto(from-pstn,${EXTEN},1)
exten => _X.,n(addZero),Set(CALLERID(num)=0${CALLERID(num)})
exten => _X.,n,Goto(from-pstn,${EXTEN},1)
Brilliant
Thankyou very much fdecher, that works perfectly - though I did change from-pstn to from-zaptel (I assume that needed to be done as that is the context it was sent to previously)
CID number sometimes wrong??
I have a custom pstn macro which does add a zero or (if it comes from another old pbx removes the first digits) to the callerID Number:
[from-Tevitel-custom]
exten => _X.,1,GotoIf($["${CALLERID(num):0:7}" = "6154638"]?istIntern:nullDavor)
exten => _X.,n(istIntern),Set(CALLERID(num)=${CALLERID(num):7})
exten => _X.,n,Goto(from-pstn,${EXTEN},1)
exten => _X.,n(nullDavor),Set(CALLERID(num)=0${CALLERID(num)})
exten => _X.,n,Goto(from-pstn,${EXTEN},1)
The Macro works fine most of the time.
But sometimes the Macro sets a wrong number. (out of 1207 calls there are 65 wrong)
I have no clue why this happens.
the 1200 calls are the amount of calls for two days.
Is there an unsafe use of the variable CALLERID(num)?
Or is it a bug?
The Logfile says this (when it assigns the wrong number):
[Jan 20 17:05:10] VERBOSE[6547] logger.c: -- Accepting overlap call from '06257940233' to '638171' on channel 0/2, span 1
[Jan 20 17:05:10] VERBOSE[17016] logger.c: -- Starting simple switch on 'Zap/2-1'
[Jan 20 17:05:13] DEBUG[17016] chan_dahdi.c: Echo cancellation already on
[Jan 20 17:05:13] VERBOSE[17016] logger.c: -- Executing [638171@from-Tevitel-custom:1] GotoIf("Zap/2-1", "0?istIntern:nullDavor") in new stack
[Jan 20 17:05:13] VERBOSE[17016] logger.c: -- Goto (from-Tevitel-custom,638171,4)
[Jan 20 17:05:13] VERBOSE[17016] logger.c: -- Executing [638171@from-Tevitel-custom:4] Set("Zap/2-1", "CALLERID(num)=0638201") in new stack
[Jan 20 17:05:13] VERBOSE[17016] logger.c: -- Executing [638171@from-
Tevitel-custom:5] Goto("Zap/2-1", "from-pstn|638171|1") in new stack
[Jan 20 17:05:13] VERBOSE[17016] logger.c: -- Goto (from-pstn,638171,1)
The number 06257940233 calls 638171 and gets the callerid number 0638201???
But why? and where does the 638201 comes from?
And why does it work most of the time and sometimes not?
Help
I am new to the nuts and bolts of asterisk dial plans having been content to run Trixbox 2.6.2 using the GUI for setup, but I have dived in and am getting a basic feel for the logic and syntax because I have need to remove the plus sign from CID numbers. I did what is outlined above and found it did not work.
Following the call setup flow I found that regardless of what context is entered in the user details of a trunk, [from-sip-external] located in extensions.conf is called when a new call comes in on a trunk.
Because I have allow anonymous sip calls enabled [from-sip-external] was transferring the call flow directly to [from-trunk] without going to [from-trunk-custom] first. This is the code I found in [from-sip-external]:
[from-sip-external]
;give external sip users congestion and hangup
; Yes. This is _really_ meant to be _. - I know asterisk whinges about it, but
; I do know what I'm doing. This is correct.
exten => _.,1,NoOp(Received incoming SIP connection from unknown peer to ${EXTEN})
exten => _.,n,Set(DID=${IF($["${EXTEN:1:2}"=""]?s:${EXTEN})})
exten => _.,n,Goto(s,1)
exten => s,1,GotoIf($["${ALLOW_SIP_ANON}"="yes"]?from-trunk,${DID},1)
exten => s,n,Set(TIMEOUT(absolute)=15)
exten => s,n,Answer
exten => s,n,Wait(2)
exten => s,n,Playback(ss-noservice)
exten => s,n,Playtones(congestion)
exten => s,n,Congestion(5)
exten => h,1,NoOp(Hangup)
exten => i,1,NoOp(Invalid)
exten => t,1,NoOp(Timeout)
I changed from-trunk to from-trunk-custom on the eighth line and everything works as it should.
Can someone give me some insight into this? Is it my lack of knowledge making me miss something or is it a Trixbox quirk? Could someone suggest a way round this without having to edit extensions.conf? I wonder if [from-sip-external] should be looking up the actual context on the trunk and using this rather than assuming that the context will be [from-trunk].
Thanks.
The most logical reason for
The most logical reason for this behavior is that something is wrong with your trunk setup - FreePBX is not recognizing the incoming calls as being associated with that particular trunk so it's sending them to a general treatment. You should never change anything in from-sip-external - it may work now, but it probably won't at some point in the future (after a system update). The correct solution is to figure out why your trunk isn't working properly for incoming calls.
Of course, what I've said above applies to FreePBX, but not necessarily to the Trixbox forked version. We have no idea what they are doing, or why things might not work as expected there.
Thanks
Yes that was the problem, can you recommend a tutorial on setting up sip trunks that explains what is happening and how asterisk deals with it? I have Googled around but found information very sparse.
Unfortunately, setting up a
Unfortunately, setting up a SIP trunk can be as much an art as science, since it seems every provider has a slightly different setup, therefore there's no one way that works with all providers. In fact, two different FreePBX installations can be connecting to the SAME provider and need different trunk settings, just to resolve some problem that may affect one system but not another.
You can go to Howto: Setting up VOIP Provider Trunks to find examples of trunk setups for various providers. You may also want to read HOWTO: Route Dial Patterns and Trunk Dial Rules and How to get the DID of a SIP trunk when the provider doesn't send it (and why some incoming SIP calls fail), both of which deal with particular nuances of trunk setup. If you are using Elastix I suggest reading Elastix Without Tears, or if PBX in a Flash then PiaF without Tears (oh, that's right, you said you were using that distribution that forked FreePBX and went off in their own direction - much as it pains me to post the link, there's a book for that, too). Personally, I suggest using one of the first two books (all are free in .pdf format) and maybe the distribution that goes with it, so you are using genuine FreePBX.
not much use unless you happen to be one of these providers
Is there a general VOIP trunk howto rather than being tied to specific providers?
The help on the site seems to be far too specific to the specific type of install the person writing at the time used rather than a more general explanation of what to do and why.
I have not yet found anything on the site I can use.
What if your carrier doesnt give you a Trunk
Some carriers send the incoming call directly to your IP where there is no Trunk created, what do you do then? i tried
[from-trunk-custom]
exten => _X!,1,Set(CALLERID(num)=0${CALLERID(num)})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)
but it doesnt work
How are you receiving the call without a trunk?
If the call is coming in as anonymous SIP or something like that, you need to figure out what context the call is being sent to and see if there's a way to intercept that. But let's start with basics - are you SURE there's no trunk configuration for your provider? See Howto: Setting up VOIP Provider Trunks and see if your provider is listed. If not, go to the CLI (use asterisk -rvvvv from the Linux command prompt), place an incoming call and watch how it comes in - you should be able to see how it's entering the system. If all else fails, cut and paste the CLI output (remove any sensitive information such as phone numbers you don't want displayed) and maybe we can see how the call is coming into your system.
Hello, I followed the
No, you did NOT follow the
No, you did NOT follow the directions from the top post, otherwise you would not be caught in an endless loop.
Since you didn't put any more thought into your post than a simple cut and paste of the CLI output, that's all the help you're getting from me. Stop and think about what you DIDN'T include in your post that might have got you a more specific response.
Who knew.... RTFM
Who knew.... RTFM works.
Thank you.
On a side not, anyone know how I can apply this in the following scenario:
Our current setup is as follows:
Call come into Q.
It rings (say 1 person)'s desk phone, and forward to his cell using voicepulse.
What we need is for it to show as 066-Their number ONLY on the cell phone.
FreePBX module settings work for me, but
Thanks for HOWTO.
I followed your explanation:
"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." - works pretty well, but FreePBX System Status says:
There are 1 bad destinations
DEST STATUS: CUSTOM
Inbound Route: (/)
Added 7 minutes ago
(retrieve_conf.BADDEST)
(Maybe this is the reason why:
VERBOSE[9547] netsock.c: == Using SIP RTP TOS bits 184
VERBOSE[9547] netsock.c: == Using SIP RTP CoS mark 5
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:1] Set("SIP/GSMGW-00000002", "__FROM_DID=s") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:2] Gosub("SIP/GSMGW-00000002", "app-blacklist-check,s,1") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@app-blacklist-check:1] GotoIf("SIP/GSMGW-00000002", "0?blacklisted") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@app-blacklist-check:2] Set("SIP/GSMGW-00000002", "CALLED_BLACKLIST=1") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@app-blacklist-check:3] Return("SIP/GSMGW-00000002", "") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:3] ExecIf("SIP/GSMGW-00000002", "1 ?Set(CALLERID(name)=420xxxxxxxxx)") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:4] Set("SIP/GSMGW-00000002", "FAX_RX=disabled") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:5] Set("SIP/GSMGW-00000002", "__CALLINGPRES_SV=allowed_not_screened") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:6] Set("SIP/GSMGW-00000002", "CALLERPRES()=allowed_not_screened") in new stack
VERBOSE[9642] pbx.c: -- Executing [s@from-trunk:7] Goto("SIP/GSMGW-00000002", "app-setcid,7,1") in new stack
VERBOSE[9642] pbx.c: -- Goto (app-setcid,7,1)
WARNING[9642] pbx.c: Error in extension logic (missing '}')
WARNING[9642] pbx.c: Can't find trailing parenthesis for function 'CALLERID(nu'?
ERROR[9642] func_callerid.c: Unknown callerid data type 'nu'.
VERBOSE[9642] pbx.c: -- Executing [7@app-setcid:1] NoOp("SIP/GSMGW-00000002", "Changing Callerid from "420xxxxxxxxx" <420xxxxxxxxx> to CALLERID(name): 420xxxxxxxxx, CALLERID(num): 0") in new stack
VERBOSE[9642] pbx.c: -- Executing [7@app-setcid:2] Set("SIP/GSMGW-00000002", "CALLERID(name)=420xxxxxxxxx") in new stack
VERBOSE[9642] pbx.c: -- Executing [7@app-setcid:3] Set("SIP/GSMGW-00000002", "CALLERID(num)=0420xxxxxxxxx") in new stack
..)
The second method:
"Step 1: Go into the trunk Incoming Settings ..." - I had to change both INCOMING and OUTGOING context in FreePBX trunk definition screen to "from-trunk-custom", else all trunk incoming calls were from "from-trunk" context (to be true I didn't try to change OUTGOING context only).
Maybe my troubleshooting will save somebody's time.
Last but no least - versions:
Asterisk 1.6.2.0, FreePBX 2.6.0.1
we want to append in start
we want to append in start four digits with every incoming number and we want to display only four digits on those calls whose comes with alphabets. We set the following rules
exten => _X!,1,Set(CALLERID(num)=1666${CALLERID(num)})
exten => _X!,1,GotoIf($["${CALLERID(num)}" = "Unknown"]?changeCLI)
exten => _X!,s(changeCLI),Set(CALLERID(num)=1666)
these rules works fine for numeric cli but if we get abcd in cli then calls goes with 1666abcd and our carrier rejected that call because they only accept numaric cli. We just want to send 1666 instead of 1666abcd.
thanks in advance and your prompt reply will be highly appreciated.
Ahmad
Caller Selected Number
Hey there,
I'm extremely new to Asterisk, and I'm trying to figure out how exactly to make it do what I want. Your guide here has gotten me started down the right road, but what I need to do is allow the caller to enter the number they want to dial from. They need to be able to call in, get a voice prompt, dial a number, and have their caller ID be set to that particular number.
Any advice or direction you can give would be much appreciated, I'm lost to the point where I don't even necessarily know what to look for or ask beyond this.
Thanks!
Digium Skype Module
Hi I tried this with the Digium Skype module.
It does not work, the skype module uses from-pstn,
so my code used the context from-pstn-custom
this is what I see in the CLI
Starting Skype/myskypedid-08a23968 at from-pstn-custom,myskypedid,1 failed so falling back to exten 's'
Any help / thoughts welcome…
currently asterisk 1.8 and Freepbx 2.8 on pbxinaflash…
I have done the following test configs…
I have found no way to preappend the inbound number I have tried below…
but the trunk fails totally unless the second line is
include => from-pstn
this fails…
exten => _X.,1,Set(CALLERID(all)=SGU-Sales:${CALLERID(name)} <${CALLERID(num)}>)
exten => _X.,n,Goto(from-pstn,${EXTEN},1)
this works..
exten => _X.,1,Set(CALLERID(all)=SGU-Sales:${CALLERID(name)} <${CALLERID(num)}>)
include => from-pstn
this works.. which I think shows the first line is being ignored….
[from-pstn-custom]
exten => _X!,1,Set(CALLERID(all)=7test>)
include => from-pstn
this does not
exten => _X!,1,Set(CALLERID(num)=0${CALLERID(num)})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)
this does work
[from-pstn-custom]
exten => _X!,1,Set(CALLERID(num)=0${CALLERID(num)})
include => from-pstn
Thanks...
remove leading 1
I hope someone "in the know" is still watching this thread.
I am trying to remove the leading 1 from inbound CLID's before sending the call off to a2billing. I have one provider that sends for example 16045856291 and I need it to be 6045856291, all my other providers send 10 digits so I get 6045856291
I have tried some of the examples here but have had no luck!
I am running:
freepbx 2.8.1.4
piaf 1.7.5.5
asterisk 1.4.21.2
Conditional changing of CallerID
Depending on the incoming CallerID on any extension, i'd like to change it to a fixed value. I've tried the IF function, but it is not usable in an extension. Here's my code:
[from-trunk-custom] exten => _X!,1,IF(${CALLERID(num)} = 01234567899 ? Set(CALLERID(num)=09876543219)) exten => _X!,n,Set(CALLERID(name)=${CALLERID(num)}) exten => _X!,n,Goto(from-trunk,${EXTEN},1)I want to use this in order to achieve something called "fixed/mobile convergene", any call coming in from a known mobile should be converted to a specific landline number. What is the way to do it, since the official Asterisk documentation doesn't provide sufficient information on how and where the IF function can/should be used.