Skip to content

Commit

Permalink
DRAW-275 feat: discord webhook 작업
Browse files Browse the repository at this point in the history
  • Loading branch information
SunwoongH committed Sep 5, 2024
1 parent 86de7ff commit c0d40a3
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xorker.draw.webhook.discord

import com.xorker.draw.webhook.discord.dto.DiscordMessage
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.service.annotation.PostExchange
import reactor.core.publisher.Mono

internal interface DiscordClient {
@PostExchange("/api/webhooks/{path}")
fun sendMessage(
@PathVariable("path") path: String,
@RequestBody discordMessage: DiscordMessage,
): Mono<Void>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.xorker.draw.webhook.discord

import com.xorker.draw.notify.NotifyRepository
import com.xorker.draw.notify.NotifyType
import com.xorker.draw.support.logging.logger
import com.xorker.draw.webhook.discord.config.DiscordProperties
import com.xorker.draw.webhook.discord.dto.DiscordEmbed
import com.xorker.draw.webhook.discord.dto.DiscordMessage
import org.springframework.stereotype.Component

@Component
internal class DiscordWebHookAdapter(
private val discordClient: DiscordClient,
private val discordProperties: DiscordProperties,
) : NotifyRepository {
val logger = logger()

override fun notifyMessage(notifyType: NotifyType) {
sendMessage(notifyType)
}

private fun sendMessage(notifyType: NotifyType) {
try {
when (notifyType) {
is NotifyType.DiscordRandomMatchingNotifyType ->
discordClient.sendMessage(discordProperties.randomMatchingUrl, notifyType.toDiscordMessage()).block()

is NotifyType.DiscordStartGameNotifyType ->
discordClient.sendMessage(discordProperties.startGameUrl, notifyType.toDiscordMessage()).block()
}
} catch (ex: Exception) {
logger.warn(ex.message, ex)
}
}

fun NotifyType.toDiscordMessage(): DiscordMessage = when (this) {
is NotifyType.DiscordRandomMatchingNotifyType -> DiscordMessage(listOf(DiscordEmbed(this.message)))
is NotifyType.DiscordStartGameNotifyType -> DiscordMessage(listOf(DiscordEmbed(this.message)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.xorker.draw.webhook.discord.config

import com.xorker.draw.webhook.discord.DiscordClient
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.support.WebClientAdapter
import org.springframework.web.service.invoker.HttpServiceProxyFactory

@EnableConfigurationProperties(DiscordProperties::class)
@Configuration
internal class DiscordClientConfig {

@Bean
fun webClient(): WebClient = WebClient.create("https://discord.com")

@Bean
fun discordClient(): DiscordClient {
val factory = HttpServiceProxyFactory.builder()
.exchangeAdapter(WebClientAdapter.create(webClient()))
.build()

return factory.createClient(DiscordClient::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.xorker.draw.webhook.discord.config

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "discord")
internal data class DiscordProperties(
val randomMatchingUrl: String,
val startGameUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.webhook.discord.dto

data class DiscordEmbed(
val title: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.webhook.discord.dto

data class DiscordMessage(
val embeds: List<DiscordEmbed>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
discord:
random-matching-url: ${RANDOM_MATCHING_URL}
start-game-url: ${START_GAME_URL}

0 comments on commit c0d40a3

Please sign in to comment.