Skip to content

Slack message empty text #31596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public class SlackMessage implements MessageElement {
final String text;
final Attachment[] attachments;

//Note on constructors:
// Slack with fail to send a message if it has no attachments AND no text.
// Slack will succeed with attachments OR text.
public SlackMessage(String from, String[] to, String icon, Attachment[] attachments) {
this(from, to, icon, null, null); // Empty text
Copy link
Contributor

@tvernum tvernum Jun 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't attachments be used here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++, I think this is a copy-paste from the last commit just with that missing.

}

public SlackMessage(String from, String[] to, String icon, String text) {
this(from, to, icon, text, null); // Empty attachments
}

public SlackMessage(String from, String[] to, String icon, String text, Attachment[] attachments) {
this.from = from;
this.to = to;
Expand Down Expand Up @@ -202,7 +213,7 @@ public SlackMessage render(String watchId, String actionId, TextTemplateEngine e
attachments.addAll(dynamicAttachments.render(engine, model, defaults.attachment));
}
if (attachments == null) {
return new SlackMessage(from, to, icon, text, null);
return new SlackMessage(from, to, icon, text);
}
return new SlackMessage(from, to, icon, text, attachments.toArray(new Attachment[attachments.size()]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ public void testUrlPathIsFiltered() throws Exception {
HttpResponse response = new HttpResponse(500);
String path = randomAlphaOfLength(20);
HttpRequest request = HttpRequest.builder("localhost", 1234).path(path).build();
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text", null);
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text");
SentMessages sentMessages = new SentMessages("foo",
Arrays.asList(SentMessages.SentMessage.responded("recipient", slackMessage, request, response)));

Expand All @@ -600,6 +600,18 @@ public void testUrlPathIsFiltered() throws Exception {
}
}

public void testCanHaveNullText() throws Exception {
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", new Attachment[1]);
assertNull(slackMessage.getText());
assertNotNull(slackMessage.getAttachments());
}

public void testCanHaveNullAttachments() throws Exception {
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text");
assertNotNull(slackMessage.getText());
assertNull(slackMessage.getAttachments());
}

private static void writeFieldIfNotNull(XContentBuilder builder, String field, Object value) throws IOException {
if (value != null) {
builder.field(field, value);
Expand Down