Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

WaTrend Send WhatsApp Text

See more WaTrend Examples

Send a WhatsApp text.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.HttpRequest,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  req: THttpRequest;
  sbUrl: TStringBuilder;
  responseBodyStr: string;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  respStatusCode: Integer;
  jResp: TJsonObject;
  status: string;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  //  Use your actual access token instead of 555555555555555555555555555555
  req := THttpRequest.Create;
  req.AddParam('number','84933313xxx');
  req.AddParam('type','text');
  req.AddParam('message','This is a test message');
  req.AddParam('instance_id','609ACF283XXXX');
  req.AddParam('access_token','555555555555555555555555555555');

  //  Note: The WaTrend online documentation indicate a POST should be used.
  //  However, it seems you might actually need to send a GET request.
  //  It is unclear.  
  //  If a GET is neeed, you would just send to the URL w/ query params like this:
  sbUrl := TStringBuilder.Create;
  sbUrl.Append('https://app.watrend.com/api/send.php?');
  sbUrl.Append(req.GetUrlEncodedParams());
  responseBodyStr := http.QuickGetStr(sbUrl.GetAsString());
  //  The responseBodyStr contains the JSON response from the server..

  req.HttpVerb := 'POST';
  req.ContentType := 'application/x-www-form-urlencoded';

  resp := THttpResponse.Create;
  success := http.HttpReq('https://app.watrend.com/api/send.php',req,resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;

  WriteLn(resp.BodyStr);

  //  Both success and failed responses use 200 status code.

  //  A success response contains this JSON in the response body:
  //  {"status":"success", ... }

  //  A failed response will contain something like this:
  //  {"status":"error","message":"License Invalidated"}

  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);

  status := jResp.StringOf('status');
  WriteLn('status: ' + status);


  http.Free;
  req.Free;
  sbUrl.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.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.