Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.StringBuilder,
Chilkat.HttpResponse,
Chilkat.HttpRequest,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
url: TStringBuilder;
req: THttpRequest;
verificationSid: string;
enteredCode: string;
resp: THttpResponse;
result: TJsonObject;
parsed: Boolean;
status: string;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
http := THttp.Create;
http.RequireSslCertVerify := True;
http.BasicAuth := True;
http.Login := 'YOUR_API_KEY_SID';
http.Password := 'YOUR_API_KEY_SECRET';
// Both requests use the same Verify Service SID (VA...).
url := TStringBuilder.Create;
url.Append('https://verify.twilio.com/v2/Services/');
url.Append('YOUR_VERIFY_SERVICE_SID');
url.Append('/VerificationCheck');
req := THttpRequest.Create;
req.HttpVerb := 'POST';
req.ContentType := 'application/x-www-form-urlencoded';
// Load the saved SID for this user's pending attempt. Take only the code from user input.
verificationSid := 'SAVED_VERIFICATION_SID';
enteredCode := 'CODE_ENTERED_BY_USER';
req.AddParam('VerificationSid',verificationSid);
req.AddParam('Code',enteredCode);
resp := THttpResponse.Create;
success := http.HttpReq(url.GetAsString(),req,resp);
if (resp.StatusCode = 0) then
begin
WriteLn('No response from Twilio. Check connectivity and TLS settings.');
Exit;
end;
if (resp.StatusCode = 404) then
begin
WriteLn('No active verification found. Check the service and verification SIDs.');
Exit;
end;
result := TJsonObject.Create;
parsed := result.Load(resp.BodyStr);
if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
WriteLn('Twilio returned HTTP ' + resp.StatusCode);
if ((parsed = True) and (result.JsonTypeOf('code') = 2)) then
begin
WriteLn('Twilio error code: ' + result.IntOf('code'));
end;
Exit;
end;
if ((parsed <> True) or (result.JsonTypeOf('status') <> 1)) then
begin
WriteLn('Verification status missing from the response.');
Exit;
end;
status := result.StringOf('status');
if (status = 'approved') then
begin
// Record success for the user and phone number associated with verificationSid.
WriteLn('Phone number verified.');
end
else
begin
if (status = 'pending') then
begin
WriteLn('Code not accepted. Check the digits and enter the code again.');
end
else
begin
WriteLn('Phone number unverified. Verification status: ' + status);
end;
end;
http.Free;
url.Free;
req.Free;
resp.Free;
result.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.