
For many companies operating throughout the country, it is very important to know which region exactly received each incoming call, for example, to evaluate the effectiveness of a marketing campaign in regions of interest. Of course, you can make call-center operators update this information with the caller every time, but it is still more convenient to receive such information automatically.
In addition, when making outgoing calls, it is important to call from the number of the region where the client is located. You can, of course, make outgoing routes, but with more than three regions, and considering mobile numbers, the design is monstrous.
Of course, for the implementation of such a task, you can use the database, and articles with the implementation using MySQL can be found, including on the habr, but sometimes the use of the database is inappropriate or impossible. Therefore, below you will find an example of a simple implementation of the required functionality without using databases, namely, a simple script and a small piece of dialplan for Asterisk, with the help of which we will get the information of interest.
At
www.rossvyaz.ru/activity/num_resurs/registerNum is the official, updated “Extract from the register of the Russian system and the numbering plan” in four volumes, using the first digits of the code: 3, 4, 8 and 9. We need to download all the files in csv format, merge them, delete unnecessary and empty fields, change the encoding to UTF-8, and replace all spaces with "_", so that later it would be easier to work in the bash script.
The file is of the following form:
')
prefix! starting number! final number! who belongs to! where is registered
999!9440000!9449999!"_""_"""!_
999!9450000!9459999!"_""_"""!_
999!9460000!9469999!"_""_"""!_
999!9470000!9479999!"_""_"""!_
999!9480000!9489999!"_""_"""!_
999!9490000!9499999!"_"""""!__//
Now we will write a bash script. First, we will select the lines according to the three-digit code, because every time we run the entire file through the loop for a very long time. On my test, one run lasted 4 seconds, and the current version runs much faster, although it is also not instant
Note the -n option in the echo call. It is necessary, because without it, the result of executing the script, in addition to the name of the region, will contain a line feed, which is completely useless to us.
Now you need to give permissions to the script to execute.
chmod +x region.sh
and do not forget to give access to the user from whom Asterisk is running to the script and the data file.
chown asterisk:asterisk -R /path-to-script
To check, we will launch our script with some number from the console and make sure that it works.
It remains to work with Asterisk. The System application, which is usually used to execute scripts, does not suit us - we need the result of the script, not the execution status!
Therefore, we use the SHELL function:
exten => 9999,n,Noop(${CALLERID(num)}) exten => 9999,n,Set(num=${CALLERID(num):1}) ; 11-, exten => 9999,n,Set(__reg=${SHELL(/home/asterisk/region.sh ${num} )}) exten => 9999,n,Noop(${reg})
Note that we assign a variable with two characters "__" at the beginning, so that it is inherited when the Goto call is transferred to other contexts and macros.
In the console at the time of the call, we will respectively see
Executing [9999@region] NoOp("SIP/123-00000026", "79063454647") in new stack -- Executing [9999@region] Set("SIP/123-00000026", "num=9063454647") in new stack -- Executing [9999@region] Set("SIP/123-00000026", "reg=_.") in new stack -- Executing [9999@region] NoOp("SIP/123-00000026", "_.") in new stack
Similarly, you can get the name of the carrier for each number, only the field in the script will need to be received not at number 5, but at number 4
reg=$( echo $str | cut -d '!' -f4 )
Of course, we would not recommend using such a solution on high-load systems due to the rather lengthy execution of the script itself, however, in some cases it can be very useful.
The author of the article: Centos-admin.ru system administrator is Alexey Dmitriev.