Skip to content

Commit

Permalink
Merge pull request #29 from TeamMiso/feat/discord-setting
Browse files Browse the repository at this point in the history
๐Ÿ”€ :: Discord ์„ธํŒ…
  • Loading branch information
uuuuuuuk authored Mar 12, 2024
2 parents aa274d0 + e9e3490 commit a50e8e0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package andreas311.miso.domain.inquiry.application.port.output

interface DiscordMessageSendPort {
fun sendDiscordMessage(message: String)

fun toSingleDiscordMessage(string: String): String

fun createInquiryMessage(inquiryName: String): String
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package andreas311.miso.global.config
package andreas311.miso.global.querydsl

import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.context.annotation.Bean
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/andreas311/miso/global/webhook/WebHookConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package andreas311.miso.global.webhook

import okhttp3.OkHttpClient
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class WebHookConfig {
private val httpClient = OkHttpClient()

@Bean
fun httpClient() = httpClient
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package andreas311.miso.thirdparty.discord

import andreas311.miso.domain.inquiry.application.port.output.DiscordMessageSendPort
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
class DiscordMessageSendAdapter(
private val okHttpClient: OkHttpClient
): DiscordMessageSendPort {

@Value("\${discord.webhook.url}")
private lateinit var discordWebhookUrl: String

private val log = LoggerFactory.getLogger(this.javaClass.simpleName)

override fun sendDiscordMessage(message: String) {

val requestBody = message.toRequestBody("application/json; charset=utf-8".toMediaType())

val request = Request.Builder()
.url(discordWebhookUrl)
.post(requestBody)
.build()

okHttpClient.newCall(request).execute().use { response ->
if (response.isSuccessful) {
log.trace("๋ฌธ์˜๋ด‡์ด ๋ฌธ์˜์‚ฌํ•ญ ์š”์ฒญ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ „๋‹ฌํ–ˆ์–ด์š”!")
} else {
log.error(response.body?.string())
log.error("๋ฌธ์˜๋ด‡์ด ๋ฌธ์˜์‚ฌํ•ญ ์š”์ฒญ ๋Œ€์‹  ${response.code} ์ฝ”๋“œ๋ฅผ ๋ณด๋ƒˆ์–ด์š”!")
}
}
}

override fun toSingleDiscordMessage(string: String): String {
return """
{
"content":"$string"
}
""".trimIndent()
}

override fun createInquiryMessage(inquiryName: String): String {
return """
{
"content": "๋ฌธ์˜์‚ฌํ•ญ ์š”์ฒญ์ด ๋“ค์–ด์™”์–ด์š”!",
"embeds": [
{
"title": "๋ฌธ์˜์‚ฌํ•ญ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”!",
"color": 5725911,
"fields": [
{
"name": "๋ฌธ์˜์‚ฌํ•ญ ์ œ๋ชฉ",
"value": "$inquiryName",
"inline": true
}
]
}
],
"attachments": []
}
""".trimIndent()
}
}

0 comments on commit a50e8e0

Please sign in to comment.