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

treo :

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()?

Giovanni Lima :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read a sms reply after I sent an SMS in Twilio dashboard?

how can i see a list of all SMS sent in my account?

How can I access a property of my object Thymeleaf (Spring Boot)

How can i send sms to mobile from spring boot application

How can i show app name in place of twilio generated number in my sms

How can I access the "this" in my controller when using "the controller as" syntax?

How can I access my Log files in a running Spring Boot jar?

How can I directly use url to access my spring boot web application without any port?

How do I add a line break in my Twilio SMS message?

How do I send a text message (SMS) with Twilio with React Native?

Spring Boot can't access REST Controller

How can I retrieve Kafka messages inside a controller in Spring Boot?

How can I get time zone information for an SMS with Twilio?

How can I add values to Twilio SMS GitHub Action?

How can i access my filtered array from controller in angularjs?

How can I access my models in sailsjs outside a controller?

How can I log all sent SMS along with package name?

How can I use a Spring HttpRequest in my controller?

How to access a Codenvy Spring Boot web controller?

Spring Boot - how to avoid concurrent access to controller

How can I pass data loaded when my application finished launching thru my UITabBar to a UITableView?

How do I correctly access my PHP files? Directly from JS or a thru a "master" PHP?

How to access Rest APIs mentioned in Controller class of a Jar file in my Spring Boot Application

I can't send SMS on indian numbers from my Twilio account

Can't access REST Controller with Spring Boot autoconfiguration

How can I sent a variable from a controller to a view in Laravel?

I am trying to add Twilio broadcast SMS feature to my Django app, but can't work out how to retrieve all Database phone numbers

Can Spring Boot be configured to check unique constraints in my rest controller?

Access SMS Sent Message