Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
local success = false
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Load an API key for the Twilio account from private application configuration.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.basic_auth = true
http.login = "YOUR_API_KEY_SID"
http.password = "YOUR_API_KEY_SECRET"
local url = chilkat.StringBuilder.new()
url:append("https://api.twilio.com/2010-04-01/Accounts/")
url:append("YOUR_ACCOUNT_SID")
url:append("/Messages")
url:append("/")
url:append("YOUR_MESSAGE_SID")
url:append("/Media")
local mediaBase = chilkat.StringBuilder.new()
mediaBase:append_sb(url)
url:append(".json")
-- Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.follow_redirects = false
local download = chilkat.Http.new()
download.require_ssl_cert_verify = true
local fileResp = chilkat.HttpResponse.new()
local fileUrl = chilkat.StringBuilder.new()
local filename = chilkat.StringBuilder.new()
local mediaSid = chilkat.StringBuilder.new()
local saved = 0
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local more = true
while more do
success = http:http_no_body("GET", url:get_as_string(), resp)
if resp.status_code == 0 then
print("No HTTP response from Twilio.")
return
end
local parsed = reply:load(resp.body_str)
if (not success) or (resp.status_code ~= 200) then
print("Twilio returned HTTP " .. resp.status_code)
if (parsed) and (reply:json_type_of("code") == 2) then
print("Twilio error: " .. reply:int_of("code") .. " " .. reply:string_of("message"))
end
return
end
if not parsed then
print("Expected a JSON response from Twilio.")
return
end
if reply:json_type_of("media_list") ~= 4 then
print("Expected an array of media items.")
return
end
local count = reply:size_of_array("media_list")
local i = 0
while i < count do
reply.i = i
mediaSid:set_string(reply:string_of("media_list[i].sid"))
if mediaSid.length == 0 then
print("Media item is missing its SID.")
return
end
fileUrl:set_string(mediaBase:get_as_string())
fileUrl:append("/")
fileUrl:append_sb(mediaSid)
success = http:http_no_body("GET", fileUrl:get_as_string(), fileResp)
if (fileResp.status_code >= 300) and (fileResp.status_code < 400) then
fileUrl:set_string(fileResp:get_header_field("Location"))
if not fileUrl:starts_with("https://", false) then
print("Expected an HTTPS media download URL.")
return
end
success = download:http_no_body("GET", fileUrl:get_as_string(), fileResp)
end
if (not success) or (fileResp.status_code ~= 200) then
print("Media download failed for " .. mediaSid:get_as_string() .. ". HTTP " .. fileResp.status_code)
return
end
filename:set_string("mms-media-")
filename:append_int(saved + 1)
filename:append(".bin")
success = fileResp:save_body_binary(filename:get_as_string())
if not success then
print(fileResp.last_error_text)
return
end
print(filename:get_as_string() .. " " .. mediaSid:get_as_string() .. " " .. reply:string_of("media_list[i].content_type"))
saved = saved + 1
i = i + 1
end
if reply:is_null_of("next_page_uri") then
more = false
else
url:set_string(reply:string_of("next_page_uri"))
if not url:starts_with("/2010-04-01/Accounts/", true) then
print("Expected a Twilio relative next-page URI.")
return
end
url:prepend("https://api.twilio.com")
end
end
print("Media files saved: " .. saved)