如何从Twilio响应消息中获取内容

时间:2018-01-28 15:03:36

标签: java spring-mvc twilio

@RequestMapping(value = {"sms"},method = RequestMethod.POST)
public string rplyMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {

    Body body = new Body.Builder("Response message").build();
Message sms =
        new Message.Builder().body(body).build();

MessagingResponse twiml = new MessagingResponse.Builder().message(sms).build();

response.setContentType("application/xml");

try {
    response.getWriter().print(twiml.toXml());
} catch (TwiMLException e) {
    e.printStackTrace();
}

}

这就是我处理twilio响应消息的方式。我想从响应消息中获取内容。我想将它存储在数据库中。如何从响应消息中获取内容。

1 个答案:

答案 0 :(得分:0)

Twilio开发者传道者在这里。

Twilio makes a request to your application时,它会在POST请求的正文中发送编码为application/x-www-form-urlencoded的参数。

我之前从未编写过Java Spring MVC,所以请原谅,如果这不是现场,但我相信你可以使用@RequestParam注释从身体中读取这些参数。

@RequestMapping(value = {"sms"},method = RequestMethod.POST)
public string rplyMessage(
    HttpServletRequest request,
    HttpServletResponse response,
    @RequestParam("Body") String message,
    @RequestParam("From") String from
) throws IOException {
    storeMessage(from, message);

    // respond to the request
} 

邮件正文和发送邮件的编号是参数“正文”和“发件人”,您可以看到all the available request parameters here。因此,例如使用消息,您将参数设置为@RequestParam到参数的名称,然后设置类型以及您希望在方法中调用变量的内容,因此:@RequestParam("Body") String message

我不知道你打算如何使用数据库,但这就是我能告诉你的。您可以阅读有关@RequestParam here的更多信息,并查看一些Twilio Java and Spring tutorials here

让我知道这是否有帮助。