最後,我能夠讓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。他們沒有妥善地爲我們提供大量的短信,所以我們多次聯繫他們的支持,等了幾個星期,沒有任何事情......他們只是把我們的錢拿走了沒有投遞的短信,並沒有採取任何措施來解決問題。 –