You know how to receive and reply to incoming SMS messages. What if you receive an MMS message containing an image you'd like to download? Let's learn how we can grab that image and any other incoming MMS media using Java.
When Twilio receives a message for your phone number, it can make an HTTP call to a webhook that you create. The easiest way to handle HTTP requests in Java is to use Spark web framework.
Twilio expects, at the very least, for your webhook to return a 200 OK
response if everything is peachy. Often, however, you will return some TwiML in your response as well. TwiML is just a set of XML commands telling Twilio how you'd like it to respond to your message. Rather than manually generating the XML, we'll use the twilio
helper library that can make generating the TwiML and the rest of the webhook plumbing easy peasy.
When Twilio calls your webhook, it sends a number of parameters about the message you just received.
Most of these, such as the To phone number, the From phone number, and the Body of the message are available as properties of the request parameter to the Spark views.
We may receive more than one media per message, this parameter informs us how many we received. We used a custom class parseBody
to get the value and cast it to an Integer, to be used in a following loop.
_10Map<String, String> parameters = parseBody(req.body());_10String numMediaStr = parameters.get("NumMedia");_10int numMedia = Integer.parseInt(numMediaStr);
Since an MMS message can have multiple attachments, Twilio will send us form variables named MediaUrlX
, where X is a zero-based index. So, for example, the URL for the first media attachment will be in the MediaUrl0
parameter, the second in MediaUrl1
, and so on.
In order to handle a dynamic number of attachments, we loop through all the available URLs:
_10while (numMedia > 0) {_10 numMedia = numMedia - 1;_10 String mediaUrl = parameters.get(String.format("MediaUrl%d", numMedia));_10}
Attachments to MMS messages can be of many different file types. JPG and GIF images as well as MP4 and 3GP files are all common. Twilio handles the determination of the file type for you and you can get the standard mime type from the MediaContentTypeX
parameter. If you are expecting photos, then you will likely see a lot of attachments with the mime type of image/jpeg
.
_10while (numMedia > 0) {_10 numMedia = numMedia - 1;_10 String mediaUrl = parameters.get(String.format("MediaUrl%d", numMedia));_10 String contentType = parameters.get(String.format("MediaContentType%d", numMedia));_10}
Depending on your use case, storing the URLs to the images (or videos or whatever) may be all you need. There's two key features to these URLs that make them very pliable for your use in your apps:
For example, if you are building a browser-based app that needs to display the images, all you need to do is drop an <img src="twilio url to your image">
tag into the page. If this works for you, then perhaps all you need is to store the URL in a database character field.
If you want to save the media attachments to a file, then you will need to make an HTTP request to the media URL and write the response stream to a file. If you need a unique filename, you can use the last part of the media URL. For example, suppose your media URL is the following:
_10https://api.twilio.com/2010-04-01/Accounts/ACxxxx/Messages/MMxxxx/Media/ME27be8a708784242c0daee207ff73db67
You can use that last part of the URL as a unique filename. Figuring out a good extension to use is a little tricker. If you are only expecting images, you could just assume a ".jpg" extension. For a little more flexibility, you can lookup the mime type and determine a good extension to use based on that.
Here's the complete code for our controller that saves each MMS attachment to the App_Data folder:
_96package com.twilio.app;_96_96import static spark.Spark.*;_96_96import java.util.Map;_96import java.util.HashMap;_96import java.io.File;_96import java.io.InputStream;_96import java.io.UnsupportedEncodingException;_96import java.net.URL;_96import java.net.URLDecoder;_96_96import org.apache.tika.mime.MimeType;_96import org.apache.tika.mime.MimeTypes;_96_96import org.apache.commons.io.FileUtils;_96import org.apache.http.HttpResponse;_96import org.apache.http.client.methods.HttpGet;_96import org.apache.http.impl.client.CloseableHttpClient;_96import org.apache.http.impl.client.HttpClients;_96import org.apache.http.impl.client.LaxRedirectStrategy;_96_96import com.twilio.twiml.MessagingResponse;_96import com.twilio.twiml.messaging.Body;_96import com.twilio.twiml.messaging.Message;_96_96public class App {_96 public static void main(String[] args) {_96 post("/sms", (req, res) -> {_96 Map<String, String> parameters = parseBody(req.body());_96 String numMediaStr = parameters.get("NumMedia");_96 int numMedia = Integer.parseInt(numMediaStr);_96_96 if (numMedia > 0) {_96 while (numMedia > 0) {_96 numMedia = numMedia - 1;_96_96 // Get all info_96 String mediaUrl = parameters.get(String.format("MediaUrl%d", numMedia));_96 String contentType = parameters.get(String.format("MediaContentType%d", numMedia));_96 String fileName = mediaUrl.substring(mediaUrl.lastIndexOf("/") + 1);_96 MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();_96 MimeType mimeType = allTypes.forName(contentType);_96 String fileExtension = mimeType.getExtension();_96 File file = new File(fileName + fileExtension);_96_96 // Download file_96 URL url = new URL(mediaUrl);_96 CloseableHttpClient httpclient = HttpClients.custom()_96 .setRedirectStrategy(new LaxRedirectStrategy())_96 .build();_96 HttpGet get = new HttpGet(url.toURI());_96 HttpResponse response = httpclient.execute(get);_96 InputStream source = response.getEntity().getContent();_96 FileUtils.copyInputStreamToFile(source, file);_96 }_96 }_96_96 // Send message back_96 String message = (numMedia > 0) ? String.format("Thanks for sending us %s file(s)!", numMedia) : "Send us an image!";_96 res.type("application/xml");_96 Body body = new Body_96 .Builder(message)_96 .build();_96 Message sms = new Message_96 .Builder()_96 .body(body)_96 .build();_96 MessagingResponse twiml = new MessagingResponse_96 .Builder()_96 .message(sms)_96 .build();_96 return twiml.toXml();_96_96 });_96 }_96_96 // Body parser help_96 public static Map<String, String> parseBody(String body) throws UnsupportedEncodingException {_96 String[] unparsedParams = body.split("&");_96 Map<String, String> parsedParams = new HashMap<String, String>();_96 for (int i = 0; i < unparsedParams.length; i++) {_96 String[] param = unparsedParams[i].split("=");_96 if (param.length == 2) {_96 parsedParams.put(urlDecode(param[0]), urlDecode(param[1]));_96 } else if (param.length == 1) {_96 parsedParams.put(urlDecode(param[0]), "");_96 }_96 }_96 return parsedParams;_96 }_96_96 public static String urlDecode(String s) throws UnsupportedEncodingException {_96 return URLDecoder.decode(s, "utf-8");_96 }_96}
Another idea for these image files could be uploading them to a cloud storage service like Azure Blob Storage or Amazon S3. You could also save them to a database, if necessary. They're just regular files at this point. Go crazy.
If you are downloading the attachments and no longer need them to be stored by Twilio, you can easily delete them. You can send an HTTP DELETE
request to the media URL and it will be deleted, but you will need to be authenticated to do this.
Twilio supports HTTP Basic and Digest Authentication. Authentication allows you to password protect your TwiML URLs on your web server so that only you and Twilio can access them. Learn more about HTTP authentication and validating incoming requests here.
All the code, in a complete working project, is available on GitHub. If you need to dig a bit deeper, you can head over to our API Reference and learn more about the Twilio webhook request and the Media resource. Also, you will want to be aware of the pricing for storage of all the media files that you keep on Twilio's servers.
We'd love to hear what you build with this.