SQL Server Requires Chilkat v11.0.0+
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Obtain these credentials from the application's private configuration.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'BasicAuth', 1
EXEC sp_OASetProperty @http, 'Login', 'YOUR_API_KEY_SID'
EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_KEY_SECRET'
-- Both requests use the same Verify Service SID (VA...).
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://verify.twilio.com/v2/Services/'
EXEC sp_OAMethod @url, 'Append', @success OUT, 'YOUR_VERIFY_SERVICE_SID'
EXEC sp_OAMethod @url, 'Append', @success OUT, '/VerificationCheck'
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
-- Load the saved SID for this user's pending attempt. Take only the code from user input.
DECLARE @verificationSid nvarchar(4000)
SELECT @verificationSid = 'SAVED_VERIFICATION_SID'
DECLARE @enteredCode nvarchar(4000)
SELECT @enteredCode = 'CODE_ENTERED_BY_USER'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'VerificationSid', @verificationSid
EXEC sp_OAMethod @req, 'AddParam', NULL, 'Code', @enteredCode
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No response from Twilio. Check connectivity and TLS settings.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 404
BEGIN
PRINT 'No active verification found. Check the service and verification SIDs.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
DECLARE @parsed int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Twilio returned HTTP ' + @iTmp0
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'code'
IF (@parsed = 1) and (@iTmp0 = 2)
BEGIN
EXEC sp_OAMethod @result, 'IntOf', @iTmp0 OUT, 'code'
PRINT 'Twilio error code: ' + @iTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'status'
IF (@parsed <> 1) or (@iTmp0 <> 1)
BEGIN
PRINT 'Verification status missing from the response.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @status nvarchar(4000)
EXEC sp_OAMethod @result, 'StringOf', @status OUT, 'status'
IF @status = 'approved'
BEGIN
-- Record success for the user and phone number associated with verificationSid.
PRINT 'Phone number verified.'
END
ELSE
BEGIN
IF @status = 'pending'
BEGIN
PRINT 'Code not accepted. Check the digits and enter the code again.'
END
ELSE
BEGIN
PRINT 'Phone number unverified. Verification status: ' + @status
END
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
END
GO