[NO-ISSUE] Add slack sdk

This commit is contained in:
woozu-shin 2024-05-10 17:23:05 +09:00
parent 4d7bf4fb3c
commit e8ec96ed27
19 changed files with 68 additions and 33 deletions

View File

@ -22,6 +22,7 @@ dependencies {
implementation "org.springframework.cloud:spring-cloud-starter-openfeign" implementation "org.springframework.cloud:spring-cloud-starter-openfeign"
implementation "io.github.openfeign:feign-hc5" implementation "io.github.openfeign:feign-hc5"
implementation 'org.ahocorasick:ahocorasick:0.6.3' implementation 'org.ahocorasick:ahocorasick:0.6.3'
implementation "com.slack.api:slack-api-client:1.39.1"
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'

View File

@ -0,0 +1,20 @@
package com.myoa.engineering.crawl.shopping.configuration.slack;
import com.myoa.engineering.crawl.shopping.configuration.slack.properties.SlackSecretProperties;
import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SlackConfiguration {
private static final String DEFAULT_BOT_UNIT_NAME = "shopping-crawler";
@Bean
public MethodsClient slackMethodsClient(SlackSecretProperties slackSecretProperties) {
String token = slackSecretProperties.find(DEFAULT_BOT_UNIT_NAME).getToken();
return Slack.getInstance().methods(token);
}
}

View File

@ -12,7 +12,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity @Entity
@Table @Table(name = "app_user")
public class AppUser extends Auditable { public class AppUser extends Auditable {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@ -15,7 +15,7 @@ import java.time.ZonedDateTime;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity @Entity
@Table @Table(name = "article")
public class Article extends Auditable { public class Article extends Auditable {
@Id @Id

View File

@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity @Entity
@Table @Table(name = "subscribed_keyword")
public class SubscribedKeyword extends Auditable { public class SubscribedKeyword extends Auditable {
@Id @Id

View File

@ -4,6 +4,7 @@ import com.myoa.engineering.crawl.shopping.domain.model.v2.ArticleModel;
import lombok.*; import lombok.*;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@ToString @ToString
@ -12,13 +13,30 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class UserNotifyModel { public class UserNotifyModel {
private String userId; private String slackId;
private List<ArticleModel> articles; private List<ArticleModel> articles;
public static UserNotifyModel of(String userId, List<ArticleModel> articles) { public static UserNotifyModel of(String slackId, List<ArticleModel> articles) {
return UserNotifyModel.builder() return UserNotifyModel.builder()
.userId(userId) .slackId(slackId)
.articles(articles) .articles(articles)
.build(); .build();
} }
public String toCompositedMessage() {
String compositedMessage = wrapUserId() + "\n" +
articles.stream()
.map(this::convertArticletoMessage)
.collect(Collectors.joining("\n"));
// article -> builder.append(convertArticletoMessage.toMessage()).append("\n");i
return compositedMessage;
}
private String wrapUserId() {
return "<@" + slackId + ">";
}
private String convertArticletoMessage(ArticleModel article) {
return "<" + article.getArticleUrl() + "|" + article.getTitle() + ">";
}
} }

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto; package com.myoa.engineering.crawl.shopping.dto.feed.v1;
import com.myoa.engineering.crawl.shopping.support.dto.constant.PpomppuBoardName; import com.myoa.engineering.crawl.shopping.support.dto.constant.PpomppuBoardName;
import lombok.Builder; import lombok.Builder;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto; package com.myoa.engineering.crawl.shopping.dto.feed.v1;
import com.myoa.engineering.crawl.shopping.domain.entity.v1.PpomppuArticle; import com.myoa.engineering.crawl.shopping.domain.entity.v1.PpomppuArticle;
import com.myoa.engineering.crawl.shopping.support.util.DateUtil; import com.myoa.engineering.crawl.shopping.support.util.DateUtil;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto; package com.myoa.engineering.crawl.shopping.dto.feed.v1;
import com.myoa.engineering.crawl.shopping.domain.entity.v1.PpomppuArticle; import com.myoa.engineering.crawl.shopping.domain.entity.v1.PpomppuArticle;
import com.myoa.engineering.crawl.shopping.support.dto.BlockMessageDTO; import com.myoa.engineering.crawl.shopping.support.dto.BlockMessageDTO;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;

View File

@ -1,4 +1,4 @@
package com.myoa.engineering.crawl.shopping.dto.slack; package com.myoa.engineering.crawl.shopping.dto.slack.v1;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder; import lombok.Builder;

View File

@ -45,7 +45,7 @@ public class ArticleUpsertEventListener {
.map(user -> { .map(user -> {
List<ArticleModel> filteredArticles = handleAhoCorasick(articleMap) List<ArticleModel> filteredArticles = handleAhoCorasick(articleMap)
.apply(subscribedKeywordCacheService.getSubscribedKeywordsCached(user.getName())); .apply(subscribedKeywordCacheService.getSubscribedKeywordsCached(user.getName()));
return UserNotifyModel.of(user.getName(), filteredArticles); return UserNotifyModel.of(user.getSlackId(), filteredArticles);
}) })
.forEach(this::notifyMessage); .forEach(this::notifyMessage);
@ -53,16 +53,12 @@ public class ArticleUpsertEventListener {
private Function<Map<CrawlTarget, SubscribedKeywordAggregatedModel>, List<ArticleModel>> handleAhoCorasick( private Function<Map<CrawlTarget, SubscribedKeywordAggregatedModel>, List<ArticleModel>> handleAhoCorasick(
Map<CrawlTarget, List<ArticleModel>> articleMap) { Map<CrawlTarget, List<ArticleModel>> articleMap) {
return userTrieModel -> { return userTrieModel -> userTrieModel
return userTrieModel .entrySet()
.entrySet() .stream().filter(e -> articleMap.containsKey(e.getKey()))
.stream().filter(e -> articleMap.containsKey(e.getKey())) .map((entry) -> filterAhocorasick(articleMap.get(entry.getKey()), entry.getValue()))
.map((entry) -> filterAhocorasick(articleMap.get(entry.getKey()), entry.getValue())) .flatMap(List::stream)
.flatMap(List::stream) .toList();
.toList();
// return UserNotifyModel.of(userTrieModel.values().stream().findFirst().get().getUserId(),
// filteredArticle);
};
} }
private List<ArticleModel> filterAhocorasick(List<ArticleModel> articles, private List<ArticleModel> filterAhocorasick(List<ArticleModel> articles,
@ -75,12 +71,12 @@ public class ArticleUpsertEventListener {
//ArticleUpsertEventListener::printArticle //ArticleUpsertEventListener::printArticle
} }
private void notifyMessage(UserNotifyModel article) { private void notifyMessage(UserNotifyModel userNotifyModel) {
System.out.println("article = " + article); System.out.println("article = " + userNotifyModel);
if (article.getArticles().isEmpty()){ if (userNotifyModel.getArticles().isEmpty()) {
return; return;
} }
userNotifyService.notify("안녕 " + article.getUserId() + "\n" + article.getArticles()); userNotifyService.notify(userNotifyModel.toCompositedMessage());
} }
} }

View File

@ -1,7 +1,7 @@
package com.myoa.engineering.crawl.shopping.infra.client.slack; package com.myoa.engineering.crawl.shopping.infra.client.slack;
import com.myoa.engineering.crawl.shopping.configuration.FeignDefaultConfig; import com.myoa.engineering.crawl.shopping.configuration.FeignDefaultConfig;
import com.myoa.engineering.crawl.shopping.dto.slack.SlackMessageDTO; import com.myoa.engineering.crawl.shopping.dto.slack.v1.SlackMessageDTO;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;

View File

@ -1,7 +1,7 @@
package com.myoa.engineering.crawl.shopping.service; package com.myoa.engineering.crawl.shopping.service;
import com.myoa.engineering.crawl.shopping.configuration.slack.properties.SlackSecretProperties; import com.myoa.engineering.crawl.shopping.configuration.slack.properties.SlackSecretProperties;
import com.myoa.engineering.crawl.shopping.dto.slack.SlackMessageDTO; import com.myoa.engineering.crawl.shopping.dto.slack.v1.SlackMessageDTO;
import com.myoa.engineering.crawl.shopping.infra.client.slack.SlackAPIClient; import com.myoa.engineering.crawl.shopping.infra.client.slack.SlackAPIClient;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;