Caller's name lookup

Catalog of dial plans

Re: Caller's name lookup

Postby MikeTelis » Tue Nov 15, 2011 3:45 am

It should be something like this:
Code: Select all
    if !(@cname = CNAM[@cid]) && @cid =~ /^1([2-9]\d\d[2-9]\d{6})$/ && defined?(WP_key)
      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'&format=json"
      if js = sys.WebGet(url,4).to_s
        @cname, dname, city, state = %w(businessname displayname city state).map {|x| js =~ /"#{x}":"([^"]+)"/; $1}
        if !(@cname ||= dname) && defined?(CNAM_key)
          url = "http://cnam.calleridservice.com/query?u=miketelis&k=#{CNAM_key}&n=#{@cid[1..-1]}"
          @cname = sys.WebGet(url,4).to_s
        end
        @cname ||= "#{city}, #{state}" if city && state
      end
    end


You should define both keys:
Code: Select all
WP_key    = '*****'     # White Pages API key
CNAM_key  = '****' # CallerIDservice API key

The code should try WhitePages first and switch to paid service if no luck. Sorry, I didn't have the time to test this chunk of code.
MikeTelis
 
Posts: 1568
Joined: Wed Jul 30, 2008 6:48 am

Re: Caller's name lookup

Postby uhf » Tue Nov 15, 2011 4:25 am

I can't seem to get it to work.

NewCall 04:45:46:489 sip1(7704): Executing script dial plan for call to ####.
DialPlan 04:45:46:505 sip1(7704): There was a syntax error in your dial plan on line 36, please check.
DialPlan 04:45:46:505 sip1(7704): Dialplan cleanup for ####.
DialPlan 04:45:46:614 sip1(7704): Dial plan execution completed without answering and had an execution error message of Dial plan syntax error.
DialPlan 04:45:46:614 sip1(7704): UAS call failed with a response status of 500 and Dial plan syntax error.

Code: Select all
#Ruby
# SIP tracing : true or false
sys.Trace = false
sys.Log("call from #{req.Header.From.FromURI.ToString()} to #{req.URI.User}.")
sys.Log("** Call from #{req.Header.From} to #{req.URI.User} **")

# CNAM Hash Table (phonebook)
CNAM = {
   '12225551212' => 'caller',
}

if !(@cname = CNAM[@cid]) && @cid =~ /^1([2-9]\d\d[2-9]\d{6})$/ && defined?(WP_key)
      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'&format=json"
      if js = sys.WebGet(url,4).to_s
        @cname, dname, city, state = %w(businessname displayname city state).map {|x| js =~ /"#{x}":"([^"]+)"/; $1}
        if !(@cname ||= dname) && defined?(CNAM_key)
          url = "http://cnam.calleridservice.com/query?u=USER&k=MYKEY&n=#{@cid[1..-1]}"
          @cname = sys.WebGet(url,4).to_s
        end
        @cname ||= "#{city}, #{state}" if city && state
      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

   # Do your INCOMING call processing customizations here.
if sys.IsAvailable() then # If SIP Client is logged on, then ring the ATA
sys.Dial("#{sys.Username}@local",25)
sys.Respond(480, "#{sys.Username} Not available")
else
sys.Dial("13195551212@Callcentric",60) # Call Fails over to Cell via Callcentric
sys.Respond(480, "#{sys.Username} Not available")
end


I just noticed I forgot to define my WP API Key. Maybe that's the problem. But I'm tired now, so I'll look at it again tomorrow.
I really don't want the WP lookup in there since it's returning bad results for me.
uhf
 
Posts: 26
Joined: Sat Oct 30, 2010 3:05 am

Re: Caller's name lookup

Postby MikeTelis » Wed Nov 16, 2011 7:35 am

You forgot to define both API keys and there was a missing 'end'. Besides, constant definitions look better when placed in the beginning of your dialplan code :)

And yet, you need to pass caller ID / name to the ATA (see [fu=...] modifier of sys.Dial).

Here is somewhat rectified code:
Code: Select all
#Ruby

# CNAM Hash Table (phonebook)
CNAM = {
   '12225551212' => 'caller',
}

WP_key    = '***'    # White Pages API key
CNAM_key  = '***'    # CallerIDservice API key

# SIP tracing : true or false
sys.Trace = false
sys.Log("call from #{req.Header.From.FromURI.ToString()} to #{req.URI.User}.")
sys.Log("** Call from #{req.Header.From} to #{req.URI.User} **")

if !(@cname = CNAM[@cid]) && @cid =~ /^1([2-9]\d\d[2-9]\d{6})$/ && defined?(WP_key)
  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'&format=json"
  if js = sys.WebGet(url,4).to_s
    @cname, dname, city, state = %w(businessname displayname city state).map {|x| js =~ /"#{x}":"([^"]+)"/; $1}
    if !(@cname ||= dname) && defined?(CNAM_key)
      url = "http://cnam.calleridservice.com/query?u=USER&k=MYKEY&n=#{@cid[1..-1]}"
      @cname = sys.WebGet(url,4).to_s
    end
    @cname ||= "#{city}, #{state}" if city && state
  end
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

