0

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);  
    }
} 
Community
  • 1
  • 1
pa_One
  • 35
  • 1
  • 2
  • 11

1 Answers1

2

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!

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks a lot philnash. My issue got resolved with your answer. Also i am trying to debug my code by calling the webhook URL I provided in the twilio replies section. I am thinking of using a Rest client tool like postman to hit the url, can you please provide a sample XML, which will come as the request in the above code. – pa_One May 16 '18 at 13:33
  • Glad that helped, could you mark the answer as correct so that others can see it helped, please? Also, the XML should look a bit like: `Store Location: 123 East St.`. – philnash May 16 '18 at 14:39
  • Thanks a ton @philnash, One more query(sorry for the flooding questions under comments section); We have account SID (PN***************) under phone numbers and an account SID(ACf******************) in the console , which one should we use while sending an SMS. --->Also, the xml you provided looks like the response (twiml) we send after receiving a reply, but I would like to have a look at how the request xml looks like(when the reply comes in); the one we are using to get 'body' and 'from' in the above code snippet from the question. – pa_One May 17 '18 at 05:59
  • The SID that starts with "PN" is the phone number SID, so the one you want to use is the Account SID that starts with "AC". The incoming request from Twilio isn't XML, it's `application/x-www-form-urlencoded`. The parameters are all listed here: https://www.twilio.com/docs/sms/twiml. – philnash May 18 '18 at 01:21
  • Hi philnash, In my code while handling replies, which should I use to send the twiml back 1.) response.getWriter().print(twiml.toXml()); or 2.) return twiml.toXml(); – pa_One May 25 '18 at 12:45
  • What are you using as a web framework here? Have you tried both options? – philnash May 27 '18 at 12:45
  • Please have a look at the code in the below 'answer', cant paste such large text into comments. New to stack overflow (sic). Don't mind!! – pa_One May 29 '18 at 06:35
  • I think, when you're using an `HttpServletResponse` object, then you should write to it rather than returning a string. The examples I've seen use `response.getWriter().write(twiml.toXml())` rather than `print` though. – philnash May 29 '18 at 06:56
  • At the line, Body smsBody = new Body.Builder(messageBody).build(); Getting the below error:: java.lang.NoSuchMethodError: com.twilio.twiml.TwiML. (Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)V at com.twilio.twiml.messaging.Body.(Body.java:33) at com.twilio.twiml.messaging.Body.(Body.java:19) at com.twilio.twiml.messaging.Body$Builder.build(Body.java:82) – pa_One May 29 '18 at 08:41
  • Sorry, that's hard to read in a comment too. What is the missing method it is complaining about? – philnash May 29 '18 at 08:47
  • Body.Builder("xyz").build() It is at this line.. not very clear though. – pa_One May 29 '18 at 09:04
  • Are you using the latest version of the Java library? Have you imported all the necessary imports? (see the top of [this example in the documentation](https://www.twilio.com/docs/sms/twiml/message?code-sample=code-simple-sending-of-sms&code-language=java&code-sdk-version=7.x). – philnash May 29 '18 at 09:06
  • Yes Philnash, I am using the same imports mentioned in the example, I am using twilio-7.19.0-jar-with-dependencies – pa_One May 29 '18 at 09:48
  • I don’t have a working version of this to test with, but I can keep trying to guide you. What happens if you just create the builder object? `new Body.Builder(messageBody)`. It also seems to be claiming that there isn’t a constructor. I’m sure there’s not much difference between 7.19.0 and 7.21.0 but it can’t hurt to try to upgrade that too. – philnash May 29 '18 at 10:02
  • Tried: Builder smsBuilder = new Body.Builder(messageBody); smsBody = smsBuilder.build(); Getting the same error near the latter. – pa_One May 29 '18 at 10:59
  • It worked with 7.21.1 jar, Thanks for guiding me thorugh @Philnash – pa_One May 30 '18 at 09:46
  • Oh, I'm glad that's it! I was totally lost otherwise :D – philnash May 30 '18 at 10:17