Routing inbound calls based on CID Lookup Source

I’ve got freepbx distro version 2.11 on asterisk 11.7.0.

I’m trying to route calls based on the results of the CID Lookup Source. This returns either Cust: or New:. If the CID starts with Cust they should go to a queue and if it starts with New: they should go to a Ring Group. If they don’t have a CID that’s ok, this doesn’t need to be 100% correct.

I understand that using the CID in the Inbound Route isn’t going to work, so I need to change the Inbound Route destination to a module that makes the choice.

I’ve found the Dynamic Route module, which is close to what I want but instead of SQL lookup all I want is a CID lookup.

Is there a way to do this? Am I going about it in the right way?


Ok, I found it.

I need to create a custom destination. Here’s what I did:

Edit the file /etc/asterisk/extensions_custom.conf

Add the following:


exten => s,1,GotoIf($["${CALLERID(name):0:3}" = “Sup”]?ext-queues,700,1)
exten => s,n,GotoIf($["${CALLERID(name):0:10}" = “Sales:Cust”]?ext-queues,700,1)
exten => s,n,Goto(ext-group,600,1)

Now in the web admin go to admin -> custom destination.

Add a destination called custom-split-sales-support,s,1 and give it a name.

Go to your upstream module, might be the inbound route or in my case it is a time condition. Set the destination to custom destination and pick the destination name you just created.

A bit of explanation.

On my system there are two inbound routes for my two telephone numbers. They both have a custom cid lookup which is an https call to our crm system. The prepends either Cust: or New: to the caller id. Then the inbound route prepends Sales: or Sup: to the caller id. So calls end out looking like:

Sup:New:12345444 (a call to the support number from a number that’s not in crm)
Sales:Cust:13344343 (a call to the sales line from a customer)

I want to route calls from unknown numbers to the sales team to a specific call group (ring everyone, there’s money on the phone!) and calls to the support line or from customers to the sales line to the queue (they can wait).

In my setup the ring group is extension 600 and my queue is extension 700. So going back to the magic in the file. This is a dial plan, which is essential what freepbx creates for asterisk and having looked at it a bit I really love freepbx.

Line by line:


This is the context which is how asterisk and freepbx label this set of rules.

exten => s,1,GotoIf($["${CALLERID(name):0:3}" = “Sup”]?ext-queues,700,1)

exten => is just a standard part of the asterisk syntax
I’m not sure what the s is. I think it just means this isn’t a feature code or an extension you can dial.
The 1 means this is the first rule.
GotoIf says the if the part before the ? is true, then goto the part after the ?
$["${CALLERID(name):0:3}" = “Sup” means look at the name part of the caller id (I had it as num before which didn’t work), from the 0th character to the 3rd character (or it might be 3 characters) and see if it matches Sup, which mean that it is a call that came in on the support line. If it matches send it to the context ext-queues, extension 700, step number 1.

exten => s,n,GotoIf($["${CALLERID(name):0:10}" = “Sales:Cust”]?ext-queues,700,1)

Same as above mostly. The n says this is the next step, could have put in 2 as well. The gotoif now says look at 10 characters, if they match Sales:Cust (ie a customer who has called the sales line), send them to the queue.

exten => s,n,Goto(ext-group,600,1)

This last line says send everyone who got this far without being matched should be send to the ring group, extension 600, step 1.

Hope that helps someone out there.


Great job…Thanks for sharing it with the community.

very good explanation.
I did exactly what you explain, but I’m receiving this error:
“pbx.c: Channel ‘SIP/PUser1-00000003’ sent to invalid extension but no invalid handler: context,exten,priority= custom-support,s,1”

The dialplan show command, returns the right plan:

[ Context 'custom-support' created by 'pbx_config' ]
  's' =>            1. GotoIf($["${CALLERID(num):0:7}" = "xxxxxx"]?timeconditions,2,1) [pbx_config]
                    2. GotoIf($["${CALLERID(num):0:7}" = "xxxxx"]?timeconditions,2,1) [pbx_config]
                    3. Goto(timeconditions,1,1)                   [pbx_config]

-= 1 extension (3 priorities) in 1 context. =-

Any suggestion?

Instead of routing to a SIP/ extension, try routing to a LOCAL/ extension. This way, you should be able to hit any kind of phone (DAHDI, SCCP, SIP, PJSIP, etc.) without having to jump through a bunch of hoops trying to guess Channel Driver Types.

If I understand you mean that I should not use “custom-support,s,1”, but something else? But what?
“custom-support,s,1” is custom destination.

No - your custom context is perfect. Something in your system is trying to route calls to a non-existent SIP extension.

The assumption in that is that the extension may exist in some other form (a queue, a ring group, a PJ-SIP extension, etc.) so using the Channel Selector LOCAL/xxx (where xxx is the extension) makes the system more flexible than using SIP/xxx, which means that the only place the extension can exist is on a SIP phone.

On reviewing your post, I think you might have actually answered your own question. Your context is [custom-split-sales-support], but you are sending the call to

So, somewhere , you are sending the call to custom-support as a context, but the context is “custom-split-sales-support”.

1 Like

may be I do not understand completely, but my post does not use [custom-split-sales-support] but only [custom-support] , as you can see reading the dialplan show results, and the error is “pbx.c: Channel ‘SIP/PUser1-00000003’ sent to invalid extension but no invalid handler: context,exten,priority= custom-support,s,1”.
By the way the routing is: External call --> one inbound route any/any --> destination custom (custom-support).
When a call arrive, the flow is stops here "pbx.c: Channel ‘SIP/PUser1-00000003’ sent to invalid extension but no invalid handler: context,exten,priority= custom-support,s,1", but the Goto instruction seems to be correct : Goto(custom-support,s,1).

OK, then. You know best. Let us know when you figure it out.