[Asterisk-Users] CallerID name lookup AGI script

Brian Dingman
Sun, 10 Apr 2005 09:16:57 -0000

Jim,
Thanks for sharing this. I am currently using cidlookup.agi written by
James Golovich. http://asterisk.gnuinter.net/

However the problem I have with that script and probably this one also
is that my provider sends the number as +16105551212 so I need a way
to strip out the leading two characters.  Right now his just removes
everything non-numeric but that still leaves me with 16105551212 which
fails on lookups:
#remove everything non numeric from callerid string
$callerid =~ s/[^\d]//g;

I know nothing about scripting in perl. Can you offer any assistance
in how to strip the 2 leading characters? His callerid script is part
of the asterisk-perl package at http://asterisk.gnuinter.net/ ---
might want to take a look at how he does his URL queries for info as
well if you haven't already.

I really like the additional npa/nxx lookup that you are doing. Maybe
combining your efforts with his script would be helpful. He also
cache's the lookup to a directory to improve performance.


On Apr 9, 2005 3:04 PM, Jim Meehan <jmeehan-asterisk@vpizza.org>
wrote:
> 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',