B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Load an API key for the Twilio account from private application configuration.
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.BasicAuth = True
http.Login = "YOUR_API_KEY_SID"
http.Password = "YOUR_API_KEY_SECRET"
Dim url As ChilkatStringBuilder
url.Initialize
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")
Dim mediaBase As ChilkatStringBuilder
mediaBase.Initialize
mediaBase.AppendSb(url)
url.Append(".json")
' Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.FollowRedirects = False
Dim download As ChilkatHttp
download.Initialize("download")
download.RequireSslCertVerify = True
Dim fileResp As ChilkatHttpResponse
fileResp.Initialize
Dim fileUrl As ChilkatStringBuilder
fileUrl.Initialize
Dim filename As ChilkatStringBuilder
filename.Initialize
Dim mediaSid As ChilkatStringBuilder
mediaSid.Initialize
Dim saved As Int = 0
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim reply As ChilkatJsonObject
reply.Initialize
Dim more As Boolean = True
Do While (more = True)
success = http.HttpNoBody("GET", url.GetAsString, resp)
If resp.StatusCode = 0 Then
Log("No HTTP response from Twilio.")
Return
End If
Dim parsed As Boolean = reply.Load(resp.BodyStr)
If (success <> True) Or (resp.StatusCode <> 200) Then
Log("Twilio returned HTTP " & resp.StatusCode)
If (parsed = True) And (reply.JsonTypeOf("code") = 2) Then
Log("Twilio error: " & reply.IntOf("code") & " " & reply.StringOf("message"))
End If
Return
End If
If parsed <> True Then
Log("Expected a JSON response from Twilio.")
Return
End If
If reply.JsonTypeOf("media_list") <> 4 Then
Log("Expected an array of media items.")
Return
End If
Dim count As Int = reply.SizeOfArray("media_list")
Dim i As Int = 0
Do While (i < count)
reply.I = i
mediaSid.SetString(reply.StringOf("media_list[i].sid"))
If mediaSid.Length = 0 Then
Log("Media item is missing its SID.")
Return
End If
fileUrl.SetString(mediaBase.GetAsString)
fileUrl.Append("/")
fileUrl.AppendSb(mediaSid)
success = http.HttpNoBody("GET", fileUrl.GetAsString, fileResp)
If (fileResp.StatusCode >= 300) And (fileResp.StatusCode < 400) Then
fileUrl.SetString(fileResp.GetHeaderField("Location"))
If fileUrl.StartsWith("https://", False) <> True Then
Log("Expected an HTTPS media download URL.")
Return
End If
success = download.HttpNoBody("GET", fileUrl.GetAsString, fileResp)
End If
If (success <> True) Or (fileResp.StatusCode <> 200) Then
Log("Media download failed for " & mediaSid.GetAsString & ". HTTP " & fileResp.StatusCode)
Return
End If
filename.SetString("mms-media-")
filename.AppendInt(saved + 1)
filename.Append(".bin")
success = fileResp.SaveBodyBinary(filename.GetAsString)
If success = False Then
Log(fileResp.LastErrorText)
Return
End If
Log(filename.GetAsString & " " & mediaSid.GetAsString & " " & reply.StringOf("media_list[i].content_type"))
saved = saved + 1
i = i + 1
Loop
If reply.IsNullOf("next_page_uri") = True Then
more = False
Else
url.SetString(reply.StringOf("next_page_uri"))
If url.StartsWith("/2010-04-01/Accounts/", True) <> True Then
Log("Expected a Twilio relative next-page URI.")
Return
End If
url.Prepend("https://api.twilio.com")
End If
Loop
Log("Media files saved: " & saved)