Format mail and sms

This commit is contained in:
2026-01-05 23:27:12 +01:00
parent 32c67eb21a
commit 499b4b4834
2 changed files with 41 additions and 5 deletions

View File

@@ -1,6 +1,9 @@
package be.naaturel.boardmateapi.services;
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
@@ -27,11 +30,9 @@ public class SendgridService {
public void send(String dst, String subject, String text) throws ServiceException {
try {
Content content = new Content("text/plain", text);
Content content = new Content("text/plain", formatContent(text));
Mail mail = new Mail(this.senderEmail, subject, new Email(dst), content);
// sg.setDataResidency("eu");
// uncomment the above line if you are sending mail using a regional EU subuser
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
@@ -45,5 +46,27 @@ public class SendgridService {
}
}
private String formatContent(String content) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(content);
StringBuilder mailBody = new StringBuilder();
mailBody.append("White: ").append(root.get("white_name").asText()).append("\n");
mailBody.append("Black: ").append(root.get("black_name").asText()).append("\n");
mailBody.append("Time Control: ")
.append(root.get("time_control").asInt())
.append(" + ")
.append(root.get("increment").asInt())
.append("\n");
mailBody.append("Timestamp: ").append(root.get("timestamp").asLong()).append("\n\n");
mailBody.append("Moves:\n");
int i = 1;
for (JsonNode move : root.get("moves")) {
mailBody.append(i++).append(". ").append(move.asText()).append("\n");
}
return mailBody.toString();
}
}

View File

@@ -1,11 +1,17 @@
package be.naaturel.boardmateapi.services;
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class TwilioService {
@@ -16,7 +22,14 @@ public class TwilioService {
this.serviceId = serviceId;
}
public void send(String msg){
Message message = Message.creator(new PhoneNumber("+32496533833"), this.serviceId, msg).create();
public void send(String msg) throws ServiceException {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(msg);
String content = jsonNode.get("content").asText();
Message.creator(new PhoneNumber("+32496533833"), this.serviceId, content).create();
} catch (Exception e){
throw new ServiceException("Failed to send message :" + e.getMessage());
}
}
}