728x90
반응형
Microsoft Teams로 알림메세지를 보낼 경우 다음과 같이 구현할 수 있다.
1. TeamsWebhookService
@Service
@Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Slf4j
@RequiredArgsConstructor
public class TeamsWebhookService {
public void send(TeamsWebhookMessageDto dto) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(10000);
factory.setReadTimeout(10000);
HttpHeaders httpHeaders = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate(factory);
httpHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
HttpEntity<TeamsWebhookMessageDto> request = new HttpEntity<>(dto, httpHeaders);
try {
restTemplate.postForLocation(new URI(dto.getUrl()), request);
} catch (URISyntaxException e) {
log.error(e.getMessage());
throw new RuntimeException(e);
}
}
public List<Map<String, Object>> makeAttachments(
String title
) {
List<Map<String, Object>> attachments = List.of(
Map.of(
"contentType", "application/vnd.microsoft.card.adaptive",
"content", new Content(
List.of(
Map.of(
"type", "Container",
"items", List.of(
Map.of(
"type", "TextBlock",
"text", title,
"weight","bolder",
"size","medium"
)
)
),
Map.of(
"type", "Container",
"items", List.of(
Map.of(
"type", "FactSet",
"facts", List.of(
Map.of(
"title","title: ",
"value","value"
)
)
)
)
)
)
)
)
);
return attachments;
}
}
2. MessageDto
@Data
public class TeamsWebhookMessageDto {
private String url;
private String type = "message";
private List<Map<String, Object>> attachments;
@Data
public static class Content {
@JsonProperty("$schema")
private String schema;
private String type;
private String version ;
private List<Map<String, Object>> body;
public Content(List<Map<String, Object>> body) {
this.schema = "http://adaptivecards.io/schemas/adaptive-card.json";
this.type = "AdaptiveCard";
this.version = "1.0";
this.body = body;
}
}
public TeamsWebhookMessageDto(List<Map<String, Object>> attachments) {
this.attachments = attachments;
}
}
3. send 부분
TeamsWebhookMessageDto messageDto = new TeamsWebhookMessageDto(
teamsWebhookService.makeAttachments("title")
);
messageDto.setUrl("URL 들어가는 부분");
teamsWebhookService.send(messageDto);
- Adaptive Card Example code
Schema Explorer | Adaptive Cards
728x90
반응형
'Back-End > Spring Boot' 카테고리의 다른 글
Spring Boot | @AuthenticationPrincipal in Spring Security (0) | 2023.09.06 |
---|---|
Spring Boot | LocalDateTime timezone 세팅하기 (0) | 2023.08.29 |
Spring Boot | Java List, Map 초기값 주어진 채로 선언하기 (0) | 2023.08.24 |
Spring Boot | @SQLDelete (삭제 시 deleted 컬럼 변경) (0) | 2023.08.23 |
Spring Boot | Java ConcurrentModificationException (순회 도중 삭제하기) (0) | 2023.08.22 |