Skip to content

Commit 75c61ca

Browse files
authored
feat: add task card block example (#72)
1 parent 6d3329c commit 75c61ca

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎block-kit/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Read the [docs](https://docs.slack.dev/block-kit/) to learn concepts behind thes
2121
- **[Input](https://docs.slack.dev/reference/block-kit/blocks/input-block)**: Collects information from users via elements. [Implementation](./src/main/java/blocks/Input.java).
2222
- **[Markdown](https://docs.slack.dev/reference/block-kit/blocks/markdown-block)**: Displays formatted markdown. [Implementation](./src/main/java/blocks/Markdown.java).
2323
- **[Section](https://docs.slack.dev/reference/block-kit/blocks/section-block)**: Displays text, possibly alongside elements. [Implementation](./src/main/java/blocks/Section.java).
24+
- **[Task card](https://docs.slack.dev/reference/block-kit/blocks/task-card-block)**: Displays a single task, representing a single action. [Implementation](./src/main/java/blocks/TaskCard.java).
2425
- **[Video](https://docs.slack.dev/reference/block-kit/blocks/video-block)**: Displays an embedded video player. [Implementation](./src/main/java/blocks/Video.java).
2526

2627
### Block elements
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package blocks;
2+
3+
import com.slack.api.model.block.Blocks;
4+
import com.slack.api.model.block.RichTextBlock;
5+
import com.slack.api.model.block.TaskCardBlock;
6+
import com.slack.api.model.block.element.RichTextSectionElement;
7+
import com.slack.api.model.block.element.UrlSourceElement;
8+
import java.util.List;
9+
10+
/**
11+
* Displays a single task, representing a single action.
12+
* {@link https://docs.slack.dev/reference/block-kit/blocks/task-card-block/}
13+
*/
14+
public class TaskCard {
15+
/**
16+
* A task card with output and sources.
17+
*/
18+
public static TaskCardBlock example01() {
19+
TaskCardBlock block = Blocks.taskCard(t -> t.taskId("task_1")
20+
.title("Fetching weather data")
21+
.status("in_progress")
22+
.output(RichTextBlock.builder()
23+
.elements(List.of(RichTextSectionElement.builder()
24+
.elements(List.of(RichTextSectionElement.Text.builder()
25+
.text("Found weather data for Chicago from 2 sources")
26+
.build()))
27+
.build()))
28+
.build())
29+
.sources(List.of(
30+
UrlSourceElement.builder()
31+
.url("https://weather.com/")
32+
.text("weather.com")
33+
.build(),
34+
UrlSourceElement.builder()
35+
.url("https://www.accuweather.com/")
36+
.text("accuweather.com")
37+
.build())));
38+
return block;
39+
}
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package blocks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.google.gson.JsonParser;
6+
import com.slack.api.model.block.TaskCardBlock;
7+
import com.slack.api.util.json.GsonFactory;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class TaskCardTest {
11+
@Test
12+
public void testExample01() {
13+
TaskCardBlock block = TaskCard.example01();
14+
String actual = GsonFactory.createSnakeCase().toJson(block);
15+
String expected = """
16+
{
17+
"type": "task_card",
18+
"task_id": "task_1",
19+
"title": "Fetching weather data",
20+
"status": "in_progress",
21+
"output": {
22+
"type": "rich_text",
23+
"elements": [
24+
{
25+
"type": "rich_text_section",
26+
"elements": [
27+
{
28+
"type": "text",
29+
"text": "Found weather data for Chicago from 2 sources"
30+
}
31+
]
32+
}
33+
]
34+
},
35+
"sources": [
36+
{
37+
"type": "url",
38+
"url": "https://weather.com/",
39+
"text": "weather.com"
40+
},
41+
{
42+
"type": "url",
43+
"url": "https://www.accuweather.com/",
44+
"text": "accuweather.com"
45+
}
46+
]
47+
}
48+
""";
49+
assertEquals(JsonParser.parseString(expected), JsonParser.parseString(actual));
50+
}
51+
}

0 commit comments

Comments
 (0)