PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
Check an SMS verification code with Twilio Verify
See more Twilio Examples
After the user enters the SMS code, retrieve the VE verification SID saved when you started the verification. Send that SID and the entered code to the same Verify Service. Keep the code as text so a leading zero survives.
Mark the phone number on that pending attempt as verified only when Twilio returns status=approved. A pending check leaves the number unverified. Twilio can return HTTP 404 after expiration, approval, or exhaustion of check attempts. Keep a successful result in your application so it can be used after Twilio removes the verification.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Url
oleobject loo_Req
string ls_VerificationSid
string ls_EnteredCode
oleobject loo_Resp
oleobject loo_Result
long li_Parsed
string ls_Status
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
loo_Http.Login = "YOUR_API_KEY_SID"
loo_Http.Password = "YOUR_API_KEY_SECRET"
// Both requests use the same Verify Service SID (VA...).
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Url.Append("https://verify.twilio.com/v2/Services/")
loo_Url.Append("YOUR_VERIFY_SERVICE_SID")
loo_Url.Append("/VerificationCheck")
loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")
loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
// Load the saved SID for this user's pending attempt. Take only the code from user input.
ls_VerificationSid = "SAVED_VERIFICATION_SID"
ls_EnteredCode = "CODE_ENTERED_BY_USER"
loo_Req.AddParam("VerificationSid",ls_VerificationSid)
loo_Req.AddParam("Code",ls_EnteredCode)
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpReq(loo_Url.GetAsString(),loo_Req,loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No response from Twilio. Check connectivity and TLS settings."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
return
end if
if loo_Resp.StatusCode = 404 then
Write-Debug "No active verification found. Check the service and verification SIDs."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
return
end if
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
li_Parsed = loo_Result.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Twilio returned HTTP " + string(loo_Resp.StatusCode)
if (li_Parsed = 1) AND (loo_Result.JsonTypeOf("code") = 2) then
Write-Debug "Twilio error code: " + string(loo_Result.IntOf("code"))
end if
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
destroy loo_Result
return
end if
if (li_Parsed <> 1) OR (loo_Result.JsonTypeOf("status") <> 1) then
Write-Debug "Verification status missing from the response."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
destroy loo_Result
return
end if
ls_Status = loo_Result.StringOf("status")
if ls_Status = "approved" then
// Record success for the user and phone number associated with verificationSid.
Write-Debug "Phone number verified."
else
if ls_Status = "pending" then
Write-Debug "Code not accepted. Check the digits and enter the code again."
else
Write-Debug "Phone number unverified. Verification status: " + ls_Status
end if
end if
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
destroy loo_Result