The body of the method creates a list of objects Message that is populated differently depending on whether the incoming SMS that triggered this webhook is from a group member or not (line 12). We'll define the messagesSentFromGroup and methods messagesSentToGroup in a moment, but first notice how the Messages list is added to a MessagingResponse using forEach on lines 19-21. Handling messages from people outside the group If the check groupPhoneNumbers.contains(fromNumber) in the above method returns false, then we know the message is coming from someone outside our group. In this case, the method messagesSentToGroup is called to get a list of objects Message representing a copy of the incoming message to forward to each member of the group: Java Copy philippines whatsapp number the code private List<Message> messagesSentToGroup(String fromNumber, String twilioNumber, String messageBody) { List<Message> messages = new ArrayList<>(); String finalMessage = "From " + fromNumber + " " + messageBody; groupPhoneNumbers.
forEach(groupMemberNumber -> messages.add(createMessageTwiml(groupMemberNumber, twilioNumber, finalMessage)) ); return messages; } [ this code with imports on GitHub ] We build the finalMessage and add a Message to the list for each member of the group. I created a small helper method called createMessageTwiml to turn the Twilio library build template code into a single line. I thought it was worth it since we're going to be building Message objects multiple times in this class. This method looks like this: Java Copy the code private Message createMessageTwiml(String to, String from, String body) { return new Message.Builder() .
to(to) .from(from) .body(new Body.Builder(body).build()) .build(); } [ this code with imports on GitHub ] Messages from group members When a group member sends a message to the Twilio number, they must preface it with the actual destination number: A text message saying “+336xxxxx Thanks!” The "Thank you!" part of the message will be sent from the Twilio number to the number at the beginning of the message. Everyone in the group will also get a copy. To do this, the method messagesSentFromGroup needs to split the body of the message, check if it starts with a phone number (if not, send a reminder), and create a list of outgoing messages, like this: Java Copy the code private List<Message> messagesSentFromGroup(String fromNumber, String twilioNumber, String messageBody) { List<Message> messages = new ArrayList<>(); String[] messageParts = messageBody.
The body of the method creates
-
- Posts: 9
- Joined: Sun Dec 22, 2024 4:39 am