671 IT Telegram . ?

Merion Networks

Dear friend, would you be interested in having an ability to identify where the call is coming from? Based on this information, you can route your calls to different call center agents like agent #1 for Paris, agent #2 for Amsterdam and etc. Also you can make different voice announcements based on city category like in following scenario Hello! You are calling to COMPANY_NAME. In Singapore it's ETC +8, nd we are putting you through to the call agent

It's not hard to create such announcement. Read our material about speech synthesis on Asterisk by this link. :)

It's not an easy task to keep and maintain database with phone prefixes. What should be your preferred choice? To use external API, and it will be better if its going to be free. And yeah, we have one of those. :)


numverify.com

To identify geographical are we will use numverify service. It has quite good and simple API, and what is most important it's totally free when you are not exceeding 250 requests to the API during the month (12 13 requests per business day). Follow by link and register for the free account:

Numverify registration

Click Sign Up on required rate, complete the form (leave your contact details) and receive the API key:

Getting the API

Copy your API key and start to prepare your Asterisk script.


Telegram - bot

In the beginning you need to create a Telegram bot. You can read how to do it by following link (how to receive API token and chat ID) or just simply Google it:


AGI script

On this stage you should have an API token for the Telegram bot and group chat ID. We are going to execute AGI script from the dialplan on inbound call and send data to Telegram :)

#!/usr/bin/php -q
<?php
#parse callerid
require('phpagi.php'); 
$agi = new AGI(); 
$cid = $agi->request['agi_callerid'];
#Telegram connection data
$token = "your_Telegram_api ";
$chat_id = "chat ID";
#state your numverify API - key
$apikey = "numverify_API";
# cut 10 digits from the number and put 7 in front (in order to receive proper number format, 7 works for Russia)
$phone_loc = substr($cid, -10);
$phone_loc = "7$phone_loc";
#request to API and process the results
$numverify_url = "http://apilayer.net/api/validate?access_key=$apikey&number=$phone_loc";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$numverify_url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$numverify = curl_exec ($ch);
curl_close($ch);
#decode JSON
$numverify_o = json_decode($numverify, true);
print_r ($numverify_o);
#form an array in order to send it to Telegram
$arr = array(
'Dear colleagues, there is an incoming call:' => $cid,
'Country code:' => $numverify_o['country_code'],
'Country name:' => $numverify_o['country_name'],
'Location:' => $numverify_o['location'],
'Line type (PSTN/mobile):' => $numverify_o['line_type'],
);
foreach($arr as $key => $value) {
$txt .= "".$key." ".$value."%0A";
};
#send the data to Telegram API
fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");
?>

Download the script

After downloading the file change the file extension from .txt to .php

Nice! Save that script as numverify.php and upload it to the following folder: /var/lib/asterisk/agi-bin. It's extremely easy to do it using WinSCP. Then execute following commands through the SSH connection:

dos2unix /var/lib/asterisk/agi-bin/numverify.php
chown asterisk:asterisk /var/lib/asterisk/agi-bin/numverify.php
chmod 775 /var/lib/asterisk/agi-bin/numverify.php

Let's fix the dialplan. Prepare script execution in the trunk, for example:

exten => _.,n,AGI(numverify.php)

Reload the dialplan with the following command:

dialplan reload

Verification

So, let's check it out and call from the following number: +74996491913:

Location identifying test
Line type landline - PSTN number, mobile -mobile number and etc. More details you can find in numverify documentation

It works! What if call is coming from the Vladivostok? :)

Country and city identification in Asterisk

Great! Now, depending on caller's city you can modify call routing! In Moscow's case return to the dialplan the variable with value 1, for Novosibirsk 2, for Vladivostok 3 etc. Example you can check out here:


?