Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Clickatell One API: Send personalized SMS reminders in a batch

See more Clickatell Examples

Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.

Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.

Clickatell API reference

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  http: THttp;
  firstPhone: string;
  secondPhone: string;
  body: TJsonObject;
  resp: THttpResponse;
  sent: Boolean;
  reply: TJsonObject;
  parsed: Boolean;
  results: TJsonObject;
  i: Integer;
  appointment: string;
  phone: string;
  result: TJsonObject;
  found: Boolean;
  saved: Boolean;

begin
  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  //  Load the key for this One API integration from your application configuration.
  http.SetRequestHeader('Authorization','ONE_API_KEY');
  http.SetRequestHeader('Accept','application/json');

  firstPhone := 'FIRST_PHONE_NUMBER';
  secondPhone := 'SECOND_PHONE_NUMBER';
  body := TJsonObject.Create;
  body.UpdateString('messages[0].channel','sms');
  body.UpdateString('messages[0].to',firstPhone);
  body.UpdateString('messages[0].content','Your appointment with Lee is at 10:00.');
  body.UpdateString('messages[0].clientMessageId','appt-1042');
  body.UpdateString('messages[1].channel','sms');
  body.UpdateString('messages[1].to',secondPhone);
  body.UpdateString('messages[1].content','Your appointment with Sam is at 14:30.');
  body.UpdateString('messages[1].clientMessageId','appt-1043');

  resp := THttpResponse.Create;
  sent := http.HttpJson('POST','https://platform.clickatell.com/v1/message',body,'application/json',resp);
  if (resp.StatusCode = 0) then
    begin
      WriteLn('No HTTP response. Check the message report before resending.');
      Exit;
    end;
  reply := TJsonObject.Create;
  parsed := reply.Load(resp.BodyStr);
  if ((sent <> True) or ((resp.StatusCode <> 202) and (resp.StatusCode <> 207))) then
    begin
      WriteLn('Send returned HTTP ' + resp.StatusCode);
      if (reply.HasMember('error.description') = True) then
        begin
          WriteLn(reply.StringOf('error.description'));
        end;
      Exit;
    end;
  if (reply.HasMember('error.code') = True) then
    begin
      if (reply.IsNullOf('error.code') <> True) then
        begin
          WriteLn('Submission error ' + reply.StringOf('error.code') + '  ' + reply.StringOf('error.description'));
          Exit;
        end;
    end;

  //  Look up each submitted appointment in the reply using its unique recipient.
  //  This works even if the result order changes or clientMessageId is absent.
  results := TJsonObject.Create;
  i := 0;
  while (i < 2) do
    begin
      body.I := i;
      appointment := body.StringOf('messages[i].clientMessageId');
      phone := body.StringOf('messages[i].to');
      result := TJsonObject.Create;
      found := reply.FindRecord2('messages','to',phone,True,result);
      results.I := i;
      results.UpdateString('appointments[i].clientMessageId',appointment);
      if ((found <> True) or (result.JsonTypeOf('accepted') <> 5)) then
        begin
          results.UpdateString('appointments[i].outcome','unknown');
          WriteLn(appointment + ' has no confirmed result.');
        end
      else
        begin
          if (result.BoolOf('accepted') = True) then
            begin
              results.UpdateString('appointments[i].outcome','accepted');
              if (result.JsonTypeOf('apiMessageId') = 1) then
                begin
                  results.UpdateString('appointments[i].apiMessageId',result.StringOf('apiMessageId'));
                end;
              WriteLn(appointment + ' accepted.');
            end
          else
            begin
              results.UpdateString('appointments[i].outcome','rejected');
              results.UpdateString('appointments[i].reason',result.StringOf('error.description'));
              WriteLn(appointment + ' rejected: ' + result.StringOf('error.description'));
            end;
        end;
      i := i + 1;
    end;

  //  This demo writes a snapshot. In an application, update the two appointment records.
  saved := results.WriteFile('appointment-send-results.json');
  if (saved <> True) then
    begin
      WriteLn('Could not save the results. Reconcile the submitted messages before retrying.');
      Exit;
    end;
  WriteLn('Saved appointment-send-results.json');


  http.Free;
  body.Free;
  resp.Free;
  reply.Free;
  results.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.