Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
List Twilio message history
See more Twilio Examples
List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.
Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
accountSid: WideString;
url: TChilkatStringBuilder;
resp: TChilkatHttpResponse;
reply: TChilkatJsonObject;
total: Integer;
more: Integer;
parsed: Integer;
count: Integer;
i: Integer;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.BasicAuth := 1;
// Load the owning Account SID and an API key from application configuration.
accountSid := 'YOUR_ACCOUNT_SID';
http.Login := 'YOUR_API_KEY_SID';
http.Password := 'YOUR_API_KEY_SECRET';
url := TChilkatStringBuilder.Create(Self);
url.Append('https://api.twilio.com/2010-04-01/Accounts/');
url.Append(accountSid);
url.Append('/Messages.json');
url.Append('?PageSize=50');
resp := TChilkatHttpResponse.Create(Self);
reply := TChilkatJsonObject.Create(Self);
total := 0;
more := 1;
while (more = 1) do
begin
success := http.HttpNoBody('GET',url.GetAsString(),resp.ControlInterface);
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
parsed := reply.Load(resp.BodyStr);
if ((success <> 1) or (resp.StatusCode <> 200)) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode));
if ((parsed = 1) and (reply.HasMember('code') = 1)) then
begin
Memo1.Lines.Add('Twilio error ' + IntToStr(reply.IntOf('code')) + ': ' + reply.StringOf('message'));
end;
Exit;
end;
if (parsed <> 1) then
begin
Memo1.Lines.Add('Expected a JSON response.');
Exit;
end;
if (reply.JsonTypeOf('messages') <> 4) then
begin
Memo1.Lines.Add('Expected a messages array.');
Exit;
end;
count := reply.SizeOfArray('messages');
i := 0;
while (i < count) do
begin
reply.I := i;
Memo1.Lines.Add(reply.StringOf('messages[i].sid') + ' ' + reply.StringOf('messages[i].direction') + ' ' + reply.StringOf('messages[i].status'));
i := i + 1;
end;
total := total + count;
// Twilio supplies a relative URI with the cursor and returns null on the last page.
if (reply.IsNullOf('next_page_uri') = 1) then
begin
more := 0;
end
else
begin
if (reply.JsonTypeOf('next_page_uri') <> 1) then
begin
Memo1.Lines.Add('Expected a next-page URI or null.');
Exit;
end;
url.SetString('https://api.twilio.com');
url.Append(reply.StringOf('next_page_uri'));
end;
end;
Memo1.Lines.Add('Messages found: ' + IntToStr(total));