Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Schedule or cancel an SMS with Twilio
See more Twilio Examples
Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.
Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.
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.CkDateTime,
Chilkat.HttpResponse,
Chilkat.HttpRequest,
Chilkat.JsonObject,
Chilkat.StringBuilder,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
url: TStringBuilder;
cancelMessage: Boolean;
req: THttpRequest;
sendTime: TCkDateTime;
expectedHttp: Integer;
expectedStatus: string;
timeOk: Boolean;
resp: THttpResponse;
reply: TJsonObject;
parsed: Boolean;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
http := THttp.Create;
http.RequireSslCertVerify := True;
http.BasicAuth := True;
http.Login := 'YOUR_API_KEY_SID';
http.Password := 'YOUR_API_KEY_SECRET';
url := TStringBuilder.Create;
url.Append('https://api.twilio.com/2010-04-01/Accounts/');
url.Append('YOUR_ACCOUNT_SID');
url.Append('/Messages');
// Set this to true on a later run to cancel the saved message.
cancelMessage := False;
req := THttpRequest.Create;
sendTime := TCkDateTime.Create;
req.HttpVerb := 'POST';
req.ContentType := 'application/x-www-form-urlencoded';
expectedHttp := 201;
expectedStatus := 'scheduled';
if (cancelMessage <> True) then
begin
timeOk := sendTime.SetFromCurrentSystemTime();
if (timeOk <> True) then
begin
WriteLn(sendTime.LastErrorText);
Exit;
end;
timeOk := sendTime.AddSeconds(3600);
if (timeOk <> True) then
begin
WriteLn(sendTime.LastErrorText);
Exit;
end;
req.AddParam('MessagingServiceSid','YOUR_MESSAGING_SERVICE_SID');
req.AddParam('To','RECIPIENT_E164_NUMBER');
req.AddParam('Body','Scheduled message.');
req.AddParam('ScheduleType','fixed');
req.AddParam('SendAt',sendTime.GetAsTimestamp(False));
end
else
begin
// Update the individual message. This branch never creates another message.
url.Append('/');
url.Append('SAVED_SCHEDULED_MESSAGE_SID');
req.AddParam('Status','canceled');
expectedHttp := 200;
expectedStatus := 'canceled';
end;
url.Append('.json');
resp := THttpResponse.Create;
reply := TJsonObject.Create;
success := http.HttpReq(url.GetAsString(),req,resp);
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response from Twilio.');
Exit;
end;
parsed := reply.Load(resp.BodyStr);
if ((success <> True) or (resp.StatusCode <> expectedHttp)) then
begin
WriteLn('Twilio returned HTTP ' + resp.StatusCode);
if ((parsed = True) and (reply.JsonTypeOf('code') = 2)) then
begin
WriteLn('Twilio error: ' + reply.IntOf('code') + ' ' + reply.StringOf('message'));
end;
Exit;
end;
if (parsed <> True) then
begin
WriteLn('Expected a JSON response from Twilio.');
Exit;
end;
if ((reply.JsonTypeOf('sid') <> 1) or (reply.JsonTypeOf('status') <> 1)) then
begin
WriteLn('The response is missing the message SID or status.');
Exit;
end;
WriteLn('Message SID: ' + reply.StringOf('sid'));
WriteLn('Current status: ' + reply.StringOf('status'));
if (reply.StringOfEquals('status',expectedStatus,True) <> True) then
begin
WriteLn('The requested state was not confirmed. Check this SID before trying again.');
Exit;
end;
if (cancelMessage <> True) then
begin
WriteLn('Save the Message SID to check or cancel the scheduled message.');
end;
http.Free;
url.Free;
req.Free;
sendTime.Free;
resp.Free;
reply.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.