
Asterisk: routing by caller ID and region code
The information further may be of interest to those who use the asterisk, get the number 8-800 on it and have subscribers in offices located in several cities of the Russian Federation.
My solution to the classical problem of an asterisker telephone operator: business wants an office in Vladivostok to answer calls from the Far East, calls to the Urals and Siberia to an office in Novosibirsk, and all the rest to an office in Moscow.
It is possible to use the option presented earlier on Habré in the article , but I decided to make the solution a little more universal. Further details of the implementation and video instruction.
The solution is this: move the work with determining the region by phone number beyond the dialplan of the asterisk. Those. we implement the logic of determining the region code by the number of the caller on the side of the AGI server. The asterisk has no idea how the AGI server works, where it takes data, what language it is written in, but receives the region code as a result of the request, and then continues to process the dialplan call.
Workflow

How is the AGI server implemented?
AGI server - in our case, this is a nodejs application that, upon receiving a request with a number, contacts MongoDB, finds a match, and sets the required value of the REGION_CODE dialplan variable.
What is the dialplan?
The dialplan for using an AGI server is something like this:
For each region do you need to indicate your verification?
If you have offices in each region of the Russian Federation (and we have 85 of them), then it is probably worth setting up verification of 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 (and we have only 9 of them).
Those. Dialplan might look like this:
Or, of course, in combined mode. Everyone can determine the logic of building a dialplan based on the set variables COUNTY_CODE and REGION_CODE.
How can this be implemented in the user interface?
For example, in my user interface, the routing setting for the caller’s number looks like this.

Data with codes and names can be taken in npm russian-codes and used in drop-down menus.
And where does the data in MongoDB come from for an AGI server?
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 numcap data format, the 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's numcap and russian-codes ), we can get a large JSON file with the capacity of the RF operators with the indication of the region codes.
Is it necessary to update the database with data on region codes?
You can download it once and use it like that. But the utilities in the specified npm can be used to regularly update the data.
The Federal Communications Agency regularly makes changes to the numbering registry, so it’s easy to always have a fairly current database. Although, of course, phone numbers are unlikely to dramatically change the region (and operators need to carry out additional data processing). I update data in numcap about once a month.
So, what should I do to get my routing by number and region code?
1. Install agi-number-archer-app (Installation is described in detail in the repository agi-number-archer-app , you can clone it, write your configs, deployment scripts).
2. Upload data to MongoDB (use numcap-regions to upload data , use is also described in the repository).
I hope that this will come in handy for someone and will allow us to implement a more convenient and high-quality service for our customers.
Questions, suggestions, criticisms are welcome.
PS Video with step-by-step service setup.
My solution to the classical problem of an asterisker telephone operator: business wants an office in Vladivostok to answer calls from the Far East, calls to the Urals and Siberia to an office in Novosibirsk, and all the rest to an office in Moscow.
It is possible to use the option presented earlier on Habré in the article , but I decided to make the solution a little more universal. Further details of the implementation and video instruction.
The solution is this: move the work with determining the region by phone number beyond the dialplan of the asterisk. Those. we implement the logic of determining the region code by the number of the caller on the side of the AGI server. The asterisk has no idea how the AGI server works, where it takes data, what language it is written in, but receives the region code as a result of the request, and then continues to process the dialplan call.
Workflow

How is the AGI server implemented?
AGI server - in our case, this is a nodejs application that, upon receiving a request with a number, contacts MongoDB, finds a match, and sets the required value of the REGION_CODE dialplan variable.
What is the dialplan?
The dialplan for using an AGI server is something like this:
[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 do you need to indicate your verification?
If you have offices in each region of the Russian Federation (and we have 85 of them), then it is probably worth setting up verification of 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 (and 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 combined mode. Everyone can determine the logic of building a dialplan based on the set variables COUNTY_CODE and REGION_CODE.
How can this be implemented in the user interface?
For example, in my user interface, the routing setting for the caller’s number looks like this.

Data with codes and names can be taken in npm russian-codes and used in drop-down menus.
And where does the data in MongoDB come from for an AGI server?
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 numcap data format, the 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's numcap and russian-codes ), we can get a large JSON file with the capacity of the RF operators with the indication of the region codes.
Is it necessary to update the database with data on region codes?
You can download it once and use it like that. But the utilities in the specified npm can be used to regularly update the data.
The Federal Communications Agency regularly makes changes to the numbering registry, so it’s easy to always have a fairly current database. Although, of course, phone numbers are unlikely to dramatically change the region (and operators need to carry out additional data processing). I update data in numcap about once a month.
So, what should I do to get my routing by number and region code?
1. Install agi-number-archer-app (Installation is described in detail in the repository agi-number-archer-app , you can clone it, write your configs, deployment scripts).
2. Upload data to MongoDB (use numcap-regions to upload data , use is also described in the repository).
I hope that this will come in handy for someone and will allow us to implement a more convenient and high-quality service for our customers.
Questions, suggestions, criticisms are welcome.
PS Video with step-by-step service setup.