Twilio SMS API Part-2 – Twilio Incoming message handling

Twilio Incoming message handling
——————————————-

We can test via SMS log from twilio dashboard

SMS Log


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(“org.company.dispatcher”);
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 {
response.setContentType(“text/html;charset=UTF-8”);
PrintWriter out = response.getWriter();

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

logger.info(“To number ” + toNumber);
logger.info(“From number ” + fromNumber);
logger.info(“Body Message ” + bodyMessage);

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

logger.info(” smsContent :: ” + smsBodyMessage);

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

doAction(promoStatus);

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

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

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

} finally {
out.close();
}
}

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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