Implement MessageSenderService

This commit is contained in:
woozu.shin 2021-11-21 20:53:20 +09:00
parent 1505227037
commit c97c8dc01f
24 changed files with 175 additions and 74 deletions

View File

@ -11,7 +11,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
@Slf4j @Slf4j
@Profile("datasource-development") @Profile({"datasource-local", "datasource-development"})
@Configuration @Configuration
public class H2ConsoleConfiguration { public class H2ConsoleConfiguration {

View File

@ -48,8 +48,8 @@ public class CrawlAPIController {
Mono<List<PpomppuArticle>> articles = Mono<List<PpomppuArticle>> articles =
ppomppuRSSFeedService.getArticles(boardName) ppomppuRSSFeedService.getArticles(boardName)
.doOnNext(e -> ppomppuArticleService.filterOnlyNewArticles(boardName, e)) .doOnNext(e -> ppomppuArticleService.filterOnlyNewArticles(boardName, e))
.doOnNext(e -> ppomppuArticleService.save(boardName, e)) .doOnNext(e -> messageSenderService.sendMessageToSlack(e))
.doOnNext(messageSenderService::sendMessageToSlack); .doOnNext(e -> ppomppuArticleService.save(boardName, e));
return articles.then(Mono.just(APIResponse.success(result.done()))); return articles.then(Mono.just(APIResponse.success(result.done())));
} }

View File

@ -1,24 +1,26 @@
package com.myoa.engineering.crawl.ppomppu.processor.infrastructure.client; package com.myoa.engineering.crawl.ppomppu.processor.infrastructure.client;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import com.myoa.engineering.crawl.ppomppu.processor.dto.constant.WebClientPropertiesUnitName; import com.myoa.engineering.crawl.ppomppu.processor.dto.constant.WebClientPropertiesUnitName;
import com.myoa.engineering.crawl.ppomppu.support.dto.SimpleMessageDTO; import com.myoa.engineering.crawl.ppomppu.support.dto.SimpleMessageDTO;
import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebClientFilterFactory; import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebClientFilterFactory;
import com.myoa.engineering.crawl.ppomppu.support.webclient.factory.WebFluxExchangeStragiesFactory;
import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties; import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties;
import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties.WebClientPropertiesUnit; import com.myoa.engineering.crawl.ppomppu.support.webclient.properties.WebClientProperties.WebClientPropertiesUnit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
/** /**
* PpomppuNotifierSenderAPIClient * PpomppuNotifierSenderAPIClient
*
* @author Shin Woo-jin (woo-jin.shin@linecorp.com) * @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-11-17 * @since 2021-11-17
*
*/ */
@Slf4j @Slf4j
@Component @Component
@ -31,6 +33,8 @@ public class MessageSenderAPIClient {
webClientProperties.find(WebClientPropertiesUnitName.PPOMPPU_NOTIFIER_SENDER_API.getUnitName()); webClientProperties.find(WebClientPropertiesUnitName.PPOMPPU_NOTIFIER_SENDER_API.getUnitName());
this.webClient = WebClient.builder() this.webClient = WebClient.builder()
.baseUrl(webClientPropertiesUnit.getBaseUrl()) .baseUrl(webClientPropertiesUnit.getBaseUrl())
.exchangeStrategies(WebFluxExchangeStragiesFactory.ofDefault())
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.filter(WebClientFilterFactory.logRequest()) .filter(WebClientFilterFactory.logRequest())
.filter(WebClientFilterFactory.logResponse()) .filter(WebClientFilterFactory.logResponse())
.build(); .build();
@ -40,11 +44,12 @@ public class MessageSenderAPIClient {
return webClient.post() return webClient.post()
.uri("/api/v1/messages/sendMessage/messengers/slack") .uri("/api/v1/messages/sendMessage/messengers/slack")
.bodyValue(dto) .bodyValue(dto)
.exchangeToMono(e -> e.bodyToMono(String.class)) .exchangeToMono(e -> e.bodyToMono(new ParameterizedTypeReference<String>() {}))
.publishOn(Schedulers.boundedElastic()) .publishOn(Schedulers.boundedElastic())
.onErrorResume(WebClientRequestException.class, t -> { .onErrorResume(WebClientRequestException.class, t -> {
log.info("Exception occured, ignoring. : {}", t.getClass().getSimpleName()); log.info("Exception occured, ignoring. : {}", t.getClass().getSimpleName());
return Mono.empty(); return Mono.empty();
}); })
.doOnNext(e -> log.info("response: {} ", e));
} }
} }

View File

@ -9,6 +9,7 @@ import com.myoa.engineering.crawl.ppomppu.processor.dto.PpomppuArticleTransforme
import com.myoa.engineering.crawl.ppomppu.processor.infrastructure.client.MessageSenderAPIClient; import com.myoa.engineering.crawl.ppomppu.processor.infrastructure.client.MessageSenderAPIClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
/** /**
* MessageSenderService * MessageSenderService
@ -26,12 +27,12 @@ public class MessageSenderService {
this.messageSenderAPIClient = messageSenderAPIClient; this.messageSenderAPIClient = messageSenderAPIClient;
} }
public void sendMessageToSlack(PpomppuArticle article) { public Mono<String> sendMessageToSlack(PpomppuArticle article) {
messageSenderAPIClient.sendMessageToSlack(PpomppuArticleTransformer.TRANSFORM_TO_MESSAGE_DTO.apply(article)); return messageSenderAPIClient.sendMessageToSlack(PpomppuArticleTransformer.TRANSFORM_TO_MESSAGE_DTO.apply(article));
} }
public void sendMessageToSlack(List<PpomppuArticle> articles) { public Mono<String> sendMessageToSlack(List<PpomppuArticle> articles) {
messageSenderAPIClient.sendMessageToSlack(PpomppuArticleTransformer.transform(articles)); return messageSenderAPIClient.sendMessageToSlack(PpomppuArticleTransformer.transform(articles));
} }
} }

View File

@ -38,7 +38,7 @@ public class PpomppuArticleService {
} }
@Transactional @Transactional
public void save(PpomppuBoardName boardName, List<PpomppuArticle> articles) { public List<PpomppuArticle> save(PpomppuBoardName boardName, List<PpomppuArticle> articles) {
Long latestArticleId = articles.stream() Long latestArticleId = articles.stream()
.map(PpomppuArticle::getArticleId) .map(PpomppuArticle::getArticleId)
.max(Long::compareTo) .max(Long::compareTo)
@ -54,6 +54,6 @@ public class PpomppuArticleService {
latestArticleId))); latestArticleId)));
// save real articles. // save real articles.
ppomppuArticleRepository.saveAll(articles); return ppomppuArticleRepository.saveAll(articles);
} }
} }

View File

@ -0,0 +1,12 @@
spring:
config:
activate:
on-profile: local
import:
- "configserver:http://localhost:20085"
- classpath:/local/webclient.yml
server:
port: 20081
# import: optional:configserver:http://localhost:11080 # can be start up even config server was not found.

View File

@ -4,8 +4,9 @@ spring:
main: main:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
profiles: profiles:
active: ${SPRING_ACTIVE_PROFILE:development} active: ${SPRING_ACTIVE_PROFILE:local}
group: group:
local: "local,datasource-local"
development: "development,datasource-development" development: "development,datasource-development"
production: "production, datasource-production" production: "production, datasource-production"
freemarker: freemarker:

View File

@ -2,4 +2,4 @@ webclient:
init: true init: true
units: units:
- unit-name: ppn-sender-api - unit-name: ppn-sender-api
base-url: http://localhost:20081 base-url: http://localhost:20082

View File

@ -0,0 +1,5 @@
webclient:
init: true
units:
- unit-name: ppn-sender-api
base-url: http://localhost:20082

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<springProperty name="DEFAULT_LEVEL_CONFIG" source="log.defaultLevel" /> <springProperty name="DEFAULT_LEVEL_CONFIG" source="log.defaultLevel" />
<springProfile name="local">
<include resource="logback/logback-development.xml" />
<logger name="org.apache.kafka" level="INFO" />
</springProfile>
<springProfile name="development"> <springProfile name="development">
<include resource="logback/logback-development.xml" /> <include resource="logback/logback-development.xml" />
<logger name="org.apache.kafka" level="INFO" /> <logger name="org.apache.kafka" level="INFO" />

View File

@ -2,4 +2,4 @@ webclient:
init: true init: true
units: units:
- unit-name: ppn-sender-api - unit-name: ppn-sender-api
base-url: http://ppn_sender:20081 base-url: http://ppn_sender:20082

View File

@ -0,0 +1,7 @@
spring:
config:
activate:
on-profile: local
import:
- classpath:/local/webclient.yml
- "configserver:http://localhost:20085"

View File

@ -4,7 +4,7 @@ spring:
main: main:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
profiles: profiles:
active: ${SPRING_ACTIVE_PROFILE:development} active: ${SPRING_ACTIVE_PROFILE:local}
freemarker: freemarker:
enabled: false enabled: false

View File

@ -0,0 +1,5 @@
webclient:
init: true
units:
- unit-name: ppn-processor-api
base-url: http://localhost:20081

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<springProperty name="DEFAULT_LEVEL_CONFIG" source="log.defaultLevel" /> <springProperty name="DEFAULT_LEVEL_CONFIG" source="log.defaultLevel" />
<springProfile name="local">
<include resource="logback/logback-development.xml" />
<logger name="org.apache.kafka" level="INFO" />
</springProfile>
<springProfile name="development"> <springProfile name="development">
<include resource="logback/logback-development.xml" /> <include resource="logback/logback-development.xml" />
<logger name="org.apache.kafka" level="INFO" /> <logger name="org.apache.kafka" level="INFO" />

View File

@ -0,0 +1,34 @@
package com.myoa.engineering.crawl.ppomppu.sender.configuration.properties;
import java.util.List;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Getter
@Setter
@Component
@ConfigurationProperties("infra.slack.bot")
public class SlackSecretProperties {
private List<SlackSecretPropertiesUnit> units;
@Data
public static class SlackSecretPropertiesUnit {
private String botName;
private String username;
private String iconEmoji;
private String channel;
private String token;
}
public SlackSecretPropertiesUnit find(String botUnitName) {
return units.stream()
.filter(e -> e.getBotName().equals(botUnitName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("not found bot unit name : " + botUnitName));
}
}

View File

@ -1,13 +1,12 @@
package com.myoa.engineering.crawl.ppomppu.sender.controller; package com.myoa.engineering.crawl.ppomppu.sender.controller;
import com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client.MongeShoppingBotSlackMessageSender;
import com.myoa.engineering.crawl.ppomppu.support.dto.APIResponse;
import com.myoa.engineering.crawl.ppomppu.support.dto.SimpleMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.myoa.engineering.crawl.ppomppu.support.dto.APIResponse;
import com.myoa.engineering.crawl.ppomppu.support.dto.SimpleMessageDTO;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
/** /**
@ -21,11 +20,17 @@ import reactor.core.publisher.Mono;
@RequestMapping("/api/v1") @RequestMapping("/api/v1")
public class MessageSenderAPIController { public class MessageSenderAPIController {
private final MongeShoppingBotSlackMessageSender sender;
public MessageSenderAPIController(MongeShoppingBotSlackMessageSender sender) {
this.sender = sender;
}
@PostMapping("/messages/sendMessage/messengers/slack") @PostMapping("/messages/sendMessage/messengers/slack")
public Mono<APIResponse<SimpleMessageDTO>> sendMessageToSlack(SimpleMessageDTO dto) { public Mono<APIResponse<SimpleMessageDTO>> sendMessageToSlack(SimpleMessageDTO dto) {
log.info("received : {}, \nbody: {}", dto.getTitle(), dto.getBody()); log.info("received : {}, \nbody: {}", dto.getTitle(), dto.getBody());
// TODO transform return sender.sendMessage(sender.ofMessage(dto.getBody()))
return Mono.just(APIResponse.success(dto)); .then(Mono.just(APIResponse.success(dto)));
} }
} }

View File

@ -1,37 +1,29 @@
package com.myoa.engineering.crawl.ppomppu.sender.controller; package com.myoa.engineering.crawl.ppomppu.sender.controller;
import com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client.MongeShoppingBotSlackMessageSender;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackMessageDTO;
import com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client.SlackMessageSender;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
/** /**
* TestAPIController * TestAPIController
*
* @author Shin Woo-jin (woo-jin.shin@linecorp.com) * @author Shin Woo-jin (woo-jin.shin@linecorp.com)
* @since 2021-11-15 * @since 2021-11-15
*
*/ */
@RestController @RestController
@RequestMapping("/api/v1") @RequestMapping("/api/v1")
public class TestAPIController { public class TestAPIController {
private final SlackMessageSender sender; private final MongeShoppingBotSlackMessageSender sender;
public TestAPIController() { public TestAPIController(MongeShoppingBotSlackMessageSender sender) {
this.sender = new SlackMessageSender("xoxb-2688454277126-2695026012277-K2Ib13lKokmTiBSnSMrc0Bp2"); this.sender = sender;
} }
@GetMapping("/test") @GetMapping("/test")
public Mono<String> test() { public Mono<String> test() {
return sender.sendMessage(SlackMessageDTO.builder() return sender.sendMessage(sender.ofMessage("testtesttest!!!"));
.text("test!")
.iconEmoji("monge")
.channel("notify_shopping")
.username("shopping notifier")
.build());
} }
} }

View File

@ -33,4 +33,7 @@ public class SlackMessageDTO implements MessageDTO {
this.iconEmoji = iconEmoji; this.iconEmoji = iconEmoji;
} }
public void applyText(String text) {
this.text = text;
}
} }

View File

@ -0,0 +1,39 @@
package com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client;
import com.myoa.engineering.crawl.ppomppu.sender.configuration.properties.SlackSecretProperties;
import com.myoa.engineering.crawl.ppomppu.sender.configuration.properties.SlackSecretProperties.SlackSecretPropertiesUnit;
import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MongeShoppingBotSlackMessageSender extends SlackMessageSender {
private static final String SLACK_SECRET_UNIT_NAME = "monge_shopping_bot";
private final SlackSecretPropertiesUnit slackProperties;
// private final SlackMessageSender slackMessageSender;
public MongeShoppingBotSlackMessageSender(SlackSecretProperties slackSecretProperties) {
super(slackSecretProperties.find(SLACK_SECRET_UNIT_NAME).getToken());
this.slackProperties = slackSecretProperties.find(SLACK_SECRET_UNIT_NAME);
}
public SlackMessageDTO ofMessageTemplate() {
return SlackMessageDTO.builder()
.channel(slackProperties.getChannel())
.iconEmoji(slackProperties.getIconEmoji())
.username(slackProperties.getUsername())
.build();
}
public SlackMessageDTO ofMessage(String text) {
return SlackMessageDTO.builder()
.channel(slackProperties.getChannel())
.iconEmoji(slackProperties.getIconEmoji())
.username(slackProperties.getUsername())
.text(text)
.build();
}
}

View File

@ -53,34 +53,3 @@ public class SlackMessageSender implements MessageSender<SlackMessageDTO> {
} }
} }
/*
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"
*/

View File

@ -0,0 +1,10 @@
spring:
config:
activate:
on-profile: local
import:
- "configserver:http://localhost:20085"
# import: optional:configserver:http://localhost:11080 # can be start up even config server was not found.
server:
port: 20082

View File

@ -4,8 +4,9 @@ spring:
main: main:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
profiles: profiles:
active: ${SPRING_ACTIVE_PROFILE:development} active: ${SPRING_ACTIVE_PROFILE:local}
group: group:
local: local, slackapi-local
development: development, slackapi-development development: development, slackapi-development
production: production, slackapi-production production: production, slackapi-production
freemarker: freemarker:

View File

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<springProfile name="local">
<include resource="logback/logback-development.xml" />
<logger name="org.apache.kafka" level="INFO" />
</springProfile>
<springProperty name="DEFAULT_LEVEL_CONFIG" source="log.defaultLevel" /> <springProperty name="DEFAULT_LEVEL_CONFIG" source="log.defaultLevel" />
<springProfile name="development"> <springProfile name="development">
<include resource="logback/logback-development.xml" /> <include resource="logback/logback-development.xml" />