Implement MessageSenderService
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
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.RequestMapping;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -21,11 +20,17 @@ import reactor.core.publisher.Mono;
|
||||
@RequestMapping("/api/v1")
|
||||
public class MessageSenderAPIController {
|
||||
|
||||
private final MongeShoppingBotSlackMessageSender sender;
|
||||
|
||||
public MessageSenderAPIController(MongeShoppingBotSlackMessageSender sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@PostMapping("/messages/sendMessage/messengers/slack")
|
||||
public Mono<APIResponse<SimpleMessageDTO>> sendMessageToSlack(SimpleMessageDTO dto) {
|
||||
|
||||
log.info("received : {}, \nbody: {}", dto.getTitle(), dto.getBody());
|
||||
// TODO transform
|
||||
return Mono.just(APIResponse.success(dto));
|
||||
return sender.sendMessage(sender.ofMessage(dto.getBody()))
|
||||
.then(Mono.just(APIResponse.success(dto)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,29 @@
|
||||
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.RequestMapping;
|
||||
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;
|
||||
|
||||
/**
|
||||
* TestAPIController
|
||||
*
|
||||
* @author Shin Woo-jin (woo-jin.shin@linecorp.com)
|
||||
* @since 2021-11-15
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/v1")
|
||||
public class TestAPIController {
|
||||
|
||||
private final SlackMessageSender sender;
|
||||
private final MongeShoppingBotSlackMessageSender sender;
|
||||
|
||||
public TestAPIController() {
|
||||
this.sender = new SlackMessageSender("xoxb-2688454277126-2695026012277-K2Ib13lKokmTiBSnSMrc0Bp2");
|
||||
public TestAPIController(MongeShoppingBotSlackMessageSender sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public Mono<String> test() {
|
||||
return sender.sendMessage(SlackMessageDTO.builder()
|
||||
.text("test!")
|
||||
.iconEmoji("monge")
|
||||
.channel("notify_shopping")
|
||||
.username("shopping notifier")
|
||||
.build());
|
||||
return sender.sendMessage(sender.ofMessage("testtesttest!!!"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,7 @@ public class SlackMessageDTO implements MessageDTO {
|
||||
this.iconEmoji = iconEmoji;
|
||||
}
|
||||
|
||||
public void applyText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
*/
|
||||
10
sender/src/main/resources/application-local.yml
Normal file
10
sender/src/main/resources/application-local.yml
Normal 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
|
||||
@@ -4,8 +4,9 @@ spring:
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
profiles:
|
||||
active: ${SPRING_ACTIVE_PROFILE:development}
|
||||
active: ${SPRING_ACTIVE_PROFILE:local}
|
||||
group:
|
||||
local: local, slackapi-local
|
||||
development: development, slackapi-development
|
||||
production: production, slackapi-production
|
||||
freemarker:
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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" />
|
||||
<springProfile name="development">
|
||||
<include resource="logback/logback-development.xml" />
|
||||
|
||||
Reference in New Issue
Block a user