DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoUrl
Variant vReq
Handle hoReq
String sVerificationSid
String sEnteredCode
Variant vResp
Handle hoResp
Handle hoResult
Boolean iParsed
String sStatus
String sTemp1
Integer iTemp1
Integer iTemp2
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComBasicAuth Of hoHttp To True
Set ComLogin Of hoHttp To "YOUR_API_KEY_SID"
Set ComPassword Of hoHttp To "YOUR_API_KEY_SECRET"
// Both requests use the same Verify Service SID (VA...).
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get ComAppend Of hoUrl "https://verify.twilio.com/v2/Services/" To iSuccess
Get ComAppend Of hoUrl "YOUR_VERIFY_SERVICE_SID" To iSuccess
Get ComAppend Of hoUrl "/VerificationCheck" To iSuccess
Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
If (Not(IsComObjectCreated(hoReq))) Begin
Send CreateComObject of hoReq
End
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
// Load the saved SID for this user's pending attempt. Take only the code from user input.
Move "SAVED_VERIFICATION_SID" To sVerificationSid
Move "CODE_ENTERED_BY_USER" To sEnteredCode
Send ComAddParam To hoReq "VerificationSid" sVerificationSid
Send ComAddParam To hoReq "Code" sEnteredCode
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sTemp1 vReq vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No response from Twilio. Check connectivity and TLS settings."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 404) Begin
Showln "No active verification found. Check the service and verification SIDs."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iParsed
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Twilio returned HTTP " iTemp1
Get ComJsonTypeOf Of hoResult "code" To iTemp1
If ((iParsed = True) And (iTemp1 = 2)) Begin
Get ComIntOf Of hoResult "code" To iTemp1
Showln "Twilio error code: " iTemp1
End
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "status" To iTemp1
If ((iParsed <> True) Or (iTemp1 <> 1)) Begin
Showln "Verification status missing from the response."
Procedure_Return
End
Get ComStringOf Of hoResult "status" To sStatus
If (sStatus = "approved") Begin
// Record success for the user and phone number associated with verificationSid.
Showln "Phone number verified."
End
Else Begin
If (sStatus = "pending") Begin
Showln "Code not accepted. Check the digits and enter the code again."
End
Else Begin
Showln "Phone number unverified. Verification status: " sStatus
End
End
End_Procedure