Configured a webhook URL which calls the below code :::
String body = request.getParameter("Body");
String fromNumber = request.getParameter("From");
String message;
Message sms = new Message.Builder().body(new Body(message)).build();
// Create a TwiML response and add our friendly message.
MessagingResponse twiml = new MessagingResponse.Builder().message(sms).build();
response.setContentType("application/xml");
try {
response.getWriter().print(twiml.toXml());
return twiml.toXml();
final TwilioEmailContextDTO twilioEmailContextDTO = new TwilioEmailContextDTO();
twilioEmailContextDTO.setBody(body);
twilioEmailContextDTO.setFromNumber(fromNumber);
forwardTwilioSMSToMail(twilioEmailContextDTO);
}
Q1 : Does String fromNumber = request.getParameter("From");
give me the from number.
Q2 : Also I am getting the The constructor Body(String) is undefined"
compilation error.
Q3 : I am using conventional Hybris way to forward the SMS as a mail(using emailService), do we have a twilio way of doing it?
update with code
{ Please have a look at the method I am using. Using Spring, annotations.
@RequestMapping(value = "/twilioReply", method = RequestMethod.POST)
@ResponseBody
public void TwilioReplies(HttpServletRequest request, HttpServletResponse response) throws IOException {
String body = request.getParameter("Body");
String fromNumber = request.getParameter("From");
String messageBody = this.configurationService.getConfiguration().getString(TWILIO_REPLY);
Body smsBody = new Body.Builder(messageBody).build();
Message message = new Message.Builder().body(smsBody).build();
// Create a TwiML response and add our message.
MessagingResponse twiml = new MessagingResponse.Builder().message(message).build();
response.setContentType("application/xml");
try {
response.getWriter().print(twiml.toXml());
} catch (TwiMLException e) {
LOG.error("Exception Occured while twiml Email :",e);
}
}
Twilio developer evangelist here.
A1: The From
parameter is the one that Twilio sends for the number that sent the message. So yes, request.getParameter("From")
should be that number.
A2: the Body
is also built, as in this example in the documentation:
Body body = new Body.Builder("Store Location: 123 Easy St.").build();
Message message = new Message.Builder().body(body).build();
A3: Twilio has no opinions about how you send emails on your own server. If that is working for you, then go for it!