Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
Download the media files from a Twilio MMS
See more Twilio Examples
Supply the Message SID of a received or sent MMS. List its media records, then request each media resource without the .json extension to download the file bytes. Twilio may return a temporary HTTPS download URL. The example uses a separate HTTP client for that URL so the Twilio API key stays with the API host.
Run in an empty output directory. Files are written as mms-media-1.bin, mms-media-2.bin, and so on. The code prints the media SID and MIME type alongside each filename. An empty media list produces a count of zero.
Chilkat Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
url: HCkStringBuilder;
mediaBase: HCkStringBuilder;
download: HCkHttp;
fileResp: HCkHttpResponse;
fileUrl: HCkStringBuilder;
filename: HCkStringBuilder;
mediaSid: HCkStringBuilder;
saved: Integer;
resp: HCkHttpResponse;
reply: HCkJsonObject;
more: Boolean;
parsed: Boolean;
count: Integer;
i: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putBasicAuth(http,True);
CkHttp_putLogin(http,'YOUR_API_KEY_SID');
CkHttp_putPassword(http,'YOUR_API_KEY_SECRET');
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.twilio.com/2010-04-01/Accounts/');
CkStringBuilder_Append(url,'YOUR_ACCOUNT_SID');
CkStringBuilder_Append(url,'/Messages');
CkStringBuilder_Append(url,'/');
CkStringBuilder_Append(url,'YOUR_MESSAGE_SID');
CkStringBuilder_Append(url,'/Media');
mediaBase := CkStringBuilder_Create();
CkStringBuilder_AppendSb(mediaBase,url);
CkStringBuilder_Append(url,'.json');
// Handle the API redirect explicitly so a signed download uses no Twilio credentials.
CkHttp_putFollowRedirects(http,False);
download := CkHttp_Create();
CkHttp_putRequireSslCertVerify(download,True);
fileResp := CkHttpResponse_Create();
fileUrl := CkStringBuilder_Create();
filename := CkStringBuilder_Create();
mediaSid := CkStringBuilder_Create();
saved := 0;
resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
more := True;
while (more = True) do
begin
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('No HTTP response from Twilio.');
Exit;
end;
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
Memo1.Lines.Add('Twilio returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if ((parsed = True) and (CkJsonObject_JsonTypeOf(reply,'code') = 2)) then
begin
Memo1.Lines.Add('Twilio error: ' + IntToStr(CkJsonObject_IntOf(reply,'code')) + ' ' + CkJsonObject__stringOf(reply,'message'));
end;
Exit;
end;
if (parsed <> True) then
begin
Memo1.Lines.Add('Expected a JSON response from Twilio.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(reply,'media_list') <> 4) then
begin
Memo1.Lines.Add('Expected an array of media items.');
Exit;
end;
count := CkJsonObject_SizeOfArray(reply,'media_list');
i := 0;
while (i < count) do
begin
CkJsonObject_putI(reply,i);
CkStringBuilder_SetString(mediaSid,CkJsonObject__stringOf(reply,'media_list[i].sid'));
if (CkStringBuilder_getLength(mediaSid) = 0) then
begin
Memo1.Lines.Add('Media item is missing its SID.');
Exit;
end;
CkStringBuilder_SetString(fileUrl,CkStringBuilder__getAsString(mediaBase));
CkStringBuilder_Append(fileUrl,'/');
CkStringBuilder_AppendSb(fileUrl,mediaSid);
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(fileUrl),fileResp);
if ((CkHttpResponse_getStatusCode(fileResp) >= 300) and (CkHttpResponse_getStatusCode(fileResp) < 400)) then
begin
CkStringBuilder_SetString(fileUrl,CkHttpResponse__getHeaderField(fileResp,'Location'));
if (CkStringBuilder_StartsWith(fileUrl,'https://',False) <> True) then
begin
Memo1.Lines.Add('Expected an HTTPS media download URL.');
Exit;
end;
success := CkHttp_HttpNoBody(download,'GET',CkStringBuilder__getAsString(fileUrl),fileResp);
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(fileResp) <> 200)) then
begin
Memo1.Lines.Add('Media download failed for ' + CkStringBuilder__getAsString(mediaSid) + '. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(fileResp)));
Exit;
end;
CkStringBuilder_SetString(filename,'mms-media-');
CkStringBuilder_AppendInt(filename,saved + 1);
CkStringBuilder_Append(filename,'.bin');
success := CkHttpResponse_SaveBodyBinary(fileResp,CkStringBuilder__getAsString(filename));
if (success = False) then
begin
Memo1.Lines.Add(CkHttpResponse__lastErrorText(fileResp));
Exit;
end;
Memo1.Lines.Add(CkStringBuilder__getAsString(filename) + ' ' + CkStringBuilder__getAsString(mediaSid) + ' ' + CkJsonObject__stringOf(reply,'media_list[i].content_type'));
saved := saved + 1;
i := i + 1;
end;
if (CkJsonObject_IsNullOf(reply,'next_page_uri') = True) then
begin
more := False;
end
else
begin
CkStringBuilder_SetString(url,CkJsonObject__stringOf(reply,'next_page_uri'));
if (CkStringBuilder_StartsWith(url,'/2010-04-01/Accounts/',True) <> True) then
begin
Memo1.Lines.Add('Expected a Twilio relative next-page URI.');
Exit;
end;
CkStringBuilder_Prepend(url,'https://api.twilio.com');
end;
end;
Memo1.Lines.Add('Media files saved: ' + IntToStr(saved));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkStringBuilder_Dispose(mediaBase);
CkHttp_Dispose(download);
CkHttpResponse_Dispose(fileResp);
CkStringBuilder_Dispose(fileUrl);
CkStringBuilder_Dispose(filename);
CkStringBuilder_Dispose(mediaSid);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);