📜 ⬆️ ⬇️

Asterisk: Routing by Caller Number and Region Code

The information further may be of interest to those who use the asterisk, receive the number 8-800 and have subscribers in offices located in several cities of the Russian Federation.

My solution to the classic Asterisk-telephonist problem: a business wants an office in Vladivostok to answer calls from the Far East, an office in Novosibirsk to the Urals and Siberia, and an office in Moscow to all the others.

You can use the option presented earlier on Habré in the article , but I decided to make the solution a bit more universal. Further implementation details and video instruction.

')
The solution is this: make work with the definition of a region by phone number beyond the asterisk dialplan. Those. we implement the logic for determining the region code by the caller number on the AGI server side. The asterisk has no idea how the AGI server works, where it takes the data, in what language it is written, but receives the region code as a result of the request, and already continues to process the call on the dialplan.

Scheme of work



How is the AGI server implemented?

AGI-server - in our case, this is a nodejs-application, which, upon receiving a request with a number, contacts MongoDB, finds a match and sets the required value of the dial-dial variable REGION_CODE.

What is the dialplan?

The dial plan for using an AGI server is approximately as follows:

[incoming] exten => 88001234567,1,AGI(agi://localhost:3000,${CALLERID(num)}) exten => 88001234567,n,GotoIf($[${REGION_CODE}=24]?outbound,krasnoyarsk,1:) exten => 88001234567,n,GotoIf($[${REGION_CODE}=50]?outbound,moscow,1:outbound,other,1) 


For each region, you must specify your check?

If you have offices in every region of the Russian Federation (and we have 85 of them), then you should probably set up checking each code. But usually a less detailed division of the Russian Federation is required, therefore the COUNTY_CODE variable is also set - the number of the federal district (we have only 9 of them).

Those. dialplan might look like this:

 [incoming] exten => 88001234567,1,AGI(agi://localhost:3000,${CALLERID(num)}) exten => 88001234567,n,GotoIf($[${COUNTY_CODE}=5]?outbound,krasnoyarsk,1:) exten => 88001234567,n,GotoIf($[${COUNTY_CODE}=1]?outbound,moscow,1:outbound,other,1) 


Or, of course, in a combined mode. The logic of building a dialplan based on the set variables COUNTY_CODE and REGION_CODE can be determined by everyone.

How can this be implemented in the user interface?

For example, in my user interface, setting up routing by caller number looks like this



Data with codes and names can be taken in npm russian-codes and used in drop-down menus.

Where does the data in MongoDB for the AGI server come from?

The data is usually located on the website of the Federal Communications Agency in the " Open Data " section. As we download and convert to JSON, I already wrote on Habré . But in the current data format, the numcap region is indicated by a string with the name of the region, and not its code, which is not very convenient.

Therefore, using npm numcap-regions (which in turn uses npm nycap and russian-codes ) we can get a large JSON-file with the capacity of Russian operators, indicating regional codes.

Do I need to update the database with the data on the codes of the regions?

You can download it once and use it. But the utilities in the specified npm can be used to regularly update the data.

The Federal Communications Agency regularly updates the numbering register, so it’s easy to always have a fairly up-to-date database. Although, of course, telephone numbers are unlikely to drastically change the region (and operators need additional data processing). I update the data in numcap about once a month.

So, what can I do to make my routing by number and region code work?

1. Install agi-number-archer-app (The installation is described in detail in the agi-number-archer-app repository, you can clone it, write your configs, deployment scripts).

2. Load data into MongoDB (use numcap-regions to load data, use is also described in the repository).

I hope that this will come in handy to someone and will enable them to realize a more convenient and high-quality service for their clients.

Questions, suggestions, critical comments are welcome.

PS Video with step-by-step service settings.

Source: https://habr.com/ru/post/252733/


All Articles