Skip to content

Commit 2024711

Browse files
authored
feat(model): add card, carousel, and alert Block Kit blocks (#1624)
1 parent da4ba33 commit 2024711

9 files changed

Lines changed: 484 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.slack.api.model.block;
2+
3+
import com.slack.api.model.block.composition.TextObject;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* https://docs.slack.dev/reference/block-kit/blocks/alert-block
11+
* <p>
12+
* Displays an inline alert message. Alert blocks are currently only supported in modals.
13+
*/
14+
@Data
15+
@Builder
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
public class AlertBlock implements LayoutBlock {
19+
public static final String TYPE = "alert";
20+
private final String type = TYPE;
21+
22+
/**
23+
* The message content of the alert, as a plain_text or mrkdwn text object.
24+
* Maximum length for the text in this field is 200 characters.
25+
*/
26+
private TextObject text;
27+
28+
/**
29+
* The severity level of the alert. One of default, info, warning, error, or success.
30+
* Will be default if omitted.
31+
*/
32+
private String level;
33+
34+
/**
35+
* A string acting as a unique identifier for a block. If not specified, one will be generated.
36+
* Maximum length for this field is 255 characters.
37+
* block_id should be unique for each message and each iteration of a message.
38+
* If a message is updated, use a new block_id.
39+
*/
40+
private String blockId;
41+
}

‎slack-api-model/src/main/java/com/slack/api/model/block/Blocks.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,22 @@ public static ShareShortcutBlock shareShortcut() {
129129
return ShareShortcutBlock.builder().build();
130130
}
131131

132+
// AlertBlock
133+
134+
public static AlertBlock alert(ModelConfigurator<AlertBlock.AlertBlockBuilder> configurator) {
135+
return configurator.configure(AlertBlock.builder()).build();
136+
}
137+
138+
// CardBlock
139+
140+
public static CardBlock card(ModelConfigurator<CardBlock.CardBlockBuilder> configurator) {
141+
return configurator.configure(CardBlock.builder()).build();
142+
}
143+
144+
// CarouselBlock
145+
146+
public static CarouselBlock carousel(ModelConfigurator<CarouselBlock.CarouselBlockBuilder> configurator) {
147+
return configurator.configure(CarouselBlock.builder()).build();
148+
}
149+
132150
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.slack.api.model.block;
2+
3+
import com.slack.api.model.block.composition.SlackIconObject;
4+
import com.slack.api.model.block.composition.TextObject;
5+
import com.slack.api.model.block.element.BlockElement;
6+
import com.slack.api.model.block.element.ImageElement;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Builder;
9+
import lombok.Data;
10+
import lombok.NoArgsConstructor;
11+
12+
import java.util.List;
13+
14+
/**
15+
* A card is a layout block used to display a compact, structured summary of content. It can be used on
16+
* its own or grouped together inside a {@link CarouselBlock carousel}. At least one of {@code heroImage},
17+
* {@code title}, {@code actions}, or {@code body} must be provided.
18+
*
19+
* https://docs.slack.dev/reference/block-kit/blocks/card-block
20+
*/
21+
@Data
22+
@Builder
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
public class CardBlock implements LayoutBlock {
26+
public static final String TYPE = "card";
27+
private final String type = TYPE;
28+
29+
/**
30+
* A top banner image for the card in the form of an image element.
31+
* The image URL may be up to 3000 characters and the alt text up to 2000 characters.
32+
*/
33+
private ImageElement heroImage;
34+
35+
/**
36+
* A small icon displayed next to the title and subtitle in the form of an image element.
37+
* The image URL may be up to 3000 characters and the alt text up to 2000 characters.
38+
* Mutually exclusive with {@code slackIcon}.
39+
*/
40+
private ImageElement icon;
41+
42+
/**
43+
* A built-in Slack icon displayed next to the title and subtitle.
44+
* Mutually exclusive with {@code icon}.
45+
*/
46+
private SlackIconObject slackIcon;
47+
48+
/**
49+
* The title of the card. Maximum length for the text in this field is 150 characters.
50+
*/
51+
private TextObject title;
52+
53+
/**
54+
* The subtitle of the card. Maximum length for the text in this field is 150 characters.
55+
*/
56+
private TextObject subtitle;
57+
58+
/**
59+
* The body text of the card. Maximum length for the text in this field is 200 characters.
60+
*/
61+
private TextObject body;
62+
63+
/**
64+
* Secondary text displayed beneath the body of the card. Maximum length for the text in this field
65+
* is 200 characters.
66+
*/
67+
private TextObject subtext;
68+
69+
/**
70+
* Interactive elements (such as buttons) displayed at the bottom of the card. Up to 3 buttons.
71+
*/
72+
private List<BlockElement> actions;
73+
74+
private String blockId;
75+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.slack.api.model.block;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
10+
/**
11+
* A carousel is a layout block used to display a horizontally scrollable collection of
12+
* {@link CardBlock cards}. It must contain between 1 and 10 cards.
13+
*
14+
* https://docs.slack.dev/reference/block-kit/blocks/carousel-block
15+
*/
16+
@Data
17+
@Builder
18+
@NoArgsConstructor
19+
@AllArgsConstructor
20+
public class CarouselBlock implements LayoutBlock {
21+
public static final String TYPE = "carousel";
22+
private final String type = TYPE;
23+
24+
/**
25+
* An array of {@link CardBlock card} blocks. Must contain between 1 and 10 cards.
26+
*/
27+
private List<CardBlock> elements;
28+
29+
private String blockId;
30+
}

‎slack-api-model/src/main/java/com/slack/api/model/block/composition/BlockCompositions.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ public static FeedbackButtonObject feedbackButton(ModelConfigurator<FeedbackButt
8787
public static SlackFileObject slackFile(ModelConfigurator<SlackFileObject.SlackFileObjectBuilder> configurator) {
8888
return configurator.configure(SlackFileObject.builder()).build();
8989
}
90+
91+
// SlackIconObject
92+
93+
public static SlackIconObject slackIcon(ModelConfigurator<SlackIconObject.SlackIconObjectBuilder> configurator) {
94+
return configurator.configure(SlackIconObject.builder()).build();
95+
}
96+
97+
public static SlackIconObject slackIcon(String name) {
98+
return SlackIconObject.builder().name(name).build();
99+
}
90100
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.slack.api.model.block.composition;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* Defines a built-in Slack icon that can be displayed next to the title and subtitle of a card block.
10+
*
11+
* @see <a href="https://docs.slack.dev/reference/block-kit/composition-objects/slack-icon-object">document</a>
12+
*/
13+
@Data
14+
@Builder
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class SlackIconObject {
18+
public static final String TYPE = "icon";
19+
private final String type = TYPE;
20+
21+
/**
22+
* The name of the built-in Slack icon.
23+
*/
24+
private String name;
25+
}

‎slack-api-model/src/main/java/com/slack/api/util/json/GsonLayoutBlockFactory.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ private Class<? extends LayoutBlock> getLayoutClassInstance(String typeName) {
6161
return RichTextBlock.class;
6262
case ShareShortcutBlock.TYPE:
6363
return ShareShortcutBlock.class;
64+
case AlertBlock.TYPE:
65+
return AlertBlock.class;
66+
case CardBlock.TYPE:
67+
return CardBlock.class;
68+
case CarouselBlock.TYPE:
69+
return CarouselBlock.class;
6470
default:
6571
if (failOnUnknownProperties) {
6672
throw new JsonParseException("Unsupported layout block type: " + typeName);

0 commit comments

Comments
 (0)