# Do your INCOMING call processing customizations here.
if sys.IsAvailable() then # If SIP Client is logged on, then ring the ATA
  sys.Dial("#{sys.Username}@local[fu=#{@cid}]",25)
  sys.Respond(480, "#{sys.Username} Not available")
else
  sys.Dial("13195551212@Callcentric",60) # Call Fails over to Cell via Callcentric
  sys.Respond(480, "#{sys.Username} Not available")
end
MikeTelis
 
Posts: 1568
Joined: Wed Jul 30, 2008 6:48 am

Re: Caller's name lookup

Postby uhf » Wed Nov 16, 2011 3:58 pm

Well, whatever I do the code fails with syntax errors, and in some cases sends multiple calls to my ATA for each incoming call. I have no idea why. I obviously don't understand Ruby enough to figure out whats going wrong, so I'm just going to have to live with the crappy CNAM that Callcentric delivers. I would gladly pay them more money to get better CNAM results, but that's not currently an option.

Thanks for all your help, Mike.
uhf
 
Posts: 26
Joined: Sat Oct 30, 2010 3:05 am

Re: Caller's name lookup

Postby MikeTelis » Wed Nov 16, 2011 6:22 pm

Apparently you did something wrong inserting your API keys (I did check the code for syntax errors before posting it here). You were supposed to replace the whole of your "In" dialplan with my code. Anyway, if you ever come back to this topic, post the Console log you get.
MikeTelis
 
Posts: 1568
Joined: Wed Jul 30, 2008 6:48 am

Re: Caller's name lookup

Postby uhf » Fri Nov 18, 2011 9:19 pm

Mike,

I'm back.. I checked with the cnam provider to see if they had already made a script for this since they have some for asterisk, etc. They don't. But they are interested in making it work.

I have copied your entire script and am using it for incoming calls.

I did find out that the URL string I was previously provided with is wrong. Calleridservice.com gave me a new one to try.
I've pasted it in place of the old one in my dialplan. Also, where does the script find CID_USERNAME? I've pasted my username in place of that in the url script. So that line looks like this:
Code: Select all
url = "http://cnam.calleridservice.com/query?u=uhf&k=#{CID_AUTHKEY}&n=#{name}&t=html"


It appears if I do not define WP_key the script will skip that? Is this correct? I don't want to use Whitepages at all.

Code: Select all
DialPlan 20:59:12:985 sip1(1960): Using dialplan CNAM_lookup for In call to sip:sipsorceryusername@sipsorcery.com;rinstance=203215.
NewCall 20:59:12:985 sip1(1960): Executing script dial plan for call to sipsorceryusername.
DialPlan 20:59:13:031 sip1(1960): call from sip:phonenumber@66.193.176.35 to sipsorceryusername.
DialPlan 20:59:13:031 sip1(1960): ** Call from "UHF" <sip:phonenumer@66.193.176.35>;tag=3530638752-674829 to sipsorceryusername **
DialPlan 20:59:13:047 sip1(1960): There was a missing method exception in your dial plan: undefined method `name' for #<Object:0x000006a @cname=nil>.
DialPlan 20:59:13:047 sip1(1960): Dialplan cleanup for sipsorceryusername.
DialPlan 20:59:13:407 sip1(1960): Dial plan execution completed without answering and had an execution error message of Dial plan missing method.
DialPlan 20:59:13:407 sip1(1960): UAS call failed with a response status of 500 and Dial plan missing method.


If only I'd been born smarter instead of so darn good looking maybe I could make since of all this :mrgreen:
Does the {name} in the URL string need to be defined somewhere or dies it already get defined behind the scenes somewhere by sipsorcery?

Thanks again for your help. I was able to find another ATA that makes it much easier to test than when I have free time rather than constantly reconfiguration the one that I use the rest of the time. Normally I bypass Sipsorcery since I don't have a big need for it other than to hopefully make this CNAM thing work!
uhf
 
Posts: 26
Joined: Sat Oct 30, 2010 3:05 am

Re: Caller's name lookup

Postby MikeTelis » Sat Nov 19, 2011 6:08 pm

You're trying to substitute #{name} in the url string and it fails since the 'name' is not defined in your script.

I believe that the URL was correct but you changed url definition in my original script; that's why it failed.

So, let's go back and change url string to:

url = "http://cnam.calleridservice.com/query?u=#{CNAM_user}&k=#{CNAM_key}&n=#{@cid[1..-1]}"

Then, define both

CNAM_USER = '*****'
CNAM_key = '*****'

in the header of your script. It should work! :)
MikeTelis
 
Posts: 1568
Joined: Wed Jul 30, 2008 6:48 am

Re: Caller's name lookup

Postby uhf » Mon Nov 21, 2011 9:03 pm

Still get the same error:
[codeDialPlan 20:59:17:846 sip1(6820): There was a missing method exception in your dial plan: undefined method `name' for #<Object:0x0000060 @cname=nil>.][/code]
uhf
 
