基于现有Twilio响应处理代码,如何提取内容并存入数据库?
Ah, I get it—you want to capture the content related to this Twilio SMS interaction to store in your database, right? Let’s break this down based on what exactly you’re trying to save:
If you want to store the incoming message that triggered your webhook
Your current code generates the reply, but Twilio sends all details of the incoming SMS as form parameters in the POST request to your /sms endpoint. Here’s how to extract and save that data:
- Pull the key values from the
HttpServletRequest—Twilio passes parameters likeBody(the incoming message content),From(sender’s number),To(your Twilio number), andMessageSid(unique ID for the message). - Add your database persistence logic to store these values.
Here’s the modified code with this included:
@RequestMapping(value = {"sms"},method = RequestMethod.POST) public String rplyMessage(HttpServletRequest request, HttpServletResponse response) throws IOException { // Extract incoming SMS details from Twilio's request String incomingMessage = request.getParameter("Body"); String senderPhone = request.getParameter("From"); String yourTwilioNumber = request.getParameter("To"); String messageId = request.getParameter("MessageSid"); // Store these values in your database (replace with your actual DB code) // Example: smsDao.save(new SMSRecord(messageId, incomingMessage, senderPhone, yourTwilioNumber)); // Build and send your reply as before 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(); } return twiml.toXml(); }
If you want to store the reply message you’re sending
That’s even simpler—just capture the reply content before building the TwiML response:
@RequestMapping(value = {"sms"},method = RequestMethod.POST) public String rplyMessage(HttpServletRequest request, HttpServletResponse response) throws IOException { // Define and capture your reply content String replyContent = "Response message"; // Store replyContent in your database here Body body = new Body.Builder(replyContent).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(); } return twiml.toXml(); }
A few quick notes:
- Always add null checks for the request parameters (though Twilio will typically send all required fields for incoming SMS)
- Wrap your database operations in error handling to avoid breaking the Twilio response flow if something goes wrong
- If you’re using a framework like Spring Data JPA, create an entity class for your SMS records and use a repository to handle persistence
内容的提问来源于stack exchange,提问作者user8212013
相关产品推荐
相关产品推荐

