Caller's name lookup

Catalog of dial plans
MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Caller's name lookup

Post by MikeTelis » Wed Dec 09, 2009 6:14 am

The following code snippet is an augmented version of Poor man's CNAM lookup by teddy_b.

Improvement: the code uses White pages Developer API to perform reverse lookup and retrieve caller's name. White pages' reverse lookup is only used if the caller's name wasn't found in local CNAM table. That is, local CNAM always overrides Whitepages.

Update: teddy_b's code is writing caller's name by setting values on req object (req.Header.From.FromName = CNAM[@num] if (CNAM[@num])). This no longer works (been replaced with fd, fu, fh and sys.SetFromHeader). Read more in this thread. This code is using SetFromHeader and fu to accommodate these changes.

If you wish to use my code, You'll need to register on White pages Developer API and get API key. Insert key value where indicated in the code below:

Code: Select all

WP_key = 'Your_API_KEY_VALUE'     # White Pages API key

# CNAM Hash Table (phonebook) 
CNAM = { 
  '12221234567' => 'Office', 
  '12221231234' => 'Boss', 
  '12221232233' => "Wife's Cell", 
  # ...etc... 
  '12221233333' => 'Home' 
} 

  if sys.In               # If incoming call...
    name = req.Header.from.FromURI.User.to_s    # Get caller ID

    # Prepend 10-digit numbers with "1" (US country code),
    # remove 011 prefix (if present) and save back to headers

    name = ('1' + name) if name =~ /^[2-9]\d\d[2-9]\d{6}$/
    name.sub!(/^(011|\+)/,'')

    # Check CNAM first. If not found and US number, try to lookup caller's name in Whitepages
    if !(cname = CNAM[name]) && name =~ /^1([2-9]\d\d[2-9]\d{6})$/ 
      url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fapi.whitepages.com%2Freverse_phone%2F1.0%2F%3Fphone%3D#{$1}%3Bapi_key%3D#{WP_key}'%20and%20itemPath%3D'wp.listings.listing.displayname'&format=json"
      cname = $1 if sys.WebGet(url,4).to_s =~ /"displayname":(?:\[)?"([^"]+)"/ 
    end

    sys.Log("Caller's number: '#{name}'"); sys.Log("Caller's name = '#{cname}'") if cname

    sys.SetFromHeader(cname || name, nil, nil)  # Set FromName for sys.Dial

    # Forward call to bindings. Change FromURI when forwarding to
    # @local, or else Bria won't find contact in its phonebook!

    sys.Dial("#{sys.Username}@local[fu=#{name}]",45)

    sys.Respond(480, "#{sys.Username} Not online") # switch to voice mail

  else                    # Outbound call ...
#... your outbound call processing here
  end
Last edited by MikeTelis on Sat May 15, 2010 12:31 pm, edited 3 times in total.

rmp25
Posts: 64
Joined: Tue Aug 18, 2009 4:45 pm

Re: Caller's name lookup

Post by rmp25 » Thu Dec 10, 2009 3:49 am

Mike, it's great, it's works!
For some reason console gave me a syntax error here:
'12221232233' => 'Wife's Cell',
After I removed this string completely, dialplan plan works as intended.
Thanks so much.

reraikes
Posts: 237
Joined: Thu Aug 13, 2009 10:15 pm

Re: Caller's name lookup

Post by reraikes » Thu Dec 10, 2009 4:10 am

rmp25 wrote:For some reason console gave me a syntax error here:
'12221232233' => 'Wife's Cell',
It's the single-quote character (') in the middle of a single-quoted string that's the problem.

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Thu Dec 10, 2009 4:55 am

I copied CNAM sample table from teddy_b's code and overlooked this error. Fixed. Sorry for inconvenience.

SIPHokie
Posts: 7
Joined: Sun Dec 27, 2009 4:22 pm

Post by SIPHokie » Sun Dec 27, 2009 8:08 pm

MikeTelis,

Would you mind adding a bit of code that will return "Unpublished" to the caller id name if the white pages reverse lookup does not return a name?

Thanks in advance for your help!

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Sun Dec 27, 2009 10:44 pm

Replace this line:

Code: Select all

cname = $1 if sys.WebGet(url,4).to_s =~ /"displayname":(?:\[)?"([^"]+)"/
with

Code: Select all

cname = (sys.WebGet(url,4).to_s =~ /"displayname":(?:\[)?"([^"]+)"/) ? $1 : "Unpublished"

SIPHokie
Posts: 7
Joined: Sun Dec 27, 2009 4:22 pm

Post by SIPHokie » Mon Dec 28, 2009 3:46 am

Great! Thanks for your help :D

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Mon Dec 28, 2009 6:59 am

You're welcome, but...

I don't think this is a good idea, because you in fact lose caller ID of all "unpublished" callers.

With my original code, you get 3 chances:

1. If caller's number is in your CNAM table, you'll get his name displayed.

2. If caller's number is published, you'll get his name from Whitepages' records.

3. If both above failed, you'll get his number displayed. If this number is found in your phone's (or softphone's) contact list, most likely it will display his/her name as well but at least you'll know the number and might guess who's calling by, say, area code.

With the change, you'll lose this 3rd chance.

SIPHokie
Posts: 7
Joined: Sun Dec 27, 2009 4:22 pm

Post by SIPHokie » Tue Dec 29, 2009 2:58 am

Yeah, well now most calls come in as "unpublished" on the first line and the phone number on the second line of my phone.

Is there an easy way to change the phone number to add some spaces so instead of reading "12345678" it will read "1 234 5678"?

Thanks again

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Post by MikeTelis » Tue Dec 29, 2009 6:18 am

Is there an easy way to change the phone number to add some spaces so instead of reading "12345678" it will read "1 234 5678"?
I think this conversation is worth a dedicated thread. Copy formatNum code to your dialplan and place

cname = formatNum(cname)

just before sys.Log(...). Output format is somewhat different from what you asked but hope you'll like it.

Post Reply