Posts: 26
Joined: Sat Oct 30, 2010 3:05 am

Re: Caller's name lookup

Postby uhf » Mon Nov 21, 2011 10:08 pm

I've found a lot of bugs that I've fixed now. One in particular that I've not gotten passed is this line:
Code: Select all
sys.Log("Caller's number: '#{@cid}'"); sys.Log("Caller's name = '#{@cname}'") if @cname


Obviously, that should write a log entry with the callers number, and name. It does not. It outputs the following to the log:
Code: Select all
DialPlan 21:56:15:838 sip1(7524): Caller's number: ''


The script never queries Calleridservice.com. My best guess as to why is that these lines:
Code: Select all
if !(@cname = CNAM[@cid]) && @cid =~ /^1([2-9]\d\d[2-9]\d{6})$/ && defined?(CNAM_key)
       url = "http://cnam.calleridservice.com/query?u=#{CNAM_user}&k=#{CNAM_key}&n=#{@cid}"

are not making the call properly since it seems that Ruby doesn't now was {@cid} is. I may have added the @ signs, I don't remember. I had a lot of errors that I fixed by adding @'s in places. Anyway, it doesn't work with or without the @ in front of the cid.

Here's my script in it's entirety (with personal info redacted):
Code: Select all
#Ruby

# CNAM Hash Table (phonebook)
CNAM = {
   '12225551212' => 'caller',
}

WP_key    = ''    # White Pages API key
CNAM_user = 'user'
CNAM_key  = 'key'    # CallerIDservice API key

# SIP tracing : true or false
sys.Trace = false
sys.Log("call from #{req.Header.From.FromURI.ToString()} to #{req.URI.User}.")
sys.Log("** Call from #{req.Header.From} to #{req.URI.User} **")

if !(@cname = CNAM[@cid]) && @cid =~ /^1([2-9]\d\d[2-9]\d{6})$/ && defined?(CNAM_key)
       url = "http://cnam.calleridservice.com/query?u=#{CNAM_user}&k=#{CNAM_key}&n=#{@cid}"
      @cname = sys.WebGet(url,4).to_s
 end

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

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

# Forward call to bindings. Change FromURI when forwarding to

# Do your INCOMING call processing customizations here.
if sys.IsAvailable() then # If SIP Client is logged on, then ring the ATA
  sys.Dial("#{sys.Username}@local[fu=#{@cid}]",25)
  sys.Respond(480, "#{sys.Username} Not available")
else
  sys.Dial("12225551212@Callcentric",60) # Call Fails over to Cell via Callcentric
  sys.Respond(480, "#{sys.Username} Not available")
end


I think I'm getting there!
Thanks for your patience with me, and all your help, Mike!
uhf
 
Posts: 26
Joined: Sat Oct 30, 2010 3:05 am

Re: Caller's name lookup

Postby MikeTelis » Tue Nov 22, 2011 6:32 am

You're not getting anything because @cid is unassigned. This instance variable is supposed to receive Caller ID (caller's number), like it's done in my Flexible dialplan, Ver. 2. Something like this:

@cid = req.Header.from.FromURI.User.to_s # Get caller ID

Please refer to the dialplan code. The above line of code will assign a raw Caller ID to @cid. Depending on DID provider, caller ID might have leading "+", or "011" or some other prefix. Some DIDs send 10-digit caller ID.

Note that you need 10-digit caller ID for CNAM service and therefore, you must convert whatever comes from DID into 10-digit format. For example:
Code: Select all
    @cid = req.Header.from.FromURI.User.to_s    # Get caller ID

    # Prepend 10-digit numbers with "1" (US country code) and remove int'l prefix (if present)

    @cid = ('1' + @cid) if @cid =~ /^[2-9]\d\d[2-9]\d{6}$/
    @cid.sub!(/^(\+|00|011)/,'')   # Remove international prefixes, if any

and then remove leading '1':

url = "http://cnam.calleridservice.com/query?u=#{CNAM_user}&k=#{CNAM_key}&n=#{@cid[1..-1]}"

Got the idea? ;-)
MikeTelis
 
Posts: 1568
Joined: Wed Jul 30, 2008 6:48 am

PreviousNext

Return to Use Cases



Who is online

Users browsing this forum: undenlyimmeni and 0 guests

Powered by phpBB® Forum Software © phpBB Group
Theme created by StylerBB.net
cron