88 lines
3.1 KiB
Java
88 lines
3.1 KiB
Java
package com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client;
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
import org.springframework.web.reactive.function.client.WebClientRequestException;
|
|
|
|
import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackMessageDTO;
|
|
import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebClientFilterFactory;
|
|
import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebFluxExchangeStragiesFactory;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.scheduler.Schedulers;
|
|
|
|
/**
|
|
* SlackMessageSender
|
|
*
|
|
* @author Shin Woo-jin (woozu.shin@kakaoent.com)
|
|
* @since 2021-09-08
|
|
*/
|
|
@Slf4j
|
|
public class SlackMessageSender implements MessageSender<SlackMessageDTO> {
|
|
|
|
private static final String SLACK_API_URL = "https://slack.com/api";
|
|
|
|
private final WebClient webClient;
|
|
private final String apiSecret;
|
|
|
|
public SlackMessageSender(String apiSecret) {
|
|
this.webClient = WebClient.builder()
|
|
.baseUrl(SLACK_API_URL)
|
|
.defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiSecret)
|
|
.exchangeStrategies(WebFluxExchangeStragiesFactory.ofDefault())
|
|
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
.defaultHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8")
|
|
.filter(WebClientFilterFactory.logRequest())
|
|
.filter(WebClientFilterFactory.logResponse())
|
|
.build();
|
|
this.apiSecret = apiSecret;
|
|
}
|
|
|
|
@Override
|
|
public Mono<String> sendMessage(SlackMessageDTO message) {
|
|
return webClient.post()
|
|
.uri("/chat.postMessage")
|
|
.bodyValue(message)
|
|
.exchangeToMono(e -> e.bodyToMono(String.class))
|
|
.publishOn(Schedulers.boundedElastic())
|
|
.onErrorResume(WebClientRequestException.class, t -> {
|
|
log.info("Exception occured, ignoring. : {}", t.getClass().getSimpleName());
|
|
return Mono.empty();
|
|
})
|
|
.doOnNext(e -> log.info("response: {}", e));
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
import requests
|
|
import json
|
|
|
|
# woozuyeni_watchtower
|
|
SLACK_TOKEN="xoxb-2688454277126-2695026012277-K2Ib13lKokmTiBSnSMrc0Bp2"
|
|
CHANNEL = "notify_watchtower"
|
|
CHANNEL = "common"
|
|
|
|
|
|
POST_API = "https://slack.com/api/chat.postMessage"
|
|
HEADER = "Authorization: Bearer"
|
|
|
|
headers = {
|
|
"Content-Type" : "application/json",
|
|
"Authorization" : f"Bearer {SLACK_TOKEN}"
|
|
}
|
|
|
|
data = {}
|
|
data["text"] = "몽몽! :monge_big:"
|
|
data["channel"] = CHANNEL
|
|
data["username"] = "몽이봇"
|
|
data["icon_emoji"] = ":monge_big:"
|
|
|
|
# requests.post(POST_API, data=json.dumps(data), headers=headers)
|
|
|
|
|
|
attachments = [{"title": "Cat", "image_url": image_url}]
|
|
image_url = "https://myoa-universe.com/assets/love1.png"
|
|
*/ |