https://github.com/donggeon0908/slack-sdk
easy to send slack message
https://github.com/donggeon0908/slack-sdk
kotlin message slack spring webhook
Last synced: 2 months ago
JSON representation
easy to send slack message
- Host: GitHub
- URL: https://github.com/donggeon0908/slack-sdk
- Owner: DongGeon0908
- Created: 2023-06-05T06:28:59.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-26T16:12:43.000Z (almost 3 years ago)
- Last Synced: 2025-05-04T17:43:08.701Z (about 1 year ago)
- Topics: kotlin, message, slack, spring, webhook
- Language: Kotlin
- Homepage:
- Size: 71.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Slack Client
> The service introduces the ability to deliver messages to Slack via webhooks.
### Slack Config
```yaml
slack:
webhook-url: # that's webhook url
```
### Slack Properties
```kotlin
@Validated
@Configuration
@ConfigurationProperties(prefix = "slack")
class SlackProperties {
@field:NotEmpty
var webhooks: Map = emptyMap()
fun getWebhookModel(key: String): WebhookModel {
return webhooks[key] ?: throw NotFoundSlackWebhookModelException(key)
}
}
@Validated
data class WebhookModel(
@field:NotEmpty
var url: String = ""
)
```
### Slack Client
**SlackClient**
```kotlin
interface SlackClient {
/**
* if you success to message, then return "ok"
*/
suspend fun send(model: SlackMessageModel): String
/**
* support sending multiple messages, *Use after checking for overload*
*/
suspend fun sendBulk(models: List)
}
```
**SlackMessageModel**
```kotlin
data class SlackMessageModel(
/**
* Enter a message to be sent to Slack.
*/
val text: String
)
```
### Slack Client impl
```kotlin
class SuspendableSlackClient(
private val webclient: WebClient
) : SlackClient {
override suspend fun send(webhookModel: WebhookModel, slackMessageModel: SlackMessageModel): String {
return withContext(Dispatchers.IO) {
webclient
.post()
.uri(webhookModel.url)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(slackMessageModel)
.retrieve()
.awaitBody()
}
}
override suspend fun sendBulk(webhookModel: WebhookModel, slackMessageModels: List) {
slackMessageModels.forEach { slackMessageModel ->
coroutineScope {
launch(Dispatchers.IO) { send(webhookModel, slackMessageModel) }
}
}
}
}
```
### Test Api
```kotlin
@RestController
class SlackResource(
private val slackService: SlackService,
) {
@GetMapping("/api/v1/slack")
suspend fun sendMessage() = slackService.sendMessage()
@GetMapping("/api/v1/slack-bulk")
suspend fun sendBulkMessages() = slackService.sendBulkMessages()
}
```