Skip to content

Commit 04e8834

Browse files
author
Evan Rittenhouse
committed
Add first draft of Kotlin DSL documentation
1 parent 69f4f18 commit 04e8834

File tree

5 files changed

+154
-1
lines changed

5 files changed

+154
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ All released versions are available on the Maven Central repositories. The lates
6161
|groupId:artifactId|Description|
6262
|---|---|
6363
|[**com.slack.api:slack-api-model**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-model)|This module is a collection of the classes representing the [Slack core objects](https://api.slack.com/types) such as conversations, messages, users, surfaces, and blocks. As this one is an essential part of the SDK, all others depend on this.|
64+
|[**com.slack.api:slack-api-model-kotlin-extension**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-model-kotlin-extension)|This contains the Block Kit Kotlin DSL builder, which allows you to define block kit structures via a Kotlin-native DSL.|
6465
|[**com.slack.api:slack-api-client**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-client)|This is a collection of the Slack API clients. The supported are Basic API Methods, RTM (Real Time Messaging) API, SCIM API, Audit Logs API, and Status API.|
66+
|[**com.slack.api:slack-api-client-kotlin-extension**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-client-kotlin-extension)|This contains extension methods for various slack client message builders so you can seamlessly use the Block Kit Kotlin DSL directly on the Java message builders.|
6567
|[**com.slack.api:slack-app-backend**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-app-backend)|This module is a set of Slack app server-side handlers and data classes for Events API, Interactive Components, Slash Commands, Actions, and OAuth flow. These are used by Bolt framework as the foundation of it in primitive layers.|
6668
|[**com.slack.api:bolt**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:bolt)|Bolt is a framework that offers an abstraction layer to build Slack apps safely and quickly. The most commonly used Servlet environment is supported out-of-the-box.|
6769
|[**com.slack.api:bolt-servlet**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:bolt-servlet)|This is an adapter for Servlet environments.|

docs/guides/composing-messages.md

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,147 @@ ChatPostMessageResponse response = slack.methods(token).chatPostMessage(req -> r
6969
);
7070
```
7171

72-
You can use this way also for building a message for Incoming Webhooks and `response_url` calls.
72+
You can use this way also for building a message for Incoming Webhooks and `response_url` calls.
73+
74+
---
75+
## Block Kit Kotlin DSL
76+
77+
If you're using Kotlin, the Java Slack SDK also provides Kotlin-style builders which you can use to build your block kit structures.
78+
79+
```kotlin
80+
import com.slack.api.model.block.Blocks.*
81+
import com.slack.api.model.kotlin_extension.request.chat.blocks
82+
83+
val response = slack.methods(token).chatPostMessage { req -> req
84+
.channel("C1234567")
85+
.blocks {
86+
section {
87+
// "text" fields can be constructed via plainText() and markdownText()
88+
markdownText("*Please select a restaurant:*")
89+
}
90+
divider()
91+
actions {
92+
// To align with the JSON structure, you could put the elements { } block around the buttons but for brevity it can be omitted
93+
// The same is true for things such as the section block's "accessory" container
94+
button {
95+
// For instances where only plain text is acceptable, the field's name can be filled with plain text inputs
96+
text("Farmhouse", emoji = true)
97+
value("v1")
98+
}
99+
button {
100+
text("Kin Khao", emoji = true)
101+
value("v2")
102+
}
103+
}
104+
}
105+
}
106+
```
107+
108+
### Installation
109+
110+
You can add the Block Kit Kotlin DSL via 2 artifacts:
111+
112+
* The slack model kotlin extension, which adds the Kotlin DSL itself as well as the standalone `withBlocks { }` builder
113+
* The slack client kotlin extension, which adds the `.blocks { }` extension function to various Slack Client builders for seamless use of the DSL with the Java builders
114+
* The `.blocks { }` extension function is available on the following Java builders:
115+
* ChatPostEphemeralRequestBuilder
116+
* ChatPostMessageRequestBuilder
117+
* ChatScheduleMessageRequestBuilder
118+
* ChatUpdateRequestBuilder
119+
120+
**Adding via Gradle:**
121+
122+
```groovy
123+
dependencies {
124+
implementation "com.slack.api:slack-api-model-kotlin-extension:{{ site.sdkLatestVersion }}"
125+
implementation "com.slack.api:slack-api-client-kotlin-extension:{{ site.sdkLatestVersion }}"
126+
}
127+
```
128+
129+
**Adding via Gradle Kotlin DSL:**
130+
131+
```kotlin
132+
dependencies {
133+
implementation("com.slack.api:slack-api-model-kotlin-extension:{{ site.sdkLatestVersion }}")
134+
implementation("com.slack.api:slack-api-client-kotlin-extension:{{ site.sdkLatestVersion }}")
135+
}
136+
```
137+
138+
### Notable examples and features
139+
140+
**Standalone withBlocks builder which comes with the model extension**:
141+
142+
```kotlin
143+
import com.slack.api.model.kotlin_extension.block.withBlocks
144+
145+
val constructedMessage = withBlocks {
146+
section {
147+
plainText("Now this can be passed to anything that requires a list of LayoutBlocks")
148+
}
149+
}
150+
```
151+
152+
**Type safe enums for inputs which require specific string inputs**:
153+
154+
```kotlin
155+
import com.slack.api.model.kotlin_extension.block.element.ButtonStyle
156+
import com.slack.api.model.kotlin_extension.block.element.ConversationType
157+
import com.slack.api.model.kotlin_extension.block.withBlocks
158+
159+
val message = withBlocks {
160+
section {
161+
plainText("Please select the person or group you would like to send a cat GIF to.")
162+
163+
// "accessory" is provided here, but it can be omitted for brevity
164+
accessory {
165+
conversationsSelect {
166+
// Or alternatively, provide strings via filter("im", "mpim") if you'd prefer
167+
filter(ConversationType.IM, ConversationType.MULTIPARTY_IM)
168+
placeholder("Where should we send the cat?")
169+
170+
confirm {
171+
title("Confirm destination")
172+
plainText("Are you sure you want to send a cat GIF to this person or group?")
173+
confirm("Yes, send it")
174+
deny("Don't send it")
175+
176+
style(ButtonStyle.PRIMARY)
177+
}
178+
}
179+
}
180+
}
181+
}
182+
```
183+
184+
**Write DSL extension functions for message templating**:
185+
186+
```kotlin
187+
import com.slack.api.model.kotlin_extension.block.ActionsBlockBuilder
188+
import com.slack.api.model.kotlin_extension.block.withBlocks
189+
190+
fun ActionsBlockBuilder.presentOptions(vararg optionNames: String, prompt: String? = null) {
191+
staticSelect {
192+
if (prompt != null) {
193+
placeholder(prompt)
194+
}
195+
196+
options {
197+
for (optionName in optionNames) {
198+
option {
199+
plainText(optionName)
200+
value(optionName.toLowerCase())
201+
}
202+
}
203+
}
204+
}
205+
}
206+
207+
val message = withBlocks {
208+
section {
209+
markdownText("Please select your favorite color.")
210+
}
211+
actions {
212+
presentOptions("Green", "Red", "Blue", "Yellow", "Orange", "Black", prompt = "Pick a color...")
213+
}
214+
}
215+
```

docs/guides/web-api-basics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ val response = slack.methods(token).chatPostMessage { it
127127
}
128128
```
129129

130+
In addition, you can check out the [Block Kit Kotlin DSL]({{ site.url | append: site.baseurl }}/guides/composing-messages#block-kit-kotlin-dsl) for a Kotlin-native way of constructing rich messages.
131+
130132
### Handle Responses
131133

132134
If you're not yet familiar with the Slack Web API response format, read the [Evaluating responses](https://api.slack.com/web#responses) guide to understand it. All Web API responses contain a JSON object, which always contains a top-level boolean property `"ok"`, indicating success or failure.

docs/guides/web-api-client-setup.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ dependencies {
134134
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
135135
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
136136
implementation("com.slack.api:slack-api-client:{{ site.sdkLatestVersion }}")
137+
138+
// Add these dependencies if you want to use the Kotlin DSL for building rich messages
139+
implementation("com.slack.api:slack-api-model-kotlin-extension:{{ site.sdkLatestVersion }}")
140+
implementation("com.slack.api:slack-api-client-kotlin-extension:{{ site.sdkLatestVersion }}")
137141
}
138142
application {
139143
mainClassName = "ExampleKt" // add "Kt" suffix for main function source file

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ All released versions are available on the Maven Central repositories. The lates
2828
|[**com.slack.api:bolt-micronaut**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:bolt-micronaut) [📖](https://oss.sonatype.org/service/local/repositories/releases/archive/com/slack/api/bolt-micronaut/{{ site.sdkLatestVersion }}/bolt-micronaut-{{ site.sdkLatestVersion }}-javadoc.jar/!/index.html#package)|This is an adapter for [Micronaut](https://micronaut.io/) to run Bolt apps on top of it.|
2929
|[**com.slack.api:bolt-helidon**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:bolt-helidon) [📖](https://oss.sonatype.org/service/local/repositories/releases/archive/com/slack/api/bolt-helidon/{{ site.sdkLatestVersion }}/bolt-helidon-{{ site.sdkLatestVersion }}-javadoc.jar/!/index.html#package)|This is an adapter for [Helidon SE](https://helidon.io/docs/latest/) to run Bolt apps on top of it.|
3030
|[**com.slack.api:slack-api-model**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-model) [📖](https://oss.sonatype.org/service/local/repositories/releases/archive/com/slack/api/slack-api-model/{{ site.sdkLatestVersion }}/slack-api-model-{{ site.sdkLatestVersion }}-javadoc.jar/!/index.html#package)|This is a collection of the classes representing the [Slack core objects](https://api.slack.com/types) such as conversations, messages, users, blocks, and surfaces. As this is an essential part of the SDK, all other modules depend on this.|
31+
|[**com.slack.api:slack-api-model-kotlin-extension**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-model-kotlin-extension)|This contains the Block Kit Kotlin DSL builder, which allows you to define block kit structures via a Kotlin-native DSL.|
3132
|[**com.slack.api:slack-api-client**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-client) [📖](https://oss.sonatype.org/service/local/repositories/releases/archive/com/slack/api/slack-api-client/{{ site.sdkLatestVersion }}/slack-api-client-{{ site.sdkLatestVersion }}-javadoc.jar/!/index.html#package)|This is a collection of the Slack API clients. The supported are Basic API Methods, RTM (Real Time Messaging) API, SCIM API, Audit Logs API, and Status API.|
33+
|[**com.slack.api:slack-api-client-kotlin-extension**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-api-client-kotlin-extension)|This contains extension methods for various slack client message builders so you can seamlessly use the Block Kit Kotlin DSL directly on the Java message builders.|
3234
|[**com.slack.api:slack-app-backend**](https://search.maven.org/search?q=g:com.slack.api%20AND%20a:slack-app-backend) [📖](https://oss.sonatype.org/service/local/repositories/releases/archive/com/slack/api/slack-app-backend/{{ site.sdkLatestVersion }}/slack-app-backend-{{ site.sdkLatestVersion }}-javadoc.jar/!/index.html#package)|This module is a set of Slack app server-side handlers and data classes for Events API, Interactive Components, Slash Commands, Actions, and OAuth flow. These are used by Bolt framework as the foundation of it in primitive layers.|
3335

3436
---

0 commit comments

Comments
 (0)