Finally, I was able to get both Twilio and Plivo to send SMS messages. With the help of the Delphi REST Debugger and comments made in this thread, I was finally able to figure it out. Thank you! In the SendSMS function (for Plivo), instead of adding each parameter, I had to add a "Content-Type" header of "application/json" and add the parameters as RAW in the body using RESTRequest.AddBody(). One more note to make, I was originally testing this in Delphi XE5 and it would continue to give me the same error mentioned in an earlier post. When I tried it in Delphi XE7, it finally worked!!! This should work in Delphi 10 Seattle too, but haven't tested it!
Here is the working demo. On the form you need to have the following components:
Button1: TButton;
RESTClient: TRESTClient;
RESTRequest: TRESTRequest;
RESTResponse: TRESTResponse;
HTTPBasicAuthenticator: THTTPBasicAuthenticator;
ResponseMsg: TMemo;
In the OnCreate, change TypeCo from "T" or "P" depending on which company you want to test. Then run it!
Here is the code. Note, I masked out the AccountSid, AuthToken, and phone number fields for privacy.
procedure TForm1.FormCreate(Sender: TObject);
begin
// TypeCo
// = T = Twilio
// = P = Plivo
TypeCo := 'P';
if TypeCo='T' then // Twilio
begin
AccountSid := 'ACd2*************************06e38';
AuthToken := '24f63************************8ed';
BaseURL := 'https://api.twilio.com';
Resource := '/2010-04-01/Accounts/'+accountSid+'/Messages';
end
else if TypeCO='P' then // Plivo
begin
AccountSid := 'MAN*************IXYM';
AuthToken := 'ZDg0*******************************5Njhh';
BaseURL := 'https://api.plivo.com';
Resource := '/v1/Account/'+accountSid+'/Message/';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
RESTClient.ResetToDefaults;
RESTRequest.ResetToDefaults;
ResponseMsg.Clear;
RESTClient.BaseURL := BaseURL;
RESTRequest.Resource := Resource;
HTTPBasicAuthenticator.UserName := AccountSid;
HTTPBasicAuthenticator.Password := AuthToken;
RESTClient.Authenticator := HTTPBasicAuthenticator;
RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
// Here is where you can loop through your messages for different recipients.
// I use the same "To" phone number in this test
for i := 1 to 3 do
begin
if TypeCo='T' then // Twilio
SendSMS( '+1602******0','+1602******7', Format('This is test #%s using Twilio. Sent at %s',[inttostr(i),TimeToStr(Time)]))
else if TypeCo='P' then // Plivo
SendSMS( '1662******2','1602******7', Format('This is test #%s using Plivo. Sent at %s',[inttostr(i),TimeToStr(Time)]));
// Show Success or Error Message
ResponseMsg.Lines.Add(RESTResponse.Content);
ResponseMsg.Lines.Add('');
ResponseMsg.Refresh;
end;
end;
function TForm1.SendSMS(aFrom, aTo, aText: string):Boolean;
begin
result := False;
RESTRequest.Params.Clear;
RESTRequest.ClearBody;
RESTClient.BaseURL := BaseURL;
RESTRequest.Resource := Resource;
if TypeCo='T' then // Twilio
begin
RESTRequest.Params.AddUrlSegment('AccountSid', accountSid);
RESTRequest.Params.AddItem('From', aFrom);
RESTRequest.Params.AddItem('To', aTo);
RESTRequest.Params.AddItem('Body', aText);
end
else if TypeCo='P' then // Plivo
begin
// NOTE: The following Header line needs to be commented out for Delphi Seattle 10 Update 1 (or probably newer) to work. The first original Delphi 10 Seattle version would not work at all for Plivo. In this case the following error would occur: "REST request failed: Error adding header: (87) The parameter is incorrect". This was due to the HTTPBasicAuthenticator username and password character lengths adding up to greater than 56.
RESTRequest.Params.AddItem('Content-Type', 'application/json', pkHTTPHEADER, [], ctAPPLICATION_JSON);
// RESTRequest.Params.AddItem('body', '', pkRequestBody, [], ctAPPLICATION_JSON); // Thought maybe I needed this code, but works without it.
RESTRequest.AddBody(Format('{"src":"%s","dst":"%s","text":"%s"}',[aFrom,aTo,aText]),ctAPPLICATION_JSON);
end;
RESTRequest.Method := rmPOST;
RESTRequest.Execute;
result := True;
end;