Skip to content

Commit 610d528

Browse files
authored
feat: add markdown_text to chat.{postEphemeral,postMessage,scheduleMessage,update} api methods (#1531)
1 parent b08ba12 commit 610d528

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

slack-api-client/src/main/java/com/slack/api/methods/RequestFormBuilder.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ public static FormBody.Builder toForm(ChatScheduleMessageRequest req) {
15001500
warnIfEitherTextOrAttachmentFallbackIsMissing(
15011501
"chat.scheduleMessage",
15021502
req.getText(),
1503+
req.getMarkdownText(),
15031504
req.getAttachments(),
15041505
req.getAttachmentsAsString());
15051506
FormBody.Builder form = new FormBody.Builder();
@@ -1531,6 +1532,7 @@ public static FormBody.Builder toForm(ChatScheduleMessageRequest req) {
15311532
form.add("attachments", json);
15321533
}
15331534
setIfNotNull("link_names", req.isLinkNames(), form);
1535+
setIfNotNull("markdown_text", req.getMarkdownText(), form);
15341536
setIfNotNull("parse", req.getParse(), form);
15351537
setIfNotNull("reply_broadcast", req.isReplyBroadcast(), form);
15361538
setIfNotNull("thread_ts", req.getThreadTs(), form);
@@ -1554,6 +1556,7 @@ public static FormBody.Builder toForm(ChatPostEphemeralRequest req) {
15541556
warnIfEitherTextOrAttachmentFallbackIsMissing(
15551557
"chat.postEphemeral",
15561558
req.getText(),
1559+
req.getMarkdownText(),
15571560
req.getAttachments(),
15581561
req.getAttachmentsAsString());
15591562
FormBody.Builder form = new FormBody.Builder();
@@ -1583,6 +1586,7 @@ public static FormBody.Builder toForm(ChatPostEphemeralRequest req) {
15831586
setIfNotNull("icon_url", req.getIconUrl(), form);
15841587
setIfNotNull("username", req.getUsername(), form);
15851588
setIfNotNull("link_names", req.isLinkNames(), form);
1589+
setIfNotNull("markdown_text", req.getMarkdownText(), form);
15861590
setIfNotNull("parse", req.getParse(), form);
15871591
return form;
15881592
}
@@ -1591,6 +1595,7 @@ public static FormBody.Builder toForm(ChatPostMessageRequest req) {
15911595
warnIfEitherTextOrAttachmentFallbackIsMissing(
15921596
"chat.postMessage",
15931597
req.getText(),
1598+
req.getMarkdownText(),
15941599
req.getAttachments(),
15951600
req.getAttachmentsAsString());
15961601
FormBody.Builder form = new FormBody.Builder();
@@ -1599,6 +1604,7 @@ public static FormBody.Builder toForm(ChatPostMessageRequest req) {
15991604
setIfNotNull("text", req.getText(), form);
16001605
setIfNotNull("parse", req.getParse(), form);
16011606
setIfNotNull("link_names", req.isLinkNames(), form);
1607+
setIfNotNull("markdown_text", req.getMarkdownText(), form);
16021608
setIfNotNull("mrkdwn", req.isMrkdwn(), form);
16031609

16041610
if (req.getMetadataAsString() != null) {
@@ -1702,11 +1708,13 @@ public static FormBody.Builder toForm(ChatUpdateRequest req) {
17021708
warnIfEitherTextOrAttachmentFallbackIsMissing(
17031709
"chat.update",
17041710
req.getText(),
1711+
req.getMarkdownText(),
17051712
req.getAttachments(),
17061713
req.getAttachmentsAsString());
17071714
FormBody.Builder form = new FormBody.Builder();
17081715
setIfNotNull("ts", req.getTs(), form);
17091716
setIfNotNull("channel", req.getChannel(), form);
1717+
setIfNotNull("markdown_text", req.getMarkdownText(), form);
17101718
setIfNotNull("text", req.getText(), form);
17111719
setIfNotNull("parse", req.getParse(), form);
17121720
setIfNotNull("link_names", req.isLinkNames(), form);
@@ -3153,6 +3161,7 @@ private static void warnIfAttachmentWithoutFallbackDetected(String endpointName,
31533161
private static void warnIfEitherTextOrAttachmentFallbackIsMissing(
31543162
String endpointName,
31553163
String text,
3164+
String markdownText,
31563165
List<Attachment> attachments,
31573166
String attachmentsAsString) {
31583167

@@ -3165,8 +3174,8 @@ private static void warnIfEitherTextOrAttachmentFallbackIsMissing(
31653174
endpointName,
31663175
Arrays.asList(GSON.fromJson(attachmentsAsString, Attachment[].class)));
31673176
} else {
3168-
// when attachments do not exist, the top-level text is always required
3169-
if (text == null || text.trim().isEmpty()) {
3177+
// when attachments do not exist, the top-level text or markdown_text is always required
3178+
if ((text == null || text.trim().isEmpty()) && (markdownText == null || markdownText.trim().isEmpty())) {
31703179
log.warn(TEXT_WARN_MESSAGE_TEMPLATE, endpointName);
31713180
}
31723181
}

slack-api-client/src/main/java/com/slack/api/methods/request/chat/ChatPostEphemeralRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,14 @@ public void setAsUser(Boolean asUser) {
112112
*/
113113
private boolean linkNames;
114114

115+
/**
116+
* Accepts message text formatted in markdown. This argument should not be used in conjunction with blocks or text. Limit this field to 12,000 characters.
117+
*/
118+
private String markdownText;
119+
115120
/**
116121
* Change how messages are treated. Defaults to `none`. See [below](#formatting).
117122
*/
118123
private String parse;
119124

120-
}
125+
}

slack-api-client/src/main/java/com/slack/api/methods/request/chat/ChatPostMessageRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public class ChatPostMessageRequest implements SlackApiRequest {
5353
*/
5454
private boolean linkNames;
5555

56+
/**
57+
* Accepts message text formatted in markdown. This argument should not be used in conjunction with blocks or text. Limit this field to 12,000 characters.
58+
*/
59+
private String markdownText;
60+
5661
/**
5762
* JSON object with event_type and event_payload fields, presented as a URL-encoded string.
5863
* Metadata you post to Slack is accessible to any app or user who is a member of that workspace.

slack-api-client/src/main/java/com/slack/api/methods/request/chat/ChatScheduleMessageRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public void setAsUser(Boolean asUser) {
9191
*/
9292
private boolean linkNames;
9393

94+
/**
95+
* Accepts message text formatted in markdown. This argument should not be used in conjunction with blocks or text. Limit this field to 12,000 characters.
96+
*/
97+
private String markdownText;
98+
9499
/**
95100
* Change how messages are treated. Defaults to none. See below.
96101
*/
@@ -118,4 +123,4 @@ public void setAsUser(Boolean asUser) {
118123
*/
119124
private boolean unfurlMedia;
120125

121-
}
126+
}

slack-api-client/src/main/java/com/slack/api/methods/request/chat/ChatUpdateRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public void setAsUser(Boolean asUser) {
7777
this.asUser = asUser;
7878
}
7979

80+
/**
81+
* Accepts message text formatted in markdown. This argument should not be used in conjunction with blocks or text. Limit this field to 12,000 characters.
82+
*/
83+
private String markdownText;
84+
8085
/**
8186
* JSON object with event_type and event_payload fields, presented as a URL-encoded string.
8287
* Metadata you post to Slack is accessible to any app or user who is a member of that workspace.
@@ -124,4 +129,4 @@ public void setAsUser(Boolean asUser) {
124129
*/
125130
private String parse;
126131

127-
}
132+
}

slack-api-client/src/test/java/test_with_remote_apis/methods/chat_Test.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ public void postMessage_user() throws Exception {
201201
assertThat(scopes, is(notNullValue()));
202202
}
203203

204+
@Test
205+
public void postMessage_markdownText() throws Exception {
206+
loadRandomChannelId();
207+
ChatPostMessageResponse response = slack.methods(botToken).chatPostMessage(req -> req
208+
.channel(randomChannelId)
209+
.markdownText("**bold**"));
210+
assertThat(response.getError(), is(nullValue()));
211+
assertThat(response.getMessage().getText(), is("*bold*"));
212+
}
213+
204214
// https://github.com/slackapi/java-slack-sdk/issues/157
205215
@Test
206216
public void postMessage_blocksInAttachment_do_not_work_bot() throws Exception {
@@ -781,6 +791,19 @@ public void unfurl_issue_399_flickr_example() throws Exception {
781791
assertThat(unfurlResponse.getError(), is(nullValue()));
782792
}
783793

794+
@Test
795+
public void chatUpdate_markdownText() throws IOException, SlackApiException {
796+
loadRandomChannelId();
797+
ChatPostMessageResponse creation = slack.methods(botToken)
798+
.chatPostMessage(r -> r.channel(randomChannelId).text("plain"));
799+
assertThat(creation.getError(), is(nullValue()));
800+
assertThat(creation.getMessage().getText(), is("plain"));
801+
ChatUpdateResponse modified = slack.methods(botToken)
802+
.chatUpdate(r -> r.channel(randomChannelId).markdownText("**bold**").ts(creation.getTs()));
803+
assertThat(modified.getError(), is(nullValue()));
804+
assertThat(modified.getMessage().getText(), is("*bold*"));
805+
}
806+
784807
@Test
785808
public void chatUpdateWithBotToken_issue_372() throws IOException, SlackApiException {
786809
loadRandomChannelId();
@@ -988,6 +1011,17 @@ public void postEphemeral_authorship() throws Exception {
9881011
assertThat(response.getError(), is(nullValue()));
9891012
}
9901013

1014+
@Test
1015+
public void postEphemeral_markdownText() throws Exception {
1016+
loadRandomChannelId();
1017+
String userId = findUser();
1018+
ChatPostEphemeralResponse response = slack.methods(botToken).chatPostEphemeral(r -> r
1019+
.user(userId)
1020+
.channel(randomChannelId)
1021+
.markdownText("**bold**"));
1022+
assertThat(response.getError(), is(nullValue()));
1023+
}
1024+
9911025
private String findUser() throws IOException, SlackApiException {
9921026

9931027
String userId = null;
@@ -1043,10 +1077,15 @@ public void scheduleMessages() throws IOException, SlackApiException {
10431077
.blocks(blocks));
10441078
assertNull(message3.getError());
10451079

1080+
ChatScheduleMessageResponse message4 = slack.methods(botToken)
1081+
.chatScheduleMessage(r -> r.channel(randomChannelId).postAt(postAt)
1082+
.markdownText("**bold**"));
1083+
assertNull(message4.getError());
1084+
10461085
ChatScheduledMessagesListResponse after = slack.methods(botToken)
10471086
.chatScheduledMessagesList(r -> r.limit(100));
10481087
assertNull(after.getError());
1049-
assertTrue(after.getScheduledMessages().size() - before.getScheduledMessages().size() == 2);
1088+
assertTrue(after.getScheduledMessages().size() - before.getScheduledMessages().size() == 3);
10501089
}
10511090

10521091
@Test

0 commit comments

Comments
 (0)