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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ public class MessageDto {
|
||||
|
||||
private String content;
|
||||
|
||||
private int timestamp;
|
||||
private long timestamp;
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
@@ -15,11 +15,11 @@ public class MessageDto {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public int getTimestamp() {
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(int timestamp) {
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package be.naaturel.boardmateapi.controllers.dtos;
|
||||
|
||||
public class WebexWebhook {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String targetUrl;
|
||||
private String resource;
|
||||
private String event;
|
||||
private String orgId;
|
||||
private String createdBy;
|
||||
private String appId;
|
||||
private String ownedBy;
|
||||
private String status;
|
||||
private String created;
|
||||
private String actorId;
|
||||
|
||||
private WebexData data;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public String getTargetUrl() { return targetUrl; }
|
||||
public void setTargetUrl(String targetUrl) { this.targetUrl = targetUrl; }
|
||||
|
||||
public String getResource() { return resource; }
|
||||
public void setResource(String resource) { this.resource = resource; }
|
||||
|
||||
public String getEvent() { return event; }
|
||||
public void setEvent(String event) { this.event = event; }
|
||||
|
||||
public String getOrgId() { return orgId; }
|
||||
public void setOrgId(String orgId) { this.orgId = orgId; }
|
||||
|
||||
public String getCreatedBy() { return createdBy; }
|
||||
public void setCreatedBy(String createdBy) { this.createdBy = createdBy; }
|
||||
|
||||
public String getAppId() { return appId; }
|
||||
public void setAppId(String appId) { this.appId = appId; }
|
||||
|
||||
public String getOwnedBy() { return ownedBy; }
|
||||
public void setOwnedBy(String ownedBy) { this.ownedBy = ownedBy; }
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
|
||||
public String getCreated() { return created; }
|
||||
public void setCreated(String created) { this.created = created; }
|
||||
|
||||
public String getActorId() { return actorId; }
|
||||
public void setActorId(String actorId) { this.actorId = actorId; }
|
||||
|
||||
public WebexData getData() { return data; }
|
||||
public void setData(WebexData data) { this.data = data; }
|
||||
|
||||
public static class WebexData {
|
||||
private String id;
|
||||
private String roomId;
|
||||
private String roomType;
|
||||
private String personId;
|
||||
private String personEmail;
|
||||
private String created;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
|
||||
public String getRoomId() { return roomId; }
|
||||
public void setRoomId(String roomId) { this.roomId = roomId; }
|
||||
|
||||
public String getRoomType() { return roomType; }
|
||||
public void setRoomType(String roomType) { this.roomType = roomType; }
|
||||
|
||||
public String getPersonId() { return personId; }
|
||||
public void setPersonId(String personId) { this.personId = personId; }
|
||||
|
||||
public String getPersonEmail() { return personEmail; }
|
||||
public void setPersonEmail(String personEmail) { this.personEmail = personEmail; }
|
||||
|
||||
public String getCreated() { return created; }
|
||||
public void setCreated(String created) { this.created = created; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user