Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Clickatell One API: Upload and send a WhatsApp PDF
See more Clickatell Examples
Send a local invoice.pdf during the customer’s open WhatsApp service window. Upload the binary PDF for one recipient, then reference the returned fileId in a separate message to the same recipient. The upload and message submission have separate acceptance results.
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.BinData,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
http: THttp;
recipient: string;
pdf: TBinData;
loaded: Boolean;
upload: THttpResponse;
uploaded: Boolean;
media: TJsonObject;
decoded: Boolean;
fileId: string;
body: TJsonObject;
resp: THttpResponse;
sent: Boolean;
reply: TJsonObject;
parsed: 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');
recipient := 'PHONE_NUMBER';
pdf := TBinData.Create;
loaded := pdf.LoadFile('invoice.pdf');
if (loaded <> True) then
begin
WriteLn('Could not read invoice.pdf.');
Exit;
end;
// Restrict this customer's invoice to that customer's number.
http.SetUrlVar('recipient',recipient);
upload := THttpResponse.Create;
uploaded := http.HttpBd('POST','https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}',pdf,'application/pdf',upload);
if ((uploaded <> True) or (upload.StatusCode <> 201)) then
begin
WriteLn('PDF upload failed. HTTP status ' + upload.StatusCode);
Exit;
end;
media := TJsonObject.Create;
decoded := media.Load(upload.BodyStr);
if ((decoded <> True) or (media.BoolOf('accepted') <> True)) then
begin
WriteLn('Upload was not accepted: ' + media.StringOf('error.description'));
Exit;
end;
fileId := media.StringOf('fileId');
if ((media.JsonTypeOf('fileId') <> 1) or (fileId = '')) then
begin
WriteLn('Upload returned no file ID.');
Exit;
end;
body := TJsonObject.Create;
body.UpdateString('messages[0].channel','whatsapp');
body.UpdateString('messages[0].to',recipient);
body.UpdateString('messages[0].media.fileId',fileId);
body.UpdateString('messages[0].media.contentType','application/pdf');
body.UpdateString('messages[0].media.fileName','invoice.pdf');
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)) 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;
// HTTP acceptance and the recipient result are separate checks.
if ((parsed <> True) or (reply.JsonTypeOf('messages[0].accepted') <> 5)) then
begin
WriteLn('Missing recipient result. Check the message report.');
Exit;
end;
if (reply.BoolOf('messages[0].accepted') <> True) then
begin
WriteLn('Message rejected: ' + reply.StringOf('messages[0].error.code') + ' ' + reply.StringOf('messages[0].error.description'));
Exit;
end;
if (reply.JsonTypeOf('messages[0].apiMessageId') <> 1) then
begin
WriteLn('Accepted without a message ID. Check the message report.');
Exit;
end;
WriteLn('Accepted. Message ID: ' + reply.StringOf('messages[0].apiMessageId'));
http.Free;
pdf.Free;
upload.Free;
media.Free;
body.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.