Add webex message posting
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package be.naaturel.boardmateapi.services;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.common.models.Message;
|
||||
import be.naaturel.boardmateapi.repository.MessageRepo;
|
||||
import be.naaturel.boardmateapi.repository.dtos.MessageDto;
|
||||
import be.naaturel.boardmateapi.repository.mappings.MessageMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MessageService {
|
||||
|
||||
private final MessageRepo repo;
|
||||
|
||||
|
||||
public MessageService(MessageRepo repo){
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
public String save(Message message) throws ServiceException {
|
||||
try {
|
||||
MessageDto dto = MessageMapper.toDto(message);
|
||||
MessageDto result = repo.save(dto);
|
||||
return result.getId();
|
||||
} catch (Exception e){
|
||||
throw new ServiceException("Failed to save message : " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package be.naaturel.boardmateapi.services;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.common.helpers.Logger;
|
||||
import be.naaturel.boardmateapi.common.models.Message;
|
||||
import be.naaturel.boardmateapi.common.models.Room;
|
||||
import be.naaturel.boardmateapi.repository.RoomRepo;
|
||||
import be.naaturel.boardmateapi.repository.dtos.RoomDto;
|
||||
import be.naaturel.boardmateapi.repository.mappings.RoomMapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class WebexService {
|
||||
|
||||
private final String botToken;
|
||||
private final RoomRepo repo;
|
||||
|
||||
@Autowired
|
||||
public WebexService(
|
||||
RoomRepo repo,
|
||||
@Qualifier("botToken") String botToken){
|
||||
this.repo = repo;
|
||||
this.botToken = botToken;
|
||||
}
|
||||
|
||||
public void post(Message m) throws ServiceException {
|
||||
try(HttpClient client = HttpClient.newHttpClient()) {
|
||||
|
||||
Room room = getClientRoom(m.getClientId());
|
||||
if(room == null){
|
||||
room = createRoom(m.getClientId());
|
||||
}
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String jsonBody = mapper.writeValueAsString(
|
||||
Map.of(
|
||||
"roomId", room.getId(),
|
||||
"text", m.getContent()
|
||||
)
|
||||
);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://webexapis.com/v1/messages"))
|
||||
.header("Authorization", "Bearer " + this.botToken)
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response =
|
||||
client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() >= 300) {
|
||||
throw new RuntimeException("Webex error " + response.statusCode() + " : " + response.body());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
Logger.displayError(Arrays.toString(e.getStackTrace()));
|
||||
throw new ServiceException("Failed to post message");
|
||||
}
|
||||
}
|
||||
|
||||
public Room createRoom(String clientId) throws ServiceException {
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String jsonBody = mapper.writeValueAsString(Map.of("title", "Support"));
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://webexapis.com/v1/rooms"))
|
||||
.header("Authorization", "Bearer " + this.botToken)
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() >= 300) {
|
||||
throw new RuntimeException("Webex error " + response.statusCode() + " : " + response.body());
|
||||
}
|
||||
|
||||
JsonNode jsonNode = mapper.readTree(response.body());
|
||||
String id = jsonNode.get("id").asText();
|
||||
String title = jsonNode.get("title").asText();
|
||||
RoomDto dto = new RoomDto();
|
||||
dto.setTitle(title);
|
||||
dto.setClientId(clientId);
|
||||
dto.setRoomId(id);
|
||||
repo.save(dto);
|
||||
|
||||
return new Room(id, title, clientId);
|
||||
} catch (Exception e) {
|
||||
Logger.displayError(Arrays.toString(e.getStackTrace()));
|
||||
throw new ServiceException("Failed to create private room : " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private Room getClientRoom(String clientId){
|
||||
Optional<RoomDto> dto = repo.findByClientId(clientId);
|
||||
return dto.map(RoomMapper::toModel).orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user