📜 ⬆️ ⬇️

Multiphone in FreePBX: full connection automation

Greetings

I will share one more small, but very useful crutch to freepbx. As you know, many people love and use multifon as a sip trunk to all kinds of asterisks, getting “almost-free” multichannel incoming communication channel and quite interesting prices for outgoing ones.

The stability of the service, of course, at the level of its price. But many (including me) are satisfied. In this case, you can (and need!) Sim at the same time, for example, for outgoing calls to your home region. The balance "moves", the number will not be selected for non-use.
')
But sometimes the balance goes "in the negative." Then the multi-phone is turned off, and the routing setting is lost. When replenishing the balance of calls go to the sim, but not in asterisk.

For those who have many such trunks, especially on different PBXs (like mine, I serve asterisk and different clients) - it’s quite problematic to constantly monitor the “multi-phones” routes manually.

I offer an automated solution for those with freepbx.
The script runs on the crown (once an hour from me) and does the following:

1) Pulls the registration data of all multifon trunks from the asterisk database (on the LIKE registration line "%@multifon.ru%");
2) Checks on the sm.megafon.ru service route, if it is incorrect, it rules on the desired one.
3) Makes amportal restart for trunk "update". Accelerates the registration procedure.

The script runs on the crown, but it is also possible to "pull" his link at any time. You can pass to it the parameter “r”, which by its values ​​coincides with the parameter “routing” of the sm.megafon.ru service.

Here is the script text (index.php file)
<?php #/  #    FreePBX #    "?r=0",  PBX: "?r=1",  : "?r=2" #vmcl****.ru #mysql settings $hostname = "localhost"; $username = "mysql_user"; $password = "mysql_password"; $dbName = "asterisk"; mysql_connect($hostname,$username,$password) or die("NO connect to MySQL: ".mysql_error()); mysql_select_db($dbName) or die("MySQL ERROR:".mysql_error()); mysql_query("set names 'utf8'"); date_default_timezone_set("Europe/Moscow"); if(isset($_GET['r'])) $r = $_GET['r']; else $r = '1'; //    ?r=X,    PBX //browser settings $header = array(); $header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; $header[] = 'Cache-Control: max-age=0'; $header[] = 'Connection: keep-alive'; $header[] = 'Keep-Alive: 300'; $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'; $header[] = 'Accept-Language: en-us,en;q=0.5'; $header[] = 'Pragma: '; //seach multifon trunks $mf_query = mysql_query('SELECT `data` FROM `sip` WHERE `keyword` = "register" AND `data` LIKE "%@multifon.ru%" ORDER BY `data`') or die("MySQL ERROR:".mysql_error()); while($mf_row = mysql_fetch_array($mf_query, MYSQL_ASSOC)) { $pre_data = explode("@", $mf_row['data']); $data = explode(":", $pre_data[0]); print " {$data[0]}..."; // $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://sm.megafon.ru/sm/client/routing?login={$data[0]}@multifon.ru&password={$data[1]}"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)"); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_TIMEOUT, 20); $response = curl_exec($ch); curl_close ($ch); $routing = json_decode(json_encode(simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA)), true); // if($routing['result']['code'] == 200) { // ok if($routing['routing'] == $r) { //  print "   {$routing['routing']}<br>"; } else { print " {$routing['routing']},   $r..."; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://sm.megafon.ru/sm/client/routing/set?login={$data[0]}@multifon.ru&password={$data[1]}&routing=$r"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)"); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_TIMEOUT, 20); $response = curl_exec($ch); curl_close ($ch); $result = json_decode(json_encode(simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA)), true); if($result['result']['code'] == 200) { print "{$result['result']['description']}<br>"; } else { print " sm.megafon.ru   {$result['result']['code']} {$result['result']['description']}<br>"; } exec("amportal reload"); } } else { print " sm.megafon.ru   {$routing['result']['code']} {$routing['result']['description']}<br>"; } } print ""; ?> 


Here is the sh-file that lies in cron:
 #!/bin/sh #move this file to cron folders cd /var/www/html/mf && /usr/bin/nohup /usr/bin/php -f index.php >/tmp/mf_php.log & 


The script, as you can see, is located in the / var / www / html / mf directory, in the scope of the web server.
You need to specify only the data to connect to mysql in the settings section of the script.

Good luck!

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


All Articles