Strip off Country and Area Code from incoming Caller ID (name)


i have following situation:

Incoming call for example:

  • “CallerID num” is 089123456 (area code 089 for Munich, Germany
  • “CallerID name” is +4989123456 (country +49 Germany and area code 89 for Munich)

What I want

  • Display the incoming “Caller ID number” of my hometown (i.e. Munich) without area code 089. So it will be only 123456
  • Display the incoming “Caller ID name” without country code +49 and without area code 89. So it will be also only 123456

Country code +49 should always be removed from “Caller ID number” and if it´s not a call from Munich it should be prefixed with 0 before Area Code.
Area Code 089 (or +4989) should alwys be removed from “Caller ID name”.

I tried this:

exten => _.,1,ExecIf($[ "${CALLERID(num):0:3}" = "+49" ]?Set(CALLERID(num)=0${CALLERID(num):3}))
exten => _.,1,ExecIf($[ "${CALLERID(num):0:3}" = "089" ]?Set(CALLERID(num)=${CALLERID(num):3}))
exten => _.,n,Set(CALLERID(name)=${CALLERID(num)})
exten => _.,n,Goto(from-trunk,${EXTEN},1)

and changed context in trunk to "[from-trunk-arearemove]

But it didn´t work. The CallerID name and CallerID number will always displayed as the code wouldn´t exist.

I have FreePBX 13 running on Raspberry PI 2.

Could anyone please help me?


We have “Set CallerID” module to do this.

Thanks for your reply. But I don´t understand how this works with the module. The guide also doesn´t help me. :frowning:

You have two priority 1’s in your code which will break it, but perhaps more robustly, I suggest you take the [from-pstn-e164-us] context in /etc/asterisk/extensiopns.conf as a template and build your own [from-pstn-e164-de] context to suit your particular dialing style and send inbound calls to that context, it will help with directories, callbacks and call presentation

explains well how the PSTN works in Germany.

The same source ( is always good if you are not DE, if you are UK it’s easy (apart from your granny who lives in Brampton :slight_smile: ) for IT it’s a pain.

You need to do some filtering (by calling/called number) in Incoming Routes then send a call to “Set CallerID” destination.
The sample “Set CallerID” entry:
CallerID Name ${CALLERID(name):4}
CallerID Number ${CALLERID(num):3}
Then you need to send the calls somewhere - the simple way is to specify the desired extension, ring group, etc as a Destination right here in “Set CallerID”.
You can also return a call back to your dialplan - create a Local trunk and specify it’s name as a destination here.
If you’re comfortable with the custom contexts - I fully support what was suggested by @dicko.

Thanks for your replys.

This is my solution:

In extensions_custom.conf I´ve added:

exten => _.,1,ExecIf($[ "${CALLERID(name):0:3}" = "+49" ]?Set(CALLERID(name)=0${CALLERID(name):3}))
exten => _.,n,ExecIf($[ "${CALLERID(num):0:3}" = "+49" ]?Set(CALLERID(num)=0${CALLERID(num):3}))
exten => _.,n,ExecIf($[ "${CALLERID(name):0:5}" = "02961" ]?Set(CALLERID(name)=${CALLERID(name):5}))
exten => _.,n,ExecIf($[ "${CALLERID(num):0:5}" = "02961" ]?Set(CALLERID(num)=${CALLERID(num):5}))

All tests worked until now.

There are good technical reasons NOT to use the context, from-pstn-custom in this way. I recommend that you change your trunk context to something like from-pstn-de then modify your code to:

exten => _.,1,ExecIf($[ "${CALLERID(name):0:3}" = "+49" ]?Set(CALLERID(name)=0${CALLERID(name):3}))
exten => _.,n,ExecIf($[ "${CALLERID(num):0:3}" = "+49" ]?Set(CALLERID(num)=0${CALLERID(num):3}))
exten => _.,n,ExecIf($[ "${CALLERID(name):0:5}" = "02961" ]?Set(CALLERID(name)=${CALLERID(name):5}))
exten => _.,n,ExecIf($[ "${CALLERID(num):0:5}" = "02961" ]?Set(CALLERID(num)=${CALLERID(num):5}))
exten => _.,n,goto(from-pstn,${EXTEN},1)
1 Like

Ok thank you very much. I will try it tomorrow.

Can you explain why it’s not good to use this context?

Because of the way the includes are arranged, every line that you add to from-pstn-custom will overwrite a generated line in the from-pstn context.

With the from-pstn-de context It’s working like a charm. Thank you so much!

1 Like

I came across this example when trying to figure out how to set CALLERID(num) and CALLERID(name) to “0” if they are “unknown”. Based on this post I created a custom context “from-trunk-unknown” in extensions_custom.conf, with the following lines:
exten => _.,1,ExecIf($[ “${CALLERID(name):0:7}” = “unknown”]?Set(CALLERID(name)=0))
exten => _.,n,ExecIf($[ “${CALLERID(num):0:7}” = “unknown”]?Set(CALLERID(num)=0))
exten => _.,n,Goto(from-trunk,${EXTEN},1)

and set context=from-trunk-unknown in the trunk SIP Settings under incoming. When I check /var/log/asterisk/full this context is never used after registering on “reload/apply”. Now im stuck trying to figure out how to set the CID info. Any hints on what I’m missing would be highly appreciated


1 - you should start your own thread instead of hooking onto an old one.
2 - make sure the context is getting pulled into the server. You can search for the context name in the ‘full’ logfile, or you can use the fwconsole context command to look at the contexts in your current system to see if it’s being pulled in or used.

I’m also using FreePBX framework version (which is up to date like yours), and am using the context “[ext-did-custom]” in the “/etc/asterisk/extensions_custom.conf” file for this purpose.

Thus, for a given trunk setup where I have defined for an exemplary SIP provider its user name to be “1036819” and a ring group definition called “600”, I have the following line entries in the “/etc/asterisk/extensions_custom.conf” file:

exten => 1036819,1,NoOp(CALLERID(num)=${CALLERID(num)})
exten => 1036819,n,NoOp(CALLERID(name)=${CALLERID(name)})
exten => 1036819,n,NoOp(EXTEN=${EXTEN})
exten => 1036819,n,NoOp(CHANNEL=${CHANNEL})
exten => 1036819,n,ExecIf($[$["${CALLERID(num):1:8}" = "nonymous"] | $["${CALLERID(num):1:6}" = "nknown"]] ?Set(CALLERID(num)=900000000))
exten => 1036819,n,Set(CALLERID(num)=${FILTER(0-9,${CALLERID(num)})})
exten => 1036819,n,ExecIf($["${CALLERID(num):0:2}" != "00" & "${CALLERID(num):0:1}" = "0" ] ?Set(CALLERID(num)=0044${CALLERID(num):1:12}))
exten => 1036819,n,ExecIf($["${CALLERID(num):0:4}" = "0044"] ?Set(CALLERID(num)=0${CALLERID(num):4:12}))
exten => 1036819,n,Set(__DIRECTION=INBOUND)
exten => 1036819,n,Gosub(sub-record-check,s,1(in,${EXTEN},dontcare))
exten => 1036819,n,Gosub(app-blacklist-check,s,1())
exten => 1036819,n,Set(__FROM_DID=${EXTEN})
exten => 1036819,n,Set(CDR(did)=${FROM_DID})
exten => 1036819,n,ExecIf($[ "${CALLERID(name)}" = "" ] ?Set(CALLERID(name)=${CALLERID(num)}))
exten => 1036819,n,Set(__MOHCLASS=)
exten => 1036819,n,Set(__REVERSAL_REJECT=FALSE)
exten => 1036819,n,GotoIf($["${REVERSAL_REJECT}"!="TRUE"]?post-reverse-charge)
exten => 1036819,n,GotoIf($["${CHANNEL(reversecharge)}"="1"]?macro-hangupcall)
exten => 1036819,n(post-reverse-charge),Noop()
exten => 1036819,n,Macro(privacy-mgr,3,10)
exten => 1036819,n(did-cid-hook),Noop(CallerID Entry Point)
exten => 1036819,n(dest-ext),Goto(from-did-direct,230,1)

In my case above:

  • I’m using the “NoOp” statements for debug purposes;

  • for any incoming calls with “unknown” or “anonymous” caller IDs*, I’m translating them to a specific number (so that I can manipulate them further in another context);

  • the next lines simply add and strip the international prefix (here the international dialling code for the UK) - I have a reason for this as some SIP providers do not provide the standard national codes;

  • and above last lines, I have copied them 1:1 from the “[ext-did-0001]” context in file “extensions_additional.conf”, where you find the default context definition for the bespoken “600” ring group.

Hope above concept sounds simple enough to carry on with your work in a similar direction.

Thanks for your input! Menawhile I solved my problem, everyone interested may check this post: Changing inbound CID name and num from "unknown" to "0" via custom dialplan context