Twilio Delphi sms-bericht versturen

· Functies

Vind hieronder een eenvoudig voorbeeld van hoe je een Twilio-sms-bericht verstuurt met de sgcWebSockets-bibliotheek.

Twilio

Code 

Geef gewoon de parameters mee aan de methode: telefoon waarvandaan het bericht wordt verstuurd, telefoon waar het bericht moet worden afgeleverd en de tekst van het bericht.

Stel je eigen Twilio Account Sid en Twilio Auth Token in.

uses
  sgcHTTP;
procedure SendTwilioSMS(const aFrom, aTo, aMessage: string);
var
  oHTTP: TsgcHTTP1Client;
  oParams: TStringList;
  vTwilio_Account_Sid, vTwilio_Auth_Token: string;
begin
  oHTTP := TsgcHTTP1Client.Create(nil);
  Try
    oParams := TStringList.Create;
    Try
      vTwilio_Account_Sid := 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
      vTwilio_Auth_Token := 'YYYYYYYYYYYYYYYYYYYYYYYYYYY';
      oParams.Add(Format('Body=%s', [aMessage]));
      oParams.Add(Format('From=%s', [aFrom]));
      oParams.Add(Format('To=%s', [aTo]));
      oHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
      oHTTP.Request.BasicAuthentication := True;
      oHTTP.Request.Username := vTwilio_Account_Sid;
      oHTTP.Request.Password := vTwilio_Auth_Token;
      Try
        ShowMessage(oHTTP.Post('https://api.twilio.com/2010-04-01/Accounts/' + vTwilio_Account_Sid + '/Messages.json', oParams));
      Except
        On E: Exception do
          ShowMessage(E.Message);
      End;
    Finally
      FreeAndNil(oParams);
    End;
  Finally
    FreeAndNil(oHTTP);
  End;
end;