Integrate webex webhooks
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
package be.naaturel.boardmateapi.controllers;
|
||||
|
||||
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.controllers.dtos.MessageDto;
|
||||
import be.naaturel.boardmateapi.controllers.dtos.MessagePostRequestDto;
|
||||
import be.naaturel.boardmateapi.controllers.dtos.ResponseBody;
|
||||
import be.naaturel.boardmateapi.controllers.dtos.WebexWebhook;
|
||||
import be.naaturel.boardmateapi.services.MessageService;
|
||||
import be.naaturel.boardmateapi.services.MqttService;
|
||||
import be.naaturel.boardmateapi.services.WebexService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -19,11 +25,16 @@ public class ChatController {
|
||||
|
||||
private final MessageService messageService;
|
||||
private final WebexService webexService;
|
||||
private final MqttService mqttService;
|
||||
|
||||
@Autowired
|
||||
public ChatController(MessageService messageService, WebexService webexService){
|
||||
public ChatController(
|
||||
MessageService messageService,
|
||||
WebexService webexService,
|
||||
MqttService mqttService){
|
||||
this.messageService = messageService;
|
||||
this.webexService = webexService;
|
||||
this.mqttService = mqttService;
|
||||
}
|
||||
|
||||
@PostMapping("/message/send")
|
||||
@@ -81,4 +92,38 @@ public class ChatController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/message/webhook")
|
||||
public ResponseEntity<ResponseBody<Void>> handleWebhook(
|
||||
@RequestHeader("X-Spark-Signature") String signature,
|
||||
@RequestBody String rawPayload) {
|
||||
|
||||
Logger.displayInfo("========================");
|
||||
ResponseBody<Void> result = ResponseBody.createEmpty();
|
||||
|
||||
if (!webexService.verifySignature(rawPayload, signature)) {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result);
|
||||
}
|
||||
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
WebexWebhook payload = mapper.readValue(rawPayload, WebexWebhook.class);
|
||||
|
||||
Room room = webexService.getByRoom(payload.getData().getRoomId());
|
||||
Message msg = webexService.fetchMessage(payload.getData().getId());
|
||||
this.messageService.save(msg);
|
||||
|
||||
this.mqttService.publish("/chat/" + room.getClientId() + "/message", msg);
|
||||
|
||||
Logger.displayInfo(msg.getContent());
|
||||
Logger.displayInfo(msg.getId());
|
||||
Logger.displayInfo(msg.getClientId());
|
||||
|
||||
result.setSuccess(true);
|
||||
return ResponseEntity.ok(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
result.setMessage("Unable to handle request");
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user