Implement Twilio and Sendgrid
This commit is contained in:
@@ -51,6 +51,12 @@ dependencies {
|
||||
//======================MQTT======================
|
||||
implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5")
|
||||
|
||||
//======================TWILIO======================
|
||||
implementation(group = "com.twilio.sdk", name = "twilio", version = "8.0.0")
|
||||
|
||||
//======================SENDGRID======================
|
||||
implementation("com.sendgrid:sendgrid-java:4.4.1")
|
||||
|
||||
//======================PROMETHEUS======================
|
||||
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
|
||||
|
||||
|
||||
@@ -19,6 +19,10 @@ services:
|
||||
WEBEX_BOT_TOKEN: "YmU0NTdkNDMtYTg1ZC00M2YyLTk3YzUtODA1MWFmOTk1NjA1ZmU3MTYxNGUtYWZm_P0A1_14a2639d-5e4d-48b4-9757-f4b8a23372de"
|
||||
WEBEX_CLIENT_TOKEN: "YjkyMmJkNDQtNDViZC00Zjg3LThiMDItNzk3NDIxOGVhODBhYjVjMTQ4YzktMjFi_P0A1_14a2639d-5e4d-48b4-9757-f4b8a23372de"
|
||||
WEBEX_SHARED_SECRET: "cUxSc0ZjNVBZVG5oRmhqaVN0YUtMTEVZb0pIZW5EY2Rwa0hUaWdiVm9nWlJiY2t5aFdmTjhvWmQ5U3R3TDIxVE1CRTl4VGJldVI3TFdVa3lMbFVPZUVSMkZPSnBHTjk="
|
||||
TWILIO_SERVICE_ID: "MGc971109e0d7e7e17ba93ae3b641d8673"
|
||||
TWILIO_ACCOUNT_SID: "ACf041152850f4535c266bb82644bda152"
|
||||
TWILIO_AUTH_TOKEN: "15fc6699c14892cf363ccac0a39ada58"
|
||||
SENDGRID_KEY: "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"
|
||||
DATABASE_NAME: "board-mate-db"
|
||||
JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
|
||||
depends_on:
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package be.naaturel.boardmateapi.configurations.configurations;
|
||||
|
||||
import be.naaturel.boardmateapi.configurations.properties.SendgridProperties;
|
||||
import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.helpers.mail.objects.Email;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SendgridConfig {
|
||||
|
||||
|
||||
private final SendgridProperties properties;
|
||||
|
||||
|
||||
public SendgridConfig(SendgridProperties properties){
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Bean("email")
|
||||
public Email sendGridMail(){
|
||||
return new Email("laurent0206.cr@gmail.com");
|
||||
}
|
||||
|
||||
@Bean("sendGrid")
|
||||
public SendGrid sendGrid(){
|
||||
return new SendGrid(this.properties.apiKey);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package be.naaturel.boardmateapi.configurations.configurations;
|
||||
|
||||
import be.naaturel.boardmateapi.configurations.properties.TwilioProperties;
|
||||
import com.twilio.Twilio;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class TwilioConfig {
|
||||
|
||||
private final TwilioProperties properties;
|
||||
|
||||
public TwilioConfig(TwilioProperties properties){
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Bean(name = "twilioServiceId")
|
||||
public String serviceId(){
|
||||
return this.properties.serviceId;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Object initTwilio(){
|
||||
Twilio.init(properties.sid, properties.authToken);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package be.naaturel.boardmateapi.configurations.properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SendgridProperties {
|
||||
|
||||
@Value("${sendgrid.api.key}")
|
||||
public String apiKey;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package be.naaturel.boardmateapi.configurations.properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TwilioProperties {
|
||||
|
||||
|
||||
@Value("${twilio.service-id}")
|
||||
public String serviceId;
|
||||
|
||||
@Value("${twilio.account-sid}")
|
||||
public String sid;
|
||||
|
||||
@Value("${twilio.auth-token}")
|
||||
public String authToken;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package be.naaturel.boardmateapi.controllers;
|
||||
|
||||
import be.naaturel.boardmateapi.services.SendgridService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@RestController
|
||||
public class EmailController {
|
||||
|
||||
private final SendgridService service;
|
||||
|
||||
@Autowired
|
||||
public EmailController(SendgridService service){
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostMapping("/mail/send")
|
||||
public ResponseEntity<String> send(@RequestBody Map<String, Object> payload){
|
||||
try {
|
||||
service.send("laurent0206.cr@gmail.com", "Test", payload.get("content").toString());
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package be.naaturel.boardmateapi.controllers;
|
||||
|
||||
import be.naaturel.boardmateapi.services.TwilioService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class SmsController {
|
||||
|
||||
private final TwilioService service;
|
||||
|
||||
@Autowired
|
||||
public SmsController(TwilioService service){
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/sms/send")
|
||||
public ResponseEntity<String> example(){
|
||||
try {
|
||||
service.send("Hello there !");
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package be.naaturel.boardmateapi.services;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import com.sendgrid.Method;
|
||||
import com.sendgrid.Request;
|
||||
import com.sendgrid.Response;
|
||||
import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.helpers.mail.Mail;
|
||||
import com.sendgrid.helpers.mail.objects.Content;
|
||||
import com.sendgrid.helpers.mail.objects.Email;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SendgridService {
|
||||
|
||||
|
||||
private final Email senderEmail;
|
||||
private final SendGrid sendGrid;
|
||||
|
||||
public SendgridService(
|
||||
@Qualifier("email") Email email,
|
||||
@Qualifier("sendGrid") SendGrid sendGrid){
|
||||
this.sendGrid = sendGrid;
|
||||
this.senderEmail = email;
|
||||
}
|
||||
|
||||
public void send(String dst, String subject, String text) throws ServiceException {
|
||||
try {
|
||||
Content content = new Content("text/plain", 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");
|
||||
request.setBody(mail.build());
|
||||
Response response = this.sendGrid.api(request);
|
||||
System.out.println(response.getStatusCode());
|
||||
System.out.println(response.getBody());
|
||||
System.out.println(response.getHeaders());
|
||||
} catch (Exception e){
|
||||
throw new ServiceException("Failed to send email : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package be.naaturel.boardmateapi.services;
|
||||
|
||||
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;
|
||||
|
||||
@Service
|
||||
public class TwilioService {
|
||||
|
||||
private final String serviceId;
|
||||
|
||||
@Autowired
|
||||
public TwilioService(@Qualifier("twilioServiceId") String serviceId){
|
||||
this.serviceId = serviceId;
|
||||
}
|
||||
|
||||
public void send(String msg){
|
||||
Message message = Message.creator(new PhoneNumber("+32496533833"), this.serviceId, msg).create();
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,15 @@ mqtt.password=${BROKER_PASSWORD}
|
||||
webex.client.token=${WEBEX_CLIENT_TOKEN}
|
||||
webex.bot.token=${WEBEX_BOT_TOKEN}
|
||||
webex.shared-secret=${WEBEX_SHARED_SECRET}
|
||||
|
||||
#=============TWILIO=============
|
||||
twilio.service-id=${TWILIO_SERVICE_ID}
|
||||
twilio.account-sid=${TWILIO_ACCOUNT_SID}
|
||||
twilio.auth-token=${TWILIO_AUTH_TOKEN}
|
||||
|
||||
#=============SENDGRID=============
|
||||
sendgrid.api.key=${SENDGRID_KEY}
|
||||
|
||||
#=============NGROK=============
|
||||
ngrok.url=${NGROK_URL}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user