[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 "io.github.openfeign:feign-hc5"
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.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
@AllArgsConstructor
@Entity
@Table
@Table(name = "app_user")
public class AppUser extends Auditable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import com.myoa.engineering.crawl.shopping.domain.model.v2.ArticleModel;
import lombok.*;
import java.util.List;
import java.util.stream.Collectors;
@ToString
@ -12,13 +13,30 @@ import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
public class UserNotifyModel {
private String userId;
private String slackId;
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()
.userId(userId)
.slackId(slackId)
.articles(articles)
.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 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.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.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;

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;

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.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;

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.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.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 lombok.Builder;

View File

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

View File

@ -1,7 +1,7 @@
package com.myoa.engineering.crawl.shopping.service;
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 org.springframework.stereotype.Service;