Google+Voice+Integration

The following is just a quick rough sample to demonstrate how to integrate google voice into ASP.NET (or any .NET app). You can use the following to make calls or send sms.

code format="vbnet" Imports System.Net Imports System.Linq Imports System.Text Imports System.Text.RegularExpressions Imports System.IO Imports System.Web

Partial Class final Inherits System.Web.UI.Page

Private initialLoginUrl As String = Resources.GVResource.initialloginurl Private secondlogin As String = Resources.GVResource.secondlogin Private voiceurl As String = Resources.GVResource.voiceurl Private callurl As String = Resources.GVResource.callurl Private gvUserName As String = Resources.GVResource.gvusername Private gvServicePassword As String = Resources.GVResource.gvpassword Private sendsmsurl As String = Resources.GVResource.sendsmsurl

Public Shared myCookieJar As CookieContainer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim galx As String = String.Empty Dim rnr As String = String.Empty Dim mainLoginPage As String = String.Empty Dim numberToCall As String = "14071111111" Dim GVForwardingNumber As String = "14079999999"

myCookieJar = New CookieContainer galx = InitialLogin("anything") mainLoginPage = MainLogin(galx)

ParseForRNR(GetRnrPage) If MakeAcall(numberToCall, GVForwardingNumber) Then Response.Write("Call Completed") Else Response.Write("Call Failed") End If

'If SendSMS(numberToCall, "hello.testing") Then '   Response.Write("Message Sent") 'Else '   Response.Write("Message Failed") 'End If

End Sub

Private Function MakeRequest(ByVal myMethod As String, ByVal myRequestURL As Uri, ByVal myCookieContainer As CookieContainer, Optional ByVal loginParameters As String = "") As HttpWebRequest

Dim myRequest As HttpWebRequest = DirectCast(WebRequest.Create(myRequestURL.AbsoluteUri), HttpWebRequest) myRequest.CookieContainer = myCookieContainer Select Case myMethod Case "POST" With myRequest .AllowAutoRedirect = True .Method = "POST" .ContentType = "application/x-www-form-urlencoded" .ContentLength = loginParameters.Length .GetRequestStream.Write(Encoding.UTF8.GetBytes(loginParameters), 0, loginParameters.Length) End With

End Select

Return myRequest

End Function

Private Function GetResponse(ByVal myRequest As HttpWebRequest) As HttpWebResponse Return DirectCast(myRequest.GetResponse, HttpWebResponse)

End Function

Private Function ResponseReader(ByVal myResponse As HttpWebResponse) As String Return New StreamReader(myResponse.GetResponseStream).ReadToEnd End Function

Private Function ParseForGalxValue(ByVal myresponse As String) As String 'Dim galxvalue As String = Regex.Match(fullpage, "name=""GALX""\s+value=""(.+)""").Groups(0).Value 'HAVE TO FIND BETTER REGEX FOR THIS AND RNR Dim myMatch As Match = Regex.Match(myresponse, "") If myMatch.Success Then Return myMatch.Groups(1).Value Else Throw New ApplicationException("GALX NOT FOUND. Verify that initialLogin process has been satisfied.") End If   End Function

Private Function InitialLogin(ByRef galx As String) As String Return ParseForGalxValue(ResponseReader(GetResponse(MakeRequest("GET", New Uri(initialLoginUrl), myCookieJar)))) End Function

'FOLLOWING RETURNS RNR Private Function MainLogin(ByVal galx As String) As String Dim myLoginParameters As New StringBuilder myLoginParameters.AppendFormat("Email={0}&", gvUserName) myLoginParameters.AppendFormat("Passwd={0}&", gvServicePassword) myLoginParameters.AppendFormat("GALX={0}", galx)

Return ResponseReader(GetResponse(MakeRequest("POST", New Uri(secondlogin), myCookieJar, myLoginParameters.ToString)))

End Function

Private Function GetRnrPage As String Return ResponseReader(GetResponse(MakeRequest("GET", New Uri(voiceurl), myCookieJar))) End Function

Private Sub ParseForRNR(ByVal rnrpagestring As String) 'HAVE TO FIND BETTER REGEX TO GET THESE VALUES. RIGHT NOW, THE REGEX EXPRESSION 'IS JUST LIKE A HARDCODED TEXT. 'IF GOOGLE CHANGES IT ON ITS END (FOR EXAMPLE, MOVING NAME PARAMETER AFTER TYPE IN INPUT, THE FOLLOWING REGEX BREAKS

Dim myrnr As String = String.Empty 'Dim myMatch As Match = Regex.Match(rnrpagestring, "") Dim myMatch As Match = Regex.Match(rnrpagestring, "name=""_rnr_se""\s+type=""hidden""\s+value=""(.+)""")

If myMatch.Success Then myrnr = myMatch.Groups(1).Value Session("myrnr") = myrnr Else Throw New ApplicationException("RNR NOT FOUND. Check that you have logged into Google Accounts Properly.") End If       ' Return myrnr End Sub

Public Function MakeAcall(ByVal numberToCall As String, ByVal gvforwardingnumber As String) As Boolean

Dim myCallParameters As New StringBuilder With myCallParameters .AppendFormat("outgoingNumber={0}", numberToCall) .AppendFormat("&forwardingNumber={0}", gvforwardingnumber) .AppendFormat("&subscriberNumber={0}", "undefined") .AppendFormat("&remember={0}", "0") .AppendFormat("&_rnr_se={0}", CType(Session("myrnr"), String)) .AppendFormat("&phoneType={0}", "1") End With

Dim myResponse As HttpWebResponse = GetResponse(MakeRequest("POST", New Uri(callurl), myCookieJar, myCallParameters.ToString)) If myResponse.StatusCode = HttpStatusCode.OK Then Return True Else Return False End If   End Function

Public Function SendSMS(ByVal numberToSendSMSTo As String, ByVal mymessage As String) As Boolean

Dim myCallParameters As New StringBuilder With myCallParameters .AppendFormat("id={0}", "") .AppendFormat("&phoneNumber={0}", numberToSendSMSTo) .AppendFormat("&text={0}", HttpUtility.UrlEncode(mymessage)) .AppendFormat("&_rnr_se={0}", CType(Session("myrnr"), String))

End With

Dim myResponse As HttpWebResponse = GetResponse(MakeRequest("POST", New Uri(sendsmsurl), myCookieJar, myCallParameters.ToString)) If myResponse.StatusCode = HttpStatusCode.OK Then Return True Else Return False End If   End Function

End Class

code

The above code uses a global resource file and it has the following string resources.

callurl https://www.google.com/voice/call/connect gvpassword your_google_voice_password gvusername your complete_gmail_email_address_including_@_gmail_part initialloginurl https://www.google.com/accounts/ServiceLogin secondlogin https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral sendsmsurl https://www.google.com/voice/sms/send/ voiceurl https://www.google.com/voice

You can either store these values directly in code or use a resource file to keep things clean. Following is the resource file screen shot in Visual studio. Needless to say, but this is just bare bones of how the things work posted here to illustrate the steps involved to integrate. You still need to follow basic design guidelines for the app, add error handling, etc.