An open API service indexing awesome lists of open source software.

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

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()
}
```