treo
treo

Reputation: 85

How can I access the text of the SMS sent thru Twilio to my Spring Boot Controller?

I'm trying to use Twilio to receive SMS messages, and use the body of the SMS to perform various DB functions. The part where I'm stuck is parsing the message that I get from Twilio when they receive a text message.

Here's the controller:

@RequestMapping(
    value = "/incomingSMS",
    method = RequestMethod.POST)
public void getPhrase(@RequestBody String request) {
    System.out.println("***********************************");
    System.out.println(request);
    System.out.println("***********************************");
}

And here's what is getting printed out (with new lines added for readability, and some numbers censored.):

ToCountry=US&
ToState=statecode&
SmsMessageSid=smsMessageSid&
NumMedia=0&
ToCity=city&
FromZip=zipCode&
SmsSid=SmsSid&
FromState=statecode&
SmsStatus=received&
FromCity=city&
Body=Hello+it%27s+John+&
FromCountry=US&
To=%2B1toPhoneNumber&
ToZip=55401&
NumSegments=1&
MessageSid=messageSid&
AccountSid=accountSid&
From=%2B1fromPhoneNumber&
ApiVersion=2010-04-01

I can see in "Body" my message is hiding. I also see the phone number. Is there any way I can just parse this into a Twilio object that I don't know about, so I can use methods like getBody(), getFrom()?

Upvotes: 1

Views: 294

Answers (1)

Giovanni Lima
Giovanni Lima

Reputation: 71

You can easily manipulate it by using the good old java.util.Properties class.

For the example bellow, I'm using the Apache Common IO lib to transform the String into an InputStream witch is required by Properties class. After that, all you have to do is use getProperty method to get what you need.

package com.pipocandobits.maven;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.util.Properties;

public class App {
    public static void main( String[] args ) throws IOException {
        System.out.println( "Hello World!" );

        String source = "ToCountry=US&\n" +
                "ToState=statecode&\n" +
                "SmsMessageSid=smsMessageSid&\n" +
                "NumMedia=0&\n" +
                "ToCity=city&\n" +
                "FromZip=zipCode&\n" +
                "SmsSid=SmsSid&\n" +
                "FromState=statecode&\n" +
                "SmsStatus=received&\n" +
                "FromCity=city&\n" +
                "Body=Hello+it%27s+John+&\n" +
                "FromCountry=US&\n" +
                "To=%2B1toPhoneNumber&\n" +
                "ToZip=55401&\n" +
                "NumSegments=1&\n" +
                "MessageSid=messageSid&\n" +
                "AccountSid=accountSid&\n" +
                "From=%2B1fromPhoneNumber&\n" +
                "ApiVersion=2010-04-01";

        Properties properties = new Properties();
        properties.load(IOUtils.toInputStream(source, "UTF-8"));

        System.out.println("Message body = " + properties.getProperty("Body"));
    }
}

For more on how to use the java.util.Properties class check this link https://www.tutorialspoint.com/java/java_properties_class.htm

Upvotes: 3

Related Questions