📜 ⬆️ ⬇️

Asterisk, automatic detection of the cellular operator by phone number, even transferred numbers

Asterisk has its own mechanism for working with databases, I will tell you about an alternative method of working with databases from the Asterisk dialplan! It can be used not only to work with databases, but also with any other software on the server. Article about the function "SHELL".
The documentation about SHELL says:
SHELL (command)
Runs command and returns its output

Unlike SYSTEM, SHELL returns the result of the command execution. And unlike func_odbc.conf, the entire logic of processing the results of a database query can be derived from the Asterisk dialplan. With SHELL and bash, you can move mountains, with this dialplan you can leave almost unchanged. By the way, I write it on AEL ( excellent article about AEL ).

Call Handling Context
context outgoing_calls_mobile { _89XXXXXXXXX => { Noop( file_conf outgoing_calls.ael context outcoming_calls_mobile); Noop(  ${CALLERID(num)}   ${EXTEN}); Set(mobile_operator_info=${SHELL(/etc/asterisk/scripts/operatorIF.sh ${EXTEN:1} ${CALLERID(num)})}); //        ,    "error" if ("${mobile_operator_trunk_cid}" = "ERROR") { Gosub(error,s,1(${CALLERID(num)},${EXTEN})); }; //   CUT,       Set(mobile_operator_name=${CUT(mobile_operator_info,\,,1)}); Set(mobile_operator_id_region=${CUT(mobile_operator_info,\,,2)}); Set(mobile_operator_region=${CUT(mobile_operator_info,\,,3)}); Set(mobile_operator_region_id=${CUT(mobile_operator_info,\,,4)}); //     : // mobile_operator_name -   // mobile_operator_id_region - id  // mobile_operator_region -   // mobile_operator_region_id - id -   //         ,      // //      ,   ,   . Dial(SIP/${mobile_operator_name}/${EXTEN:1},60,); //    Gosub(test_dial,s,1(${DIALSTATUS},${exten})); Hangup(); }; }; 


I transfer the caller's internal number to the script, just like that, this number is added to the database.
And the script itself on BASH, it makes a request to the database to search for old records, adds new ones to the database and asks the megaphone which operator has the number now.

Learn more about the Megaphone API
The script uses a request to the public API Megaphone
On request:
 http://www.megafon.ru/api/mfn/info?msisdn=79XXXXXXXXX 

The API returns a line like:
 {"operator":"","operator_id":99,"region":" .","region_id":56} 


At the end, a line is commented out with an alternative request service, who wants, can finish it as a backup channel.
')
here it is bash script
 #!/bin/bash sql='mysql -uprovisioning -pxkYyNFuyc3nEKsFj -Dasterisk -e' #   ,          lifetime=30 #   ,         #     ,    30 ,       . array_operator_old=($($sql "SELECT operator,operator_id,region,region_id FROM operators WHERE to_phon_nomber like '$1' \ and data >DATE_ADD(NOW(), INTERVAL -$lifetime DAY) limit 1"| awk 'NR>1')) #       ,   ,       . if [ -n "${array_operator_old[1]}" ] then operator_old=${array_operator_old[0]} operator_id_old=${array_operator_old[1]} region_old=${array_operator_old[2]} region_id_old=${array_operator_old[3]} echo -n $operator_old,$operator_id_old,$region_old,$region_id_old exit 0 fi #      ,     : array_operator=($(curl -s http://www.megafon.ru/api/mfn/info?msisdn=$1|tr -d '"{}'|tr -s ' ' '_'|tr -s ',' '\t')) #   ,   ,   ERROR     test_error=$(echo -n ${array_operator[@]}|grep error) if [ -n "$(echo -n ${array_operator[@]}|grep error)" ]; then echo -n ERROR; exit 0; fi #   ,     /    #       . operator=$(echo -n ${array_operator[0]} | tr -d 'operator:'|tr -d '\,') operator_id=$(echo -n ${array_operator[1]} | tr -d 'operator_id:') region=$(echo -n ${array_operator[2]} | tr -d 'region:') region_id=$(echo -n ${array_operator[3]} | tr -d 'region_id:') $sql "REPLACE INTO operators\ (id, data, to_phon_nomber, operator, operator_id, region, region_id, in_phon_nomber)\ VALUES\ (NULL,\ NULL,\ '$1',\ '$operator',\ '$operator_id',\ '$region_old',\ '$region_id',\ '$2')" #operator=$(curl -s https://phonenum.info/phone/$1|grep \:|head -n1| sed 's/\:\ //'|tr -s "[:space:]" "_"|sed -r 's/\..*//') echo -n $operator,$operator_id,$region,$region_id exit 0 


Dump database «operators».

Thanks to all)

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


All Articles