最后,我能够让Twilio和Plivo发送短信。借助Delphi REST调试器和本主题中的注释,我终于弄清楚了。谢谢!在SendSMS函数(对于Plivo)中,我不得不添加每个参数,而是使用RESTRequest.AddBody()添加“application/json”的“Content-Type”头并在主体中添加参数为RAW。还有一点需要注意的是,我最初是在Delphi XE5中测试它,它会继续给我在之前的文章中提到的同样的错误。当我在Delphi XE7中尝试它时,它终于运行了!这应该在德尔福10西雅图工作,但还没有测试过!
这是工作演示。在表单上,你需要有以下组件:
Button1: TButton;
RESTClient: TRESTClient;
RESTRequest: TRESTRequest;
RESTResponse: TRESTResponse;
HTTPBasicAuthenticator: THTTPBasicAuthenticator;
ResponseMsg: TMemo;
在OnCreate,从“T”或“P”取决于你要测试的公司改变TypeCo。然后运行它!
这是代码。请注意,我隐瞒了AccountSid,AuthToken和电话号码字段的隐私。
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;
我不会推荐使用Plivo。他们没有妥善地为我们提供大量的短信,所以我们多次联系他们的支持,等了几个星期,没有任何事情......他们只是把我们的钱拿走了没有投递的短信,并没有采取任何措施来解决问题。 –