Skip to content

Commit 117e273

Browse files
authored
Merge pull request #3 from vusters/ephemeral
Ephemeral
2 parents 2318513 + 431dba0 commit 117e273

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/main/java/allbegray/slack/webapi/SlackWebApiClientImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,13 @@ public String unfurl(String channel, String ts, Map<String, Attachment> unfurlRe
305305
return retNode.findPath("ts").asText();
306306
}
307307

308+
@Override
309+
public String postEphemeral(ChatPostEphemeralMethod method) {
310+
method.setMapper(mapper);
311+
JsonNode retNode = call(method);
312+
return retNode.findPath("message_ts").asText();
313+
}
314+
308315
// dnd
309316

310317
@Override

src/main/java/allbegray/slack/webapi/SlackWebApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public interface SlackWebApiConstants {
4343
String CHAT_POST_MESSAGE = "chat.postMessage";
4444
String CHAT_UPDATE = "chat.update";
4545
String CHAT_UNFURL = "chat.unfurl";
46+
String CHAT_POST_EPHEMERAL = "chat.postEphemeral";
4647

4748
// dnd
4849
String DND_END_DND = "dnd.endDnd";

src/main/java/allbegray/slack/webapi/api/ChatApi.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55

66
import allbegray.slack.type.Attachment;
7+
import allbegray.slack.webapi.method.chats.ChatPostEphemeralMethod;
78
import allbegray.slack.webapi.method.chats.ChatPostMessageMethod;
89

910
public interface ChatApi {
@@ -27,4 +28,6 @@ String postMessage(String channel, String text, String username, boolean as_user
2728
String updateMessage(String channel, String ts, String text, List<Attachment> attachments, boolean link_names);
2829

2930
String unfurl(String channel, String ts, Map<String, Attachment> unfurlResponseMap, boolean user_auth_required);
31+
32+
String postEphemeral(ChatPostEphemeralMethod method);
3033
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package allbegray.slack.webapi.method.chats;
2+
3+
import allbegray.slack.exception.SlackException;
4+
import allbegray.slack.type.Attachment;
5+
import allbegray.slack.validation.ValidationError;
6+
import allbegray.slack.webapi.SlackWebApiConstants;
7+
import allbegray.slack.webapi.method.AbstractMethod;
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public class ChatPostEphemeralMethod extends AbstractMethod {
16+
protected String channel;
17+
protected String user;
18+
protected String text;
19+
protected List<Attachment> attachments;
20+
21+
protected ObjectMapper mapper;
22+
23+
public ChatPostEphemeralMethod(String channel, String user) {
24+
this.channel = channel;
25+
this.user = user;
26+
}
27+
28+
public void setText(String text) {
29+
this.text = text;
30+
}
31+
32+
public void setAttachments(List<Attachment> attachments) {
33+
this.attachments = attachments;
34+
}
35+
36+
public void addAttachment(Attachment attachment) {
37+
if (attachments == null) {
38+
attachments = new ArrayList<>();
39+
}
40+
attachments.add(attachment);
41+
}
42+
43+
public void setMapper(ObjectMapper mapper) {
44+
this.mapper = mapper;
45+
}
46+
47+
@Override
48+
public String getMethodName() {
49+
return SlackWebApiConstants.CHAT_POST_EPHEMERAL;
50+
}
51+
52+
@Override
53+
public void validate(List<ValidationError> errors) {
54+
55+
}
56+
57+
@Override
58+
protected void createParameters(Map<String, String> parameters) {
59+
parameters.put("channel", channel);
60+
parameters.put("user", user);
61+
parameters.put("text", text);
62+
63+
if (attachments != null && !attachments.isEmpty()) {
64+
try {
65+
parameters.put("attachments", mapper.writeValueAsString(attachments));
66+
} catch (JsonProcessingException e) {
67+
throw new SlackException(e);
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)