|
| 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