Format mail and sms
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package be.naaturel.boardmateapi.services;
|
package be.naaturel.boardmateapi.services;
|
||||||
|
|
||||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
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.Method;
|
||||||
import com.sendgrid.Request;
|
import com.sendgrid.Request;
|
||||||
import com.sendgrid.Response;
|
import com.sendgrid.Response;
|
||||||
@@ -27,11 +30,9 @@ public class SendgridService {
|
|||||||
|
|
||||||
public void send(String dst, String subject, String text) throws ServiceException {
|
public void send(String dst, String subject, String text) throws ServiceException {
|
||||||
try {
|
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);
|
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 request = new Request();
|
||||||
request.setMethod(Method.POST);
|
request.setMethod(Method.POST);
|
||||||
request.setEndpoint("mail/send");
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
package be.naaturel.boardmateapi.services;
|
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.rest.api.v2010.account.Message;
|
||||||
import com.twilio.type.PhoneNumber;
|
import com.twilio.type.PhoneNumber;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TwilioService {
|
public class TwilioService {
|
||||||
|
|
||||||
@@ -16,7 +22,14 @@ public class TwilioService {
|
|||||||
this.serviceId = serviceId;
|
this.serviceId = serviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String msg){
|
public void send(String msg) throws ServiceException {
|
||||||
Message message = Message.creator(new PhoneNumber("+32496533833"), this.serviceId, msg).create();
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user