Announcement

Collapse
No announcement yet.

Reverse Web Phone Number Lookup

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Reverse Web Phone Number Lookup

    I'm developing an HS3 interface to a Panasonic PBX, and the unit I have will only pass to me the callerid phone number, without name.

    I want to announce in real time via whole house Sonos speakers who the caller is while the phone is ringing, plus logging, etc.

    I will access my home contacts information to get a name from a known phone number, but when I get a call from an unknown number, I'd like to do a web call providing the phone number and retrieving the name.

    I know there are services with web APIs to do this. I talked to whitepages.com today, but they really do not have a service for such low volume 5-10 calls per day.

    I do not mind paying a fee for the information, but it has to be reasonable for a low volume. $20 per month is not reasonable.

    Anyone with experience with doing something like this?

    tenholde
    tenholde

    #2
    This is a shadow of why I gave up on the home PBX.

    I suggest you check out the ObiHai forum. There are a lot of good ideas in the vein of your question posted there.
    Originally posted by rprade
    There is no rhyme or reason to the anarchy a defective Z-Wave device can cause

    Comment


      #3
      Great idea Ed.
      Don

      Comment


        #4
        Originally posted by tenholde View Post
        I know there are services with web APIs to do this. I talked to whitepages.com today, but they really do not have a service for such low volume 5-10 calls per day.

        <snip>

        Anyone with experience with doing something like this?
        Yep. You want OpenCNAM - it's perfect for what you're doing... and it's free for low-volume usage.

        Comment


          #5
          Originally posted by sacarino View Post
          Yep. You want OpenCNAM - it's perfect for what you're doing... and it's free for low-volume usage.
          Thanks, look like what I need. I'll start playing with it tonight.

          tenholde
          tenholde

          Comment


            #6
            Ed:

            did you have any luck using OpenCNAM? I can't figure out the proper format.


            Originally posted by tenholde View Post
            Thanks, look like what I need. I'll start playing with it tonight.

            tenholde
            Don

            Comment


              #7
              This worked for me. I didn't supply userid nor authid, so it retrieved the result from cache.

              Let me know how I can help.




              Code:
               Public Sub MakeHttpRequest(ByVal Parms As Object)
                      Dim BaseUrl As String = " https://api.opencnam.com/v2/phone/"
                      Dim ParmArray() As String
                      Dim Result As String
                      ParmArray = Parms.ToString.Split("|")
                      Result = SendOpenCNAM(BaseUrl & "3148425089")
                      hs.WriteLog("OpenCNAM Test", Result)
                  End Sub
                  Function SendOpenCNAM(ByVal sURL As String) As String
                      Dim strData As String = ""
                      Dim tlRequest As System.Net.HttpWebRequest
                      Dim tlResponse As System.Net.HttpWebResponse
                      Dim tlStatusCode As Integer
                      Try
                          tlRequest = CType(System.Net.WebRequest.Create(sURL), System.Net.HttpWebRequest)
                          tlRequest.ContentType = "text/html"
                          tlRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"
                          tlRequest.Method = "GET"
                          tlRequest.Timeout = 1000
                          tlRequest.AllowAutoRedirect = True
                          tlResponse = CType(tlRequest.GetResponse(), System.Net.HttpWebResponse)
                          tlStatusCode = tlResponse.StatusCode
                          If tlStatusCode = System.Net.HttpStatusCode.OK Then
                              strData = New System.IO.StreamReader(tlResponse.GetResponseStream()).ReadToEnd()
                          Else
                              Return ""
                              hs.WriteLog("SendOpenCNAM", "HttpWebRequest Returned: " & tlStatusCode.ToString)
                          End If
                          tlResponse.Close()
                      Catch ex As Exception
                          hs.WriteLog("SendOpenCNAM", "HTTP Request Failed: " & ex.ToString)
                      End Try
                      Return strData
                  End Function
              tenholde

              Comment


                #8
                Thanks Ed;

                I was just trying to put the query in the browser address. I tried your code and got back the familiar


                Nov-21 9:41:00 PM Warning VB.Net script exception(0), re-starting: Object reference not set to an instance of an object.
                Nov-21 9:41:00 PM Event Running script in background: C:/HomeSeer HS3/scripts/reverselookup.vb

                How are you imputting the number you want to look up?
                Don

                Comment


                  #9
                  Just threw that together to see how the interface works. Hard-coded the phone number into the code.

                  I have more work to do on handling exceptions and using the authid for deeper searches (if registered).

                  The code will eventually take a phone number as parameter, and return either a null string or the name associated with the phone number. Any exceptions will be displayed in the HS log.

                  The phone number will be received from my Panasonic PBX.

                  Are you by any chance using tenScripting? If so, where is the script failing?

                  tenholde
                  tenholde

                  Comment


                    #10
                    Here is a more complete Script called: GetCallerId

                    Here is the code:

                    Code:
                    Public Sub Main(ByVal Parms As Object)
                            ''
                            '' Script to test OpenCNAM processing
                            ''
                            ''  Specify the 10-digit phone number as the parameter 
                            ''
                            Dim ParmArray() As String
                            '' Get test script parameter  (Phone Number)
                            ParmArray = Parms.ToString.Split("|")
                            '' Get Name from OpenCNAM and write to HS Log
                            hs.WriteLog("CallerId Test", "P=" & ParmArray(0).ToString & "  N=" & GetFromOpenCNAM(ParmArray(0)))
                        End Sub
                        Public Function GetFromOpenCNAM(PhoneNumber As String)
                            ''
                            '' GetFromOpenCNAM     
                            ''
                            ''   Input:  10 Digit Phone Number (string)
                            ''   Returns: Associated Name  (or blank, if no data or request fails)
                            ''
                            ''      First, a request is made to OpenCNAM without authorization codes   (no charge)
                            ''      If no data retrieved, a second request is made using authorization codes   ($0.004 per request)
                            ''
                            Dim Result As String
                            '' Validate parm is 10 digits (more error checking needed here)
                            If PhoneNumber.Length <> 10 Then
                                hs.WriteLog("GetFromOpenCNAM", "Supplied parameter not 10 digits exactly: " & PhoneNumber.ToString)
                                Return ""
                            End If
                            '' Try without authorization codes
                            Result = SendOpenCNAM(PhoneNumber, False)
                            '' If failed, try with authorization codes
                            If Result = "" Then Result = SendOpenCNAM(PhoneNumber, True)
                            Return Result
                        End Function
                        Function SendOpenCNAM(ByVal sPhoneNumber As String, UseAuthentication As Boolean) As String
                            ''
                            '' SendOpenCNAM
                            ''
                            ''      Input:  Phone Number
                            ''              Boolean to specify if authorization is to be used
                            ''
                            ''      Returns:  Associated Name (or blank if not fournd)
                            ''
                            Dim strData As String = ""
                            Dim tlRequest As System.Net.HttpWebRequest
                            Dim tlResponse As System.Net.HttpWebResponse
                            Dim tlStatusCode As Integer
                            Dim BaseUrl As String = " https://api.opencnam.com/v2/phone/"
                            Dim AuthParms As String = "?account_sid=xxxxxxx&auth_token=xxxxxxxxxx"
                            Dim sUrl As String
                            sUrl = BaseUrl & sPhoneNumber
                            If UseAuthentication Then
                                sUrl = sUrl & AuthParms
                            End If
                            Try
                                tlRequest = CType(System.Net.WebRequest.Create(sUrl), System.Net.HttpWebRequest)
                                tlRequest.ContentType = "text/html"
                                tlRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"
                                tlRequest.Method = "GET"
                                tlRequest.Timeout = 1000
                                tlRequest.AllowAutoRedirect = True
                                tlResponse = CType(tlRequest.GetResponse(), System.Net.HttpWebResponse)
                                tlStatusCode = tlResponse.StatusCode
                                If tlStatusCode = System.Net.HttpStatusCode.OK Then
                                    strData = New System.IO.StreamReader(tlResponse.GetResponseStream()).ReadToEnd()
                                Else
                                    Return ""
                                    hs.WriteLog("SendOpenCNAM", "HttpWebRequest Returned: " & tlStatusCode.ToString)
                                End If
                                tlResponse.Close()
                            Catch ex As System.Net.WebException
                                If ex.Message.Contains("(404)") Then
                                    '' 404   No Data for Phone Number, return blank
                                    Return ""
                                End If
                                If ex.Message.Contains("(401)") Then
                                    '' 401   Not Authorized, account codes invalid
                                    hs.WriteLog("SendOpenCNAM", "Authorization Failed, URL: " & sUrl)
                                    Return ""
                                End If
                                If ex.Message.Contains("(400)") Then
                                    '' 400   Phone Number Provided is Invalid
                                    hs.WriteLog("SendOpenCNAM", "Invalid Phone Number, URL: " & sUrl)
                                    Return ""
                                End If
                                If ex.Message.Contains("(403)") Then
                                    '' 403   Exceeded Daily Request Count
                                    hs.WriteLog("SendOpenCNAM", "Exceeded Request Limit, URL: " & sUrl)
                                    Return ""
                                End If
                                If ex.Message.Contains("(402)") Then
                                    '' 402  Deliquent Account
                                    hs.WriteLog("SendOpenCNAM", "Account is Deliquent, URL: " & sUrl)
                                    Return ""
                                End If
                                '' ELSE
                                hs.WriteLog("SendOpenCNAM", "HTTP Request Failed: " & ex.ToString)
                                Return ""
                            End Try
                            Return strData
                        End Function
                    Here is HS3 event defined to call the script entry point Main, with a phone number as a parameter:





                    Here is the log entries created when I ran the event:



                    Let me know if I can explain any of the code.

                    While I developed this under HS3, I do not think there is any reason it would not run as is under HS2.

                    Note that you have to supply your own authorization codes, or eliminate the second call that uses the auth codes. I've place xxxxxx for auth codes.

                    tenholde
                    Last edited by tenholde; November 22, 2014, 10:12 PM.
                    tenholde

                    Comment


                      #11
                      Great! thanks. I'll play with it a bit.

                      Don
                      Don

                      Comment

                      Working...
                      X