📜 ⬆️ ⬇️

Attempt to do without regular expressions for phone numbers of own region

Hi% username%!


In the process of administering Asterisk PBX, sooner or later there is a need to route calls to directions and most often it is: city, MN, intra-zone communication, MG. With the first two everything is clear. But the last two ... Well, and what is difficult you say? A car and a small cart of articles and scripts about the compilation of regular expressions for DEF and ABC codes have already been written, but periodically all these regulars have to be updated. And then there was a thought, is it possible to automate it. Unfortunately, something ready, and most importantly suitable, could not be found. So we will reinvent your bike.

To do this, we need to do the following:

  1. Configure an asterisk connection to the database
  2. Create and fill in a table with DEF and ABC region codes
  3. Describe the functions additional features for asterisk
  4. Write contexts for the directions we need.


Step 1. Configure the asterisk connection to the database

I assume that you already have a database installed and configured. Due to the fact that I have implemented a realtime mechanism that uses the postgresql database, all the described further actions will be valid for postgresql, for other databases, minor changes may be required.
On the server with asterisk, install unixodbc, as well as the postgresql driver for odbc with all dependencies. For Debian / Ubuntu, this is done with the command:
')
#apt-get install unixodbc odbc-postgresql

We give the odbc driver description file to the following form:

 #cat /etc/odbcini.ini [PostgreSQL ANSI] Description = PostgreSQL ODBC driver (ANSI version) Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so Debug = 0 CommLog = 1 UsageCount = 1 [PostgreSQL Unicode] Description = PostgreSQL ODBC driver (Unicode version) Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.so Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so Debug = 0 CommLog = 1 UsageCount = 1 


For 32-bit systems, the path to the drivers will be different.

Create a connection with a previously created database:

 # cat /etc/odbc.ini [config] Description = PostgreSQL connection to 'asterisk' database Driver = PostgreSQL ANSI Database = asterisk Servername = 192.168.204.167 UserName = asterisk Password = Uidj$5tuYF Port = 5432 Protocol = 9.1 KSQO = No ReadOnly = No RowVersioning = No ShowSystemTables = No ShowOidColumn = No FakeOidIndex = No ConnSettings = 


Check the connection to the database:

 isql config 

If we see the following invitation:
 +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> 


it means that our connection is working fine.

Register this connection in /etc/asterisk/res_odbc.conf:

 [asterisk] enabled => yes dsn => config username => asterisk password => Uidj$5tuYF pre-connect => yes 


Step 2. Create and fill in a table with DEF and ABC region codes

On the database server we create an asterisk database inside, with which we create approximately the following table (the minimum required number of columns is indicated):

 CREATE TABLE zones ( def smallint NOT NULL, start integer NOT NULL, finish integer NOT NULL) WITHOUT OIDS; 


Fill in the created table by running the following script:

 #!/bin/bash REGION='' echo "COPY zones FROM stdin;" > zones.sql wget -q http://www.rossvyaz.ru/docs/articles/DEF-9x.html -O DEF-9x.html cat DEF-9x.html|iconv -f cp1251 -t utf8|grep -i $REGION|awk '{print($3"\t"$6"\t"$9)}' >> zones.sql rm DEF-9x.html wget -q http://rossvyaz.ru/docs/articles/ABC-3x.html -O ABC-3x.html cat ABC-3x.html |iconv -f cp1251 -t utf8|grep -i $REGION|awk '{print($3"\t"$6"\t"$9)}' >> zones.sql rm ABC-3x.html wget -q http://rossvyaz.ru/docs/articles/ABC-4x.html -O ABC-4x.html cat ABC-4x.html |iconv -f cp1251 -t utf8|grep -i $REGION|awk '{print($3"\t"$6"\t"$9)}' >> zones.sql rm ABC-4x.html wget -q http://rossvyaz.ru/docs/articles/ABC-8x.html -O ABC-8x.html cat ABC-8x.html |iconv -f cp1251 -t utf8|grep -i $REGION|awk '{print($3"\t"$6"\t"$9)}' >> zones.sql rm ABC-8x.html echo "\." >> zones.sql psql -U asterisk -d asterisk -c 'DELETE FROM zones' psql -U asterisk -d asterisk -f zones.sql rm zones.sql 


Step 3. Describe the functions additional functions for asterisk

We will need an additional function, which we will describe in /etc/asterisk/func_odbc.conf

 [ZONES] prefix=CHECK dsn=asterisk readhandle=asterisk readsql=SELECT count(*) from zones where def='${ARG1:1:3}' and start<='${ARG1:4}' and finish>='${ARG1:4}' 


Step 4. Write contexts for the directions we need.

Final step: describe contexts.

The first context is for subscribers who are allowed only intrazone calls:

 [zones] include => template 


The second for those who are allowed long distance calls:

 [meggorod] include => template 


Yes, they are the same:

 [template] exten => _8XXXXXXXXXX,1,GotoIf($["${CHECK_ZONES(${EXTEN})}" = "1"]?4) same => 2,GotoIf($["${CONTEXT}" = "meggorod"]?6:error,1) same => 3,Hangup() same => 4,Dial(SIP/prov1/${EXTEN},30,tTS(3600)gxX) same => 5,Hangup() same => 6,Dial(SIP/prov2/${EXTEN},30,tTS(3600)gxX) exten => error,1,Playback(an-error-has-occured) exten => error,n,Hangup() 


Well, that seems to be all. The option is far from ideal, but it works.

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


All Articles