Dynamic Routes - Extension Check

@lgaetz maybe you have an idea.

I want to check if an DNID is an extension on my FreePBX box and if not, send the call to one of my trunks. Ideally I want to do this with one patterned inbound route (done) and one dynamic route.

I have the dynamic route running a MySQL query:
SELECT EXISTS(SELECT extension FROM asterisk.users WHERE extension LIKE '%${CALLERID(dnid)}%') as truth

If the extension exists, it returns a 1, if not it returns a 0. For 0 I can send it to the trunk via the Dynamic route, no problem.

For the true, what is the most efficient way send the call to the extension (the DNID variable)? Can I send it to the extensions context (not sure what that is) directly? I need to avoid the inbound routes so I don’t cause a loop.

Cleaner way is to use the Asterisk variable in dynroutes with this expression:

${DIALPLAN_EXISTS(ext-local,${dnid-var},1)}

The ext-local context exists for local extensions and their associated mailboxes. If the incoming DNID is only numeric, then it will only match on local extension numbers. No SQL necessary.

If you use virtual or custom extensions then you’ll want to do dialplan show ext-local to see if there are extensions in that context that you don’t want to match.

1 Like

What’s the difference between dnid-var & callerid(dnid)?

There’s no such thing as dnid-var (unless you define it), that’s a placeholder I used instead of looking up the correct syntax. :slight_smile:

1 Like

Just seeing the second part of the question now. I think the easiest way to do this is not use dynroutes at all but instead set your trunk context to from-internal as if it’s an intracompany trunk, that’s the conventional way to do this. The risk here is that would allow inbound callers access to local feature codes which may not be desirable depending on how the DID is defined, so a less privileged way would be to create your own context like:

[from-trunk-comtech]
include => ext-local-custom
include => outbound-allroutes

Then set the trunk context to this.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.