Example of a Delphi/Pascal Interface for the Bulk SMS Gateway / API

For more information regarding the bulk sending API click here.

A code example for sending SMS via a Delphi Pascal language, thanks to Graham Murt for the example code.

{*******************************************************************************
*                                                                              *
*                 TextMarketer Delphi/Pascal Interface                         *
*                                                                              *
********************************************************************************
{
  example use:
  procedure TForm1.Button1Click(Sender: TObject);
  begin
    SendTextMarketerSms(username, password, sender, recipient, message);
  end;
}
unit untTextMarketer;
interface
uses Classes, IdHttp;
const
  C_HTTP_URL  = 'https://api.textmarketer.co.uk/gateway/';
type
  TTextMarketerSMS = class
  private
    FHttp: TIdHttp;
    FPassword: string;
    FUsername: string;
  public
    constructor Create; virtual;
    destructor Destroy; override;
    function SendSms(AOriginator,
                     ANumber,
                     AMessage: string;
                     AXmlResponse: Boolean): string;
    property Username: string read FUsername write FUsername;
    property Password: string read FPassword write FPassword;
  end;
  function SendTextMarketerSms(AUsername, APassword, ASender, ANumber, AMessage: string): string;
implementation
uses SysUtils;
function SendTextMarketerSms(AUsername, APassword, ASender, ANumber, AMessage: string): string;
var
  ASms: TTextMarketerSMS;
begin
  ASms := TTextMarketerSMS.Create;
  try
    ASms.Username := AUsername;
    ASms.Password := APassword;
    Result := ASms.SendSms(ASender, ANumber, AMessage, True);
  finally
    ASms.Free;
  end;
end;
{ TTextMarketerSMS }
function UrlEncode(const DecodedStr: String): String;
var
  I: Integer;
begin
  Result := '';
  if Length(DecodedStr) > 0 then
    for I := 1 to Length(DecodedStr) do
    begin
      if not (DecodedStr[I] in ['0'..'9', 'a'..'z',
                                       'A'..'Z', ' ']) then
        if DecodedStr[I] = '£' then
          Result := Result + '%C2%A3'
        else
          Result := Result + '%' + IntToHex(Ord(DecodedStr[I]), 2)
      else if not (DecodedStr[I] = ' ') then
        Result := Result + DecodedStr[I]
      else
        begin
          Result := Result + '%20'
        end;
    end;
end;
constructor TTextMarketerSMS.Create;
begin
  inherited;
  FHttp := TIdHTTP.Create(nil);
end;
destructor TTextMarketerSMS.Destroy;
begin
  FHttp.Free;
  inherited;
end;
function TTextMarketerSMS.SendSms(AOriginator, ANumber, AMessage: string;
  AXmlResponse: Boolean): string;
var
  AResult: Boolean;
  AParams: AnsiString;
  AResponse: TStringList;
begin
  AResponse := TStringList.Create;
  try
    AParams := '';
    AParams := AParams + ('?username='+FUsername);
    AParams := AParams + ('&password='+FPassword);
    AParams := AParams + ('&number='+ANumber);
    AParams := AParams + ('&orig='+UrlEncode(AOriginator));
    AParams := AParams + ('&message='+UrlEncode(AMessage));
    if AXmlResponse then AParams := AParams + ('&option=xml');
    try
      Result := FHttp.Post(C_HTTP_URL+AParams, AResponse);
    except
      Result := '';
    end;
  finally
    AResponse.Free;
  end;
end;
end.