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\"");
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', "\"$name\""); > > _______________________________________________ > Asterisk-Users mailing list > Asterisk-Users@lists.digium.com > http://lists.digium.com/mailman/listinfo/asterisk-users > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users >
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', "\"$name\""); > > _______________________________________________ > Asterisk-Users mailing list > Asterisk-Users@lists.digium.com > http://lists.digium.com/mailman/listinfo/asterisk-users > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users
sorry, not sure if i am understanding your purpose, you want to alter the callerid_id?? if im right, then, could you show me the code inside cid_rewrite.php?? best regards On 5/20/05, pbx@itsngroup.com <pbx@itsngroup.com> wrote:> I have been trying to get this script to work as well > cid_rewrite > > However this is what the CLI reports: > -- Executing EAGI("Zap/1-1", "cid_rewrite/cid_rewrite.php|us") > -- Launched AGI Script /var/lib/asterisk/agi-bin/cid_rewrite.php > AGI Tx >> agi_request: cid_rewrite.php > AGI Tx >> agi_channel: Zap/1-1 > AGI Tx >> agi_language: en > AGI Tx >> agi_type: Zap > AGI Tx >> agi_uniqueid: 1116600703.79 > AGI Tx >> agi_callerid: 619xxxxxxx <-- it is getting caller id number > AGI Tx >> agi_calleridname: unknown > AGI Tx >> agi_callingpres: 0 > AGI Tx >> agi_callingani2: 0 > AGI Tx >> agi_callington: 0 > AGI Tx >> agi_callingtns: 0 > AGI Tx >> agi_dnid: unknown > AGI Tx >> agi_rdnis: unknown > AGI Tx >> agi_context: incoming-zap > AGI Tx >> agi_extension: s > AGI Tx >> agi_priority: 2 > AGI Tx >> agi_enhanced: 1.0 > AGI Tx >> agi_accountcode: > AGI Tx >> > -- AGI Script cid_rewrite.php completed, returning 0 > -- Executing Dial("Zap/1-1", "IAX2/4000|20|rtT") > -- Called 4000 > -- Call accepted by 129.46.90.210 (format gsm) > -- Format for call is gsm > > However, it does not have any information being returned. > > I have edited the agi_config.php to point to where the information is. But > it just is not getting anything. > > Help???... > > _______________________________________________ > Asterisk-Users mailing list > Asterisk-Users@lists.digium.com > http://lists.digium.com/mailman/listinfo/asterisk-users > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users >-- "Su nombre es GNU/Linux, no solamente Linux, mas info en http://www.gnu.org"