问题
TO NOTE: When you see
postmapping-url-here
andMyOwnDefinedEntity
down where I show the code, it just means I'm trying not to reveal too much.
So I'm using Twilio
to send and receive text messages. I've been stuck on this problem for around 3 days now, and I just can't seem to figure out how to resolve it.
I'm using Spring Boot
as the application framework, Gradle
as the build tool, and VSCode
as the IDE. I'm also using Ngrok
to create a tunnel for localhost:8080
to run on.
When I ran it as a:
public static void main(String[] args) {
}
it worked perfectly and my Twilio
number sent a text back in response.
However, when I put it inside of its own function, call that function, and run it with the entire application, I still get the same status code of 200
from when it worked as a main
method, but my Twilio
number doesn't text me back.
I've tried using @PostMapping
and @GetMapping
as I've tried testing it out for both POST
and GET
.
In my send and receive code from the Twilio
site, I've tried using application/xml
and application/json
for the response type.
Here's some of the code I have so far:
public static void TwilioRespondToSMS() {
get("/", (req, res) -> "");
post("/<postmapping-url-here>", (req, res) -> {
res.type("application/xml");
Body body = new Body
.Builder("This is a response to your text message")
.build();
Message sms = new Message
.Builder()
.body(body)
.build();
MessagingResponse twiml = new MessagingResponse
.Builder()
.message(sms)
.build();
return twiml.toXml();
});
}
This is the main
code:
@SpringBootApplication
public class ApplicationServerClass {
public static void main(String[] args) {
SpringApplication.run(ApplicationServerClass.class, args);
//TwilioRespondToSMS();
}
// Whatever other code ...
}
I've also tried putting my function inside of:
@Bean
public CommandLineRunner commandRunner() {
return (args) -> {
TwilioRespondToSMS();
// Whatever other code ...
}
}
Here is the @PostMapping
function:
@CrossOrigin(origins = "http://localhost:4200")
@PostMapping("/<postmapping-url-here>")
public ResponseEntity<MyOwnDefinedEntity> getMyOwnDefinedEntity(@PathVariable Long id) {
log.debug("REST request to get MyOwnDefinedEntity : {}", id);
Optional<MyOwnDefinedEntity> myOwnDefinedEntity = myOwnDefinedEntityRepository.findById(id);
//if(myOwnDefinedEntity.isPresent())
return new ResponseEntity<MyOwnDefinedEntity>(MyOwnDefinedEntity.get(), HttpStatus.OK);
}
The example from Twilio's site showed it as a main
function. Is there anything I need to change from that example when running it with the entire application?
Thanks in advance.
回答1:
Okay so after 4 days of trying to resolve my problem, I've finally been able to come up with a solution on my own.
I figured maybe it was just a silly mistake that I've never noticed until now, but I feel like this solution wasn't very easy to come up with. So I'm going to share how I came up with this.
Before mapping the URL in my post
function to a custom @PostMapping
method, I was actually getting a status code of 404
because it did not recognize any POST
request URLs to map to.
I figured the correct solution would be to use a custom made @PostMapping
, but this just caused my Java Spark
to be using the same port: 8080
as the port my Spring Boot
application was running on. This is why I was not receiving any texts back from my Twilio number, even though I was getting a status code of 200
.
So here's what I did instead to fix the problem:
First, I removed my custom
@PostMapping
function
@PostMapping("/<postmapping-url-here>")
public ResponseEntity<MyOwnDefinedEntity> getMyOwnDefinedEntity(@PathVariable Long id) {
log.debug("REST request to get MyOwnDefinedEntity : {}", id);
Optional<MyOwnDefinedEntity> myOwnDefinedEntity = myOwnDefinedEntityRepository.findById(id);
//if(myOwnDefinedEntity.isPresent())
return new ResponseEntity<MyOwnDefinedEntity>(MyOwnDefinedEntity.get(), HttpStatus.OK);
}
I kept my function inside of:
@Bean
public CommandLineRunner commandRunner() {
return (args) -> {
TwilioRespondToSMS();
// Whatever other code ...
}
}
Finally, inside of my TwilioRespondToSMS()
, I added in: port(8070);
, or whatever other port number that isn't 8080
.
In this case Java Spark
is now using 8070
through the Ngrok
tunnel URL and Spring Boot
is using 8080
. At this point, I've finally been successfully able to get a response text message back from my Twilio
number while running Spring Boot
at the same time.
来源:https://stackoverflow.com/questions/55215504/is-there-a-reason-why-my-twilio-phone-number-wont-respond-to-text-messages-even