How do you turn a handful of isolated messages to and from the same party into a true conversation? You need some way to remember state between each message that is exchanged. This is because SMS is a stateless protocol. Building traditional web applications has this same hurdle, as HTTP is also a stateless protocol. This problem has been solved for web applications through the use of HTTP cookies and, rather than reinvent the wheel, the Twilio Messaging API uses the same solution.
This guide will show you how to use Programmable Messaging to accomplish this in your Java Servlets application. The code snippets in this guide are written using Java and require the Java JDK 7 or higher. They also make use of the Twilio Java SDK.
If you haven't written your own SMS webhooks with Java and Servlets before, you may want to first check out our guide, Receive and Reply to SMS and MMS Messages in Java. Ready to go? Let's get started!
Twilio Conversations, a more recent product offering, is an omni-channel messaging platform that allows you to build engaging conversational, two-way messaging experiences. Be sure to check out our Conversations product to see if it's a better fit for your needs.
The cookies let you share state across multiple messages allowing you to treat separate messages as a conversation, and store data about the conversation in the cookies for future reference.
You can store the data directly in a cookie, or you can use a session state management framework. The code sample below does the latter, using the Interface HttpSession available for Java Servlets.
Let's try using session counters to see if a particular user has messaged us before. If they're a new sender, we'll thank them for the new message. If they've sent us messages before, we'll specify how many messages we've gotten from them.
1import java.io.IOException;2import java.util.HashMap;34import javax.servlet.http.HttpServlet;5import javax.servlet.http.HttpServletRequest;6import javax.servlet.http.HttpServletResponse;7import javax.servlet.http.HttpSession;89import com.twilio.twiml.messaging.Body;10import com.twilio.twiml.messaging.Message;11import com.twilio.twiml.MessagingResponse;12import com.twilio.twiml.TwiMLException;1314public class TwilioServlet extends HttpServlet {15public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {16HttpSession session = request.getSession(true);17Integer counter = (Integer) session.getAttribute("counter");18if (counter == null) {19counter = new Integer(0);20}2122/* Increment the counter by one, and store the count in the session. */23int count = counter.intValue();24count++;25session.setAttribute("counter", new Integer(count));2627// Create a dict of people we know.28HashMap<String, String> callers = new HashMap<String, String>();29callers.put("+14158675308", "Rey");30callers.put("+12349013030", "Finn");31callers.put("+12348134522", "Chewy");3233String fromNumber = request.getParameter("From");34String toNumber = request.getParameter("To");35String fromName = callers.get(fromNumber);36if (fromName == null) {37// Use the caller's name38fromName = "Friend";39}4041String message =42fromName + " has messaged " + toNumber + " " + String.valueOf(count) + " times.";4344// Create a TwiML response and add our friendly message.45Message sms = new Message.Builder().body(new Body(message)).build();46MessagingResponse twimlResponse = new MessagingResponse.Builder().message(sms).build();4748response.setContentType("application/xml");4950try {51response.getWriter().print(twimlResponse.toXml());52} catch (TwiMLException e) {53e.printStackTrace();54}55}56}