Account  |   Register  |   Pricing  |   Asterisk  |   HTTP API   |   ENUM   |   Code  |   Batch Client   |   Networks  |   Status  |   About  |   Contact

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:


# 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

# 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")
elsif($carrier eq "23410")

# .. 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 @
	my $browser   = LWP::UserAgent->new;
	my $url       = "";
	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;

© 2016 Comcetera Ltd. is a product of Comcetera™ Ltd
Comcetera Ltd is Registered in England, Company 6058188 at Unit 13 Freeland Business Park, Poole BH16 6FH.