Python Requires Chilkat v11.0.0+
Python
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 Python Downloads
import sys
import chilkat2
success = False
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Load an API key for the Twilio account from private application configuration.
http = chilkat2.Http()
http.RequireSslCertVerify = True
http.BasicAuth = True
http.Login = "YOUR_API_KEY_SID"
http.Password = "YOUR_API_KEY_SECRET"
url = chilkat2.StringBuilder()
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")
mediaBase = chilkat2.StringBuilder()
mediaBase.AppendSb(url)
url.Append(".json")
# Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.FollowRedirects = False
download = chilkat2.Http()
download.RequireSslCertVerify = True
fileResp = chilkat2.HttpResponse()
fileUrl = chilkat2.StringBuilder()
filename = chilkat2.StringBuilder()
mediaSid = chilkat2.StringBuilder()
saved = 0
resp = chilkat2.HttpResponse()
reply = chilkat2.JsonObject()
more = True
while (more == True) :
success = http.HttpNoBody("GET",url.GetAsString(),resp)
if (resp.StatusCode == 0):
print("No HTTP response from Twilio.")
sys.exit()
parsed = reply.Load(resp.BodyStr)
if ((success != True) or (resp.StatusCode != 200)):
print("Twilio returned HTTP " + str(resp.StatusCode))
if ((parsed == True) and (reply.JsonTypeOf("code") == 2)):
print("Twilio error: " + str(reply.IntOf("code")) + " " + reply.StringOf("message"))
sys.exit()
if (parsed != True):
print("Expected a JSON response from Twilio.")
sys.exit()
if (reply.JsonTypeOf("media_list") != 4):
print("Expected an array of media items.")
sys.exit()
count = reply.SizeOfArray("media_list")
i = 0
while (i < count) :
reply.I = i
mediaSid.SetString(reply.StringOf("media_list[i].sid"))
if (mediaSid.Length == 0):
print("Media item is missing its SID.")
sys.exit()
fileUrl.SetString(mediaBase.GetAsString())
fileUrl.Append("/")
fileUrl.AppendSb(mediaSid)
success = http.HttpNoBody("GET",fileUrl.GetAsString(),fileResp)
if ((fileResp.StatusCode >= 300) and (fileResp.StatusCode < 400)):
fileUrl.SetString(fileResp.GetHeaderField("Location"))
if (fileUrl.StartsWith("https://",False) != True):
print("Expected an HTTPS media download URL.")
sys.exit()
success = download.HttpNoBody("GET",fileUrl.GetAsString(),fileResp)
if ((success != True) or (fileResp.StatusCode != 200)):
print("Media download failed for " + mediaSid.GetAsString() + ". HTTP " + str(fileResp.StatusCode))
sys.exit()
filename.SetString("mms-media-")
filename.AppendInt(saved + 1)
filename.Append(".bin")
success = fileResp.SaveBodyBinary(filename.GetAsString())
if (success == False):
print(fileResp.LastErrorText)
sys.exit()
print(filename.GetAsString() + " " + mediaSid.GetAsString() + " " + reply.StringOf("media_list[i].content_type"))
saved = saved + 1
i = i + 1
if (reply.IsNullOf("next_page_uri") == True):
more = False
else:
url.SetString(reply.StringOf("next_page_uri"))
if (url.StartsWith("/2010-04-01/Accounts/",True) != True):
print("Expected a Twilio relative next-page URI.")
sys.exit()
url.Prepend("https://api.twilio.com")
print("Media files saved: " + str(saved))