Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oUrl
LOCAL oReq
LOCAL cVerificationSid
LOCAL cEnteredCode
LOCAL oResp
LOCAL oResult
LOCAL nParsed
LOCAL cStatus
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:BasicAuth := 1
oHttp:Login := "YOUR_API_KEY_SID"
oHttp:Password := "YOUR_API_KEY_SECRET"
// Both requests use the same Verify Service SID (VA...).
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://verify.twilio.com/v2/Services/")
oUrl:Append("YOUR_VERIFY_SERVICE_SID")
oUrl:Append("/VerificationCheck")
oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "POST"
oReq:ContentType := "application/x-www-form-urlencoded"
// Load the saved SID for this user's pending attempt. Take only the code from user input.
cVerificationSid := "SAVED_VERIFICATION_SID"
cEnteredCode := "CODE_ENTERED_BY_USER"
oReq:AddParam("VerificationSid", cVerificationSid)
oReq:AddParam("Code", cEnteredCode)
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq(oUrl:GetAsString(), oReq, oResp)
IF (oResp:StatusCode == 0)
? "No response from Twilio. Check connectivity and TLS settings."
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
RETURN
ENDIF
IF (oResp:StatusCode == 404)
? "No active verification found. Check the service and verification SIDs."
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
RETURN
ENDIF
oResult := CreateObject("Chilkat.JsonObject")
nParsed := oResult:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. (oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
? "Twilio returned HTTP " + Str(oResp:StatusCode)
IF ((nParsed == 1) .AND. (oResult:JsonTypeOf("code") == 2))
? "Twilio error code: " + Str(oResult:IntOf("code"))
ENDIF
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()
RETURN
ENDIF
IF ((nParsed != 1) .OR. (oResult:JsonTypeOf("status") != 1))
? "Verification status missing from the response."
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()
RETURN
ENDIF
cStatus := oResult:StringOf("status")
IF (cStatus == "approved")
// Record success for the user and phone number associated with verificationSid.
? "Phone number verified."
ELSE
IF (cStatus == "pending")
? "Code not accepted. Check the digits and enter the code again."
ELSE
? "Phone number unverified. Verification status: " + cStatus
ENDIF
ENDIF
oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oResult:destroy()