[Asterisk-Users] CallerID name lookup AGI script

Jim Meehan jmeehan-asterisk at vpizza.org
Sat Apr 9 12:04:39 MST 2005


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', "\"$name\"");
 



More information about the asterisk-users mailing list