YouTube - :)

Merion Networks

Dear friend, how often do your partners, clients or some spam companies call to your organisation? Our company, for example, recieve such calls quite ofthen :) So we realised that it would be nice to see company`s name on your operator`s phone display.

As data source, we decided to use Yandex.Maps Places API with Asterisk AGI. So, let`s start.

If amount of requests to Yandex.Maps Places API do not exceed 500 requests per 24 hours, you can use it for free otherwise agreement signment is mandatory. You can read through terms of use via following link: here

Step-by-step video

Getting a token

Firstly, go to the Yandex developer`s cabinet via following link and click Get key :

API  key receiving for Yandex Organization Search

Then click on Connect services button and then click on Yandex.Maps Places API and complete as shown below:

 API  key for Yandex.Maps Places

Now remember your token`s value, and let`s start AGI - script set up process

AGI script creation on PHP

Approximate script structure is quite simple: we are taking DID from CallerID , making CURL request towards Yandex API. Answer we`ll get in JSON format, which we need to decode into usual array, nd then disassemble it by the elements. Let`s take a look inside the script: (comments, as usual after double slash sign //):

#!/usr/bin/php -q
$agi = new AGI(); 
$cid = $agi->request['agi_callerid']; // getting the CallerID;
$key = 'd415b201-3d9b-45a7-95b9-000000000000'; // add your API key into this variable;
$yandex_url = "$cid&results=1&type=biz&lang=ru_RU&apikey=$key"; // URL, on which we`ll make data request;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$yandex_url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070725 Firefox/");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$yandex = curl_exec ($ch); // execute the request and store it in the variable;
$yandex_o = json_decode($yandex, true); // decode JSON into an array;
$views = $yandex_o['features'][0]['properties']['CompanyMetaData']['name']; // pull out an element with organisation name from the array;
if (empty($views)) {$agi->set_variable("lookupcid", "$cid");}  else {
$agi->set_variable("lookupcid", "$views");
} // if the variable with company name is empty, then to the dialplan we return the value of lookupcid value, which should be equal to caller number, otherwise we`ll write organisation name into this variable;
Download AGI script

Save this script as company_name.php in /var/lib/asterisk/agi-bin directory and execute following commands in server console:

dos2unix /var/lib/asterisk/agi-bin/company_name.php
chown asterisk:asterisk /var/lib/asterisk/agi-bin/company_name.php
chmod 775 /var/lib/asterisk/agi-bin/company_name.php
Also we think that it can be quite convenient to send company`s business scope, so operator can see it while he is receiving an inbound call, so he can briefly estimate what this call is gonna be about. You can select this data from the following elements: ['features'][0]['properties']['CompanyMetaData']['Categories'][0-2]['name'];. In the highlighted range (0 2) you can put only 1 digit.

Putting the script into the dialplan

The only part eft is the easiest part we need to put our script into the dialplan. We`ll make it in calling context 8899:

exten => 8899,n,AGI(company_name.php)
exten => 8899,n,Set(CALLERID(name)=${lookupcid})

Now we need to reload the dialplan with dialplan reload command. Let`s call from our desk phone to our corporate number +74996491913 and we should see the following:

Company`s name on Asterisk connected phone Company`s name on Asterisk connected phone