📜 ⬆️ ⬇️

Asterisk and Truecaller. Determining the name of an unknown caller for incoming calls

TrueCaller is a service for determining the name of a subscriber for incoming calls, as well as blocking spam. On smartphones with CyanogenOS 12.1, it is sewn into a standard dialer. You can also install a TrueDialler / TrueCaller with GooglePlay / AppStore / BlackBerryWorld / WindowsPhoneStore.

If you activated this functionality in your smartphone, then your contact book is completely merged into the Truecaller server? You can check whether your number is in the database using the link, for example: https://www.truecaller.com/ru/74996813210 (authentication is required).

At the moment, the service has 1.6 billion rooms worldwide. It is possible to write out your number from the database at https://www.truecaller.com/unlist .

Screw Truecaller to Asterisk


On the site Truecaller'a there is an opportunity to determine the name of the subscriber by phone number. Login to the site is possible only through third-party services and social networks. For authentication, I chose Vkontakte (Oauth protocol).
')
1. Manually access truecaller.com, using a previously registered Vkontakte account, to allow access.
2. It is necessary to create an internal database for storing contacts that have already been called. This is necessary so that every time you do not contact the service truecaller'a.
3. Write an authentication script on truecaller.com through the VKontakte network, as well as a function to check the numbers for the subscriber’s name.

The script is written in PHP for ease of implementation for AGI and general readability.

Create a database in MySQL:

USE asterisk; CREATE TABLE asterisk.phonebook ( id int(11) NOT NULL AUTO_INCREMENT, create_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, number varchar(20) NOT NULL, name varchar(80) NOT NULL, temporary_contact tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (id) ) ENGINE = INNODB AUTO_INCREMENT = 9 AVG_ROW_LENGTH = 8192 CHARACTER SET utf8 COLLATE utf8_general_ci; 

PHP script /var/lib/asterisk/agi-bin/phonebook.php (for those who do through PHP-AGI, do not forget to uncomment the corresponding lines, you will get the result in the variable of the CID_NAME channel):

 #!/usr/bin/php -q <?php $error_level = error_reporting(0); set_time_limit(30); //require('phpagi.php'); //$agi = new AGI(); if (isset($argv[1])) {$num=$argv[1];} else {$num=NULL;} $cookie_file='/tmp/asterisk_truecaller_vk.cookie'; $vk = array("login"=> "_", "password"=> "_"); $mysql = array("hostname" => "localhost", "login"=> "root", "password"=> "_mysql", "database"=> "asterisk"); if (!is_null($num)) { $callerid_name=get_num($num,$vk,$mysql,$cookie_file,true); return $callerid_name; } else { echo "   \n"; //$agi->set_variable("CID_NAME", ""); return false; } //     truecaller function get_num($num,$vk,$mysql,$cookie_file,$isauth) { //     mysql_connect($mysql['hostname'],$mysql['login'],$mysql['password']); mysql_select_db($mysql['database']) or die(mysql_error()); mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET 'utf8'"); mysql_query("SET SESSION collation_connection = 'utf8_general_ci'"); $query = "SELECT * FROM phonebook WHERE `number`=$num"; $res = mysql_query($query); $count = mysql_num_rows($res); if ($count>0) while ($row=mysql_fetch_array($res)) { $name=$row['name']; echo "   MySQL '".$name."'\n"; //$agi->set_variable("CID_NAME", "$name"); return $name; } mysql_close(); //   truecaller if ($isauth) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.truecaller.com/ru/'.$num ); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch); if (preg_match("/You need to sign in to view the result/i", $data)) { echo "  TC\n"; $isauth = oauth_vk($vk, $cookie_file); get_num($num,$vk,$mysql,$cookie_file,$isauth); } else { preg_match("/<div class=\"detailView__nameText\">\n\s*(.+)\s\n\s*<\/div>/i", $data, $matches); if (count($matches)>0) { $name=$matches[1]; echo "   TC '".$name."'\n"; mysql_connect($mysql['hostname'],$mysql['login'],$mysql['password']); mysql_select_db($mysql['database']) or die(mysql_error()); mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET 'utf8'"); mysql_query("SET SESSION collation_connection = 'utf8_general_ci'"); $query = "INSERT INTO phonebook (`name`,`number`) VALUE ('".$name."','".$num."')"; $res = mysql_query($query); mysql_close(); //$agi->set_variable("CID_NAME", "$name"); return $name; } else { echo "  TC  \n"; //$agi->set_variable("CID_NAME", ""); return false; } } } else { echo " TC   ,    \n"; //$agi->set_variable("CID_NAME", ""); return false; } } //    "  " function oauth_vk($vk, $cookie_file) { unlink($cookie_file); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://oauth.vk.com/authorize?client_id=4951501&scope=friends%2Coffline&redirect_uri=http%3A%2F%2Fwww.truecaller.com%2Fsign-in%2Fvk&response_type=code&state=KKoLuT0vbWEOXfqIW9C0yAvoX7uoEDszIrVOxYSr'); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch); preg_match("/<form method=\"post\" action=\"(.+)\">/i", $data, $matches); if (count($matches)>0) $action=$matches[1]; preg_match("/<input type=\"hidden\" name=\"_origin\" value=\"(.+)\">/i", $data, $matches); if (count($matches)>0) $origin=$matches[1]; preg_match("/<input type=\"hidden\" name=\"ip_h\" value=\"(.+)\" \/>/i", $data, $matches); if (count($matches)>0) $ip_h=$matches[1]; preg_match("/<input type=\"hidden\" name=\"lg_h\" value=\"(.+)\" \/>/i", $data, $matches); if (count($matches)>0) $lg_h=$matches[1]; preg_match("/<input type=\"hidden\" name=\"to\" value=\"(.+)\">/i", $data, $matches); if (count($matches)>0) $to=$matches[1]; if (isset($action) && isset($origin) && isset($ip_h) && isset($lg_h) && isset($to)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $action ); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array( '_origin'=>$origin, 'ip_h'=>$ip_h, 'lg_h'=>$lg_h, 'to'=>$to, 'email'=>$vk['login'], 'pass'=>$vk['password'] )); $data = curl_exec($ch); curl_close($ch); preg_match('/Location: (http\:\/\/www\.truecaller\.com\/sign\-in\/vk\?code\=.+)\&state.+/', $data, $matches); if (count($matches)>0) $location=$matches[1]; if (isset($location)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $location); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch); if (preg_match("/\<title\>Signed In \| Truecaller\<\/title\>/i", $data)) { echo " VK  \n"; return true; } else { echo "     VK /      \n"; return false; } } else { echo "     VK /  location  \n"; return false; } } else { echo "     VK /     action='".$action."', origin='".$origin."', ip_h='".$ip_h."', lg_h='".$lg_h."', to='".$to."'\n"; return false; } } ?> 

I have a dialplan for LUA, so in extensions.lua:

 local call = {} call.cid_num = channel["CALLERID(num)"]:get() call.cid_name = "" --      local handle = io.popen("/var/lib/asterisk/agi-bin/phonebook.php "..call.cid_num) local founded_name = handle:read("*a") handle:close() app.Noop(founded_name) _, _, call.cid_name = string.find(founded_name,"%s%s.+%s'(.+)'") channel["CALLERID(name)"]:set(call.cid_name) 

In this script, there is no accounting for blocking spam contacts. This article is described as an “overview” of the possibility of integrating such a wonderful service Truecaller with your PBX.

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


All Articles