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