Twilio SMS API Part-2 – Twilio Incoming message handling

Twilio Incoming message handling

We can test via SMS log from twilio dashboard


Import Note :

When sending SMS messages to your Sandbox Number, you must include your PIN at the beginning of your message.

For example, a message would look something like “2732-3300 Ahoy Hoy!”

The Twilio sandbox would then call your SMS URL. You can’t get a number of your own in the trial, for that you will need to upgrade your account by attaching a credit card.

Twilio server callback url code setup

public class TwilioBackendHandler extends HttpServlet {

private static final Logger logger = Logger.getLogger(“”);
private final String SUBSCRIBE = “Subscribe”;
private final String UNSUBSCRIBE = “Unsubscribe”;

private String toNumber;
private String fromNumber;
private String bodyMessage;
private String shortName;
private String promoStatus;

* Processes requests for both HTTP <shortName>GET</shortName> and <shortName>POST</shortName> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();

try {
toNumber = request.getParameter(“To”).trim();
fromNumber = request.getParameter(“From”).trim();
bodyMessage = request.getParameter(“Body”).trim();“To number ” + toNumber);“From number ” + fromNumber);“Body Message ” + bodyMessage);

String[] smsBodyMessage = bodyMessage.split(” “);” smsContent :: ” + smsBodyMessage);

if (smsBodyMessage.length == 2) {
shortName = smsBodyMessage[0].trim();
promoStatus = smsBodyMessage[1].trim();


Enumeration en = request.getParameterNames();
String str = “”;” ———- All Twilio Parameters ———— “);

while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
String paramValue = request.getParameter(paramName);

str = str + “&” + paramName + “=” + URLEncoder.encode(paramValue);
}“Response string  —-> ” + str);

} finally {


2 thoughts on “Twilio SMS API Part-2 – Twilio Incoming message handling

  1. Tim says:

    I am using TwilioRestClient for sending sms through Twilio. How can I assure STOP to actually unsubscribe user? Also, How can I implement re-subscribe functionality?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s