Tcl Requires Chilkat v11.0.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Obtain these credentials from the application's private configuration.
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_BasicAuth $http 1
CkHttp_put_Login $http "YOUR_API_KEY_SID"
CkHttp_put_Password $http "YOUR_API_KEY_SECRET"
# Both requests use the same Verify Service SID (VA...).
set url [new_CkStringBuilder]
CkStringBuilder_Append $url "https://verify.twilio.com/v2/Services/"
CkStringBuilder_Append $url "YOUR_VERIFY_SERVICE_SID"
CkStringBuilder_Append $url "/VerificationCheck"
set req [new_CkHttpRequest]
CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
# Load the saved SID for this user's pending attempt. Take only the code from user input.
set verificationSid "SAVED_VERIFICATION_SID"
set enteredCode "CODE_ENTERED_BY_USER"
CkHttpRequest_AddParam $req "VerificationSid" $verificationSid
CkHttpRequest_AddParam $req "Code" $enteredCode
set resp [new_CkHttpResponse]
set success [CkHttp_HttpReq $http [CkStringBuilder_getAsString $url] $req $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts "No response from Twilio. Check connectivity and TLS settings."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
exit
}
if {[CkHttpResponse_get_StatusCode $resp] == 404} then {
puts "No active verification found. Check the service and verification SIDs."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
exit
}
set result [new_CkJsonObject]
set parsed [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {expr [$success != 1] || [expr [[CkHttpResponse_get_StatusCode $resp] < 200] || [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
puts "Twilio returned HTTP [CkHttpResponse_get_StatusCode $resp]"
if {expr [$parsed == 1] && [[CkJsonObject_JsonTypeOf $result "code"] == 2]} then {
puts "Twilio error code: [CkJsonObject_IntOf $result code]"
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $result
exit
}
if {expr [$parsed != 1] || [[CkJsonObject_JsonTypeOf $result "status"] != 1]} then {
puts "Verification status missing from the response."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $result
exit
}
set status [CkJsonObject_stringOf $result "status"]
if {$status == "approved"} then {
# Record success for the user and phone number associated with verificationSid.
puts "Phone number verified."
} else {
if {$status == "pending"} then {
puts "Code not accepted. Check the digits and enter the code again."
} else {
puts "Phone number unverified. Verification status: $status"
}
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $result