[Asterisk-Users] CallerID name lookup AGI script

Nabeel Jafferali
Sat, 07 May 2005 14:35:29 -0000

Jim:

I modified your script to first look up Google and then look up 411.com.
It's better for me, because 411.com has Canadian listings too. I still left
Google in because it's much faster and if it has information, I'd rather
use
that. I removed the area code thing because it's no use to me. I also
removed the reversing first/last names thing that you did.

Here's the code:

#!/usr/bin/perl
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$number = $ARGV[0];
$found = 0;

open(RESULTS, "/usr/bin/curl -s -m 2 -A Mozilla/4.0
http://www.google.com/search?q=phonebook:$number |");
while (<RESULTS>) {
  if (m/Residential Phonebook/ || m/Business Phonebook/) {
    $found = 1;
    @fields = split(/>/);
    @result = split(/-/, $fields[35]);
    chop($result[0]);
    $name = $result[0];
  }
  if (m/did not match any/) {
    $found = 0;
  }
}

if ($found == 0) {
  open(RESULTS, "/usr/bin/curl -s -m 2 -A Mozilla/4.0
http://www.411.com/10668/search/Reverse_Phone?phone=$number |");
  while (<RESULTS>) {
    if (m/__FIRST/) {
      $found = 1;
      @fname = split(/\"/);
    }
    elsif (m/__LAST/) {
      @lname = split(/\"/);
      $name = $fname[1] . " " .$lname[1];
    }
  }
}

$AGI->set_variable('googlename', "\"$name\"");

--
Nabeel Jafferali
X2 Networks
www.x2n.ca
T: 1.647.722.6900
   1.877.VOIP.X2N
F: 1.866.655.6698
FWD: 46990

> -----Original Message-----
> From: asterisk-users-bounces@lists.digium.com [mailto:asterisk-users-
> bounces@lists.digium.com] On Behalf Of Jim Meehan
> Sent: April 9, 2005 3:05 PM
> To: asterisk-users@lists.digium.com
> Subject: [Asterisk-Users] CallerID name lookup AGI script
>
> Hi all,
>
> My VoIP provider (race.com) doesn't send name info with CallerID, so I
> wrote
> an AGI script that does the following:
>
> 1) If it's a toll free number (800|888|877|866), set the CallerID name
to
> "TollFree Caller"
> 2) Use curl to look up the number in Google phonebook
> 3) If a business listing, set the CallerID name to business name, as is.
> 4) If it's a residential listing, reverse the listing so it's last
name
> first,
> then set the CallerID name to that.
> 5) If there's no match in Google phonebook, look up the NPA/NXX on
> www.areacodedownload.com and set the CallerID name to "@ST
RATECENTER"
> where
> "ST" is the two-letter state abbreviation, and
"RATECENTER" is the name of
> telco rate center in that state.
>
> Thought some of you might find this AGI script useful, so I'm including
it
> below.  It requires the Asterisk::AGI perl module.
>
> There are other reverse phone lookup sources that are more complete than
> Google's, but they are harder to screen scrape.  Also, I probably could
> have
> made this a little cleaner if I used the Google API rather than screen
> scraping with curl/perl.  Please feel free to take a shot at making any of
> those modifications.
>
> Here's a snippet from my extensions.conf where it gets called:
>
> exten => s,1,AGI(callerid.agi|${CALLERIDNUM})
> exten => s,2,SetCallerId,"${googlename}
<${CALLERIDNUM}>"
> exten => s,3,Dial(${PHONES},30,r)
> exten => s,4,Answer
> exten => s,5,Wait(2)
> exten => s,6,Voicemail(u3001)
> exten => s,7,Hangup
>
>
> And here's the script:
>
> #!/usr/bin/perl
>
> use Asterisk::AGI;
>
> $AGI = new Asterisk::AGI;
>
> $number = $ARGV[0];
>
> if ($number =~ m/(800|888|877|866)\d{7}/) {
>   $AGI->set_variable('googlename', "\"TollFree
Caller\"");
>   exit 0;
> }
>
> open(RESULTS, "/usr/bin/curl -s -m 2 -A Mozilla/4.0
> http://www.google.com/search
> ?q=phonebook:$number |");
>
> while (<RESULTS>) {
>   if (m/Residential Phonebook/) {
>     $reverse = 1;
>     @fields = split(/>/);
>   }
>   if (m/Business Phonebook/) {
>     @fields = split(/>/);
>   }
>   if (m/did not match any/) {
>     @digits = split(//, $number);
>     $npa = $digits[0] . $digits[1] . $digits[2];
>     $nxx = $digits[3] . $digits[4] . $digits[5];
>     open(LOCATION, "/usr/bin/curl -s -m 2 -A Mozilla/4.0
> http://www.areacodedown
> load.com/$npa/$nxx/ |");
>     while (<LOCATION>) {
>       if (m/>State</) {
>         $line = <LOCATION>;
>         $line =~ m/\"\#CACACA\">\w* (\w\w)<\/td>/;
>         $name = "\@$1";
>       }
>       if (m/>Rate Center</) {
>         $line = <LOCATION>;
>         $line =~ m/\"\#CACACA\">((\w|\s)*)<\/td>/;
>         $name = $name . " " . $1;
>       }
>     }
>     $AGI->set_variable('googlename',
"\"$name\"");
>     exit 0;
>   }
> }
>
> @result = split(/-/, $fields[35]);
> chop($result[0]);
> if ($reverse) {
>   @words = split(/ /, $result[0]);
>   $last = pop(@words);
>   unshift(@words, "$last,");
>   foreach $word (@words) {
>     $name = $name . $word . " ";
>   }
> }
> if ($reverse == 0) {
>   $name = $result[0];
> }
>
> $AGI->set_variable('googlename',