2
votes

I'm trying to handle SMSs sent to my twilio account server side.

It set up the URL on twilio's dashboard:

http://f.cl.ly/items/093Q0E3l081R1H3Q3P00/Screen%20Shot%202014-12-12%20at%2012.43.54%20PM.png

The endoint is very simple:

    @BodyParser.Of(BodyParser.Json.class)
public static Result handleManagerSMS() {
    Logger.info("handleManagerSMS body (text):" + request().body().asText());
    Logger.info("handleManagerSMS body (json):" + request().body().asJson());
    IncomingSMS incomingSMS = parseRequestJson(IncomingSMS.class);
    if (incomingSMS != null) {
        Logger.info(incomingSMS.toString());
}
    TwiMLResponse twiml = new TwiMLResponse();
return ok(twiml.toXML()).as("text/xml");
}

Both values printed are null:

[info] application - handleManagerSMS body (text):null
[info] application - handleManagerSMS body (json):null

I was expecting to get the data described here:

https://www.twilio.com/docs/api/twiml/sms/twilio_request

Any idea? Thanks

1
I ended up using a GET instead of a POST and I got the parameters, as expected, by calling: request().queryString() - LG01

1 Answers

1
votes

Ricky from Twilio here.

Glad you got this working with a GET request. If you want to use a POST request here you can use DynamicForm to access the POST data.

First you'd need to import the required library:

import play.data.DynamicForm;
import play.data.Form;

You can then access the POST data like this within your handleManagerSMS function:

  DynamicForm form = Form.form().bindFromRequest();
  Logger.info("Get Body of Text Message:" + form.get("Body"));
  Logger.info("Get From Number of Text Message:" + form.get("From"));