Number Portability Lookup - Sample Code for Asterisk AGI Script Integration
AGI / Perl Sample Code:
The following code snippet performs a carrier lookup on a given mobile telephone number to find their mobile network provider. The code can be integrated into your perl AGI script in order to perform network lookup queries at any point during your script, routing calls as necessary based on the response.
This solution uses the LWP perl module, which must be installed on your server; You can install this with apt-get install libwww-perl on Debian and Ubuntu servers or via your distribution's own package installer. Alternatively you can download it and install by hand from CPAN.
Other Code Samples & Asterisk Carrier Lookup Examples
To find code samples for other languages, click here.
To find an example of integrating carrier lookups directly into your extensions.conf dialplan, click here.
Code Sample:
#!/usr/bin/perl
# Sample Number Portability Lookup Query using Perl LWP
# This code may be freely used in your own AGI scripts to provide
# carrier lookup capability through numberportabilitylookup.com
# It is intended to be included in your own AGI script, and is
# as such, not a complete AGI script in its own right.
use LWP::UserAgent;
# Get the number we're calling. In this example we read the EXTEN
# variable to get the number the user just dialed. You could fetch
# the number from anywhere, depending on the purpose of your script.
my $dest = $AGI->get_variable("EXTEN");
# Perfom a lookup
my $carrier = lookupCarrier($dest);
# Route the call based on the provider...
if ($carrier eq "23415")
{
$AGI->exec('Dial','SIP/+'.$dest.'\@sip-provider-for-vodafone');
}
elsif($carrier eq "23410")
{
$AGI->exec('Dial','SIP/+'.$dest.'\@sip-provider-for-o2');
}
else
{
$AGI->exec('Dial','SIP/+'.$dest.'\@sip-provider-for-others');
}
# .. etc ..
# The lookupCarrier subroutine which is called above, and does all the
# hard work. Include this at the bottom of your AGI script, and don't
# forget to add in your username and password!
sub lookupCarrier
{
my $msisdn=shift;
my $username = "myusername"; # add your own account credentials here.
my $password = "mypassword"; # register an account @ numberportabilitylookup.com
my $browser = LWP::UserAgent->new;
my $url = "http://api.numberportabilitylookup.com/npl";
my $queryData = "user=$username&pass=$password&msisdn=$msisdn";
my $response = $browser->post($url, content=>$queryData);
my $content = $response->content;
my @dataLine=split("\n",$content);
if ($dataLine[0] eq "QUERYOK")
{
($msisdn,$carrier)=split(" ",$dataLine[1]);
}
return $carrier;
}
|