Dynamically route outbound calls to the same trunk That we received the call from

I have setup freepbx working with 7 goip gateways, each goip has 8 Sim cards. I have now 50 trunks working in production environment.
Each user has 2 Sim cards, if one user is offline, all the inbound calls are routed to the available users. These available users sometimes have to call back the customers of their co-workers and they call with their own trunk by default. So the customer doesn’t know the phone number and he doesn’t answer the call because it’s unknown for him.
I found a solution using a prefix to switch between trunk but just imagine 50 prefixes! It’s too much.
Now I’m wondering if there’s a way for a user to call back a customer with the same trunk that is inbound from automatically, maybe freepbx can save the trunk that the call is coming from or something like this!

I appreciate your help.


If you could find the context that engages the trunk, and set the necessary variables (using dynamic routes) before calling it (custom destination), then you could likely do it. I do something similar with extensions being called and dynamic routes. unfortunately, I do not know the context or variables you need off hand, but others on the forum likely will, or you can read the config files yourself to figure it out.

Hi @comtech thank you for the answer. Unfortunatly your explanation is a little difficult to understand. Can you give me an example please?

Now I read about dynamic routes and it seems very interesting and it will do the job.
There is something that I don’t find solution for, and here is a scenario :
I want to create a dynamic route that is able to return the extension of the user by simply searching the phone number into the phone book database and when it find it, just outbound to the adequate destination.
The fact of creating the dynamic route is very easy, I just have to make an SQL query. But the problem is that I’m unable to find where to assign the dynamic route on the outbound route to trigger it!
Do you have any idea?

Thank you for your help.

You cannot call outbound routes in dynamic routes, at least for now, only trunks. Your option is to go into the .conf file to see how outbound routes are called and what variables are necessary before calling the outbound routes context. Then use dynamic routes to set any variables and build a custom destination to the necessary context. Lastly you would point your last dynamic route at the custom destination to have it go do the destination you are looking for.

Your explanation is very helpful, thank you so much, I will give it a try and return back to you.

Hi @comtech,
I created the sql query that returns the extension based on the phone number and it’s working.

Now I don’t know where to put the query ! is it on a dynamic route ? or execute it directly from the dialplan of the context ? and even the context, I didn’t find any information about it, I just know that I have to create a custom-context in custom_extensions.conf file or maybe not !

I’m also unable to know how to trigger a dynamic route from dialplan, I did that just for test purposes :
exten => _X.,1,Set(HEADER_TO=${SIP_HEADER(To)})
exten => _X.,n,Set(CALLED_NUMBER=${HEADER_TO:5:10})
exten => _X.,n,MYSQL(Connect connid localhost username password asterisk)
exten => _X.,n,MYSQL(Query resultid ${connid} SELECT default_extension FROM userman_users WHERE userman_users.description = (SELECT contactmanager_groups.name FROM contactmanager_entry_numbers inner join contactmanager_group_entries on contactmanager_group_entries.id = contactmanager_entry_numbers.entryid inner join contactmanager_groups ON contactmanager_groups.id = contactmanager_group_entries.groupid WHERE contactmanager_entry_numbers.number = ‘099999999’ LIMIT 1 ))
exten => _X.,n,MYSQL(Fetch fetchid ${resultid} source_ext)
exten => _X.,n,MYSQL(Disconnect ${connid})
exten => _X.,n,NoOp(&& the value found is ${source_ext} )

when I make a call between internal extensions, the query is working but I have an error : service unavailable.

I’m really stucking :frowning: Please help

I found the trunks that are assigned to the outbound routes, all are on the file extensions_additional.conf and they look something like this :
include => from-trunk-pjsip-Goip_1_Channel_2_outbound-custom
exten => _.,1,Set(GROUP()=OUT_11)
exten => _.,n,Goto(from-trunk,${EXTEN},1)

here : OUT_11 is referring to the outbound trunk :

Also, I found this :

and this :

any idea ?

It looks like you are getting closer.

does outrt-## correlate with the outbound route you would like to use (follow the context steps)? If yes, then you need to build a custom destination to routes to the proper context accordingly. Then you can call the custom destination (s) in your dynamic route.

Hi !
Thank you for the reply.
Yes I think that they are !
How could I call the custom destination from the dynamic route ? I mean what can trigger the dynamic route to execute each time a call is started ? should I call it from the dialplan ?
If I use an asterisk variable in the dialplan, does the dynamic route get the variable from the dialplan ?

Supposing that I create an outrt-4-custom context, I will need to replace the extension by the result of the sql query. In the example, the extension is strict (64 in this case).

Too much questions I know ! I’m sorry about that. I’m confused a little bit.

Here I created a new outbound route without mentioning the user caller id and it gave me this :

You can see that there’s no caller id extension instead of the outrt-4 context.

Ok I Found Something very interesting :
First : I uncommented all of those contexts :

Letting only one context : [outbound-allroutes-custom]

after that I went to my frepbx config edit file :

the number 80 is referencing the trunk id in the database, when I change it to another id value, it will take the trunk to go throught. So it’s working !

Now I have 2 solutions in head :
Solution 1 : Create a table in the asterisk database that will contain these columns :

I will execute a sql query to get the trunkId where the extension = resultOfTheFirstQuery
after that I will put it in the place of 80 as a variable and let the dialplan route the calls.

Solution 2 :
Try to deal with it without creating a new table and using dynamic route but I really don’t know how to do it !

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