-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathSlackMessageOutput.java
194 lines (160 loc) · 7.06 KB
/
SlackMessageOutput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package org.graylog2.plugins.slack.output;
import com.floreysoft.jmte.Engine;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.configuration.Configuration;
import org.graylog2.plugin.configuration.ConfigurationException;
import org.graylog2.plugin.configuration.ConfigurationRequest;
import org.graylog2.plugin.inputs.annotations.ConfigClass;
import org.graylog2.plugin.inputs.annotations.FactoryClass;
import org.graylog2.plugin.outputs.MessageOutput;
import org.graylog2.plugin.outputs.MessageOutputConfigurationException;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.plugins.slack.SlackClient;
import org.graylog2.plugins.slack.SlackMessage;
import org.graylog2.plugins.slack.SlackPluginBase;
import org.graylog2.plugins.slack.configuration.SlackConfiguration;
import org.graylog2.plugins.slack.configuration.SlackConfigurationRequestFactory;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.google.common.base.Strings.isNullOrEmpty;
public class SlackMessageOutput extends SlackPluginBase implements MessageOutput {
private final Engine templateEngine;
private AtomicBoolean running = new AtomicBoolean(false);
private final Configuration configuration;
private final Stream stream;
private final SlackClient client;
@Inject
public SlackMessageOutput(
@Assisted Stream stream,
@Assisted Configuration configuration,
Engine templateEngine
) throws MessageOutputConfigurationException {
this.configuration = configuration;
this.stream = stream;
this.templateEngine = templateEngine;
// Check configuration.
try {
checkConfiguration(configuration);
} catch (ConfigurationException e) {
throw new MessageOutputConfigurationException("Missing configuration: " + e.getMessage());
}
this.client = new SlackClient(configuration);
running.set(true);
}
@Override
public void stop() {
running.set(false);
}
@Override
public boolean isRunning() {
return running.get();
}
@Override
public void write(Message msg) throws RuntimeException {
boolean shortMode = configuration.getBoolean(SlackConfiguration.CK_SHORT_MODE);
String message = shortMode ? buildShortMessageBody(msg) : buildFullMessageBody(stream, msg);
SlackMessage slackMessage = createSlackMessage(configuration, message);
// Add custom message
String template = configuration.getString(SlackConfiguration.CK_CUSTOM_MESSAGE);
Boolean hasTemplate = !isNullOrEmpty(template);
if (!shortMode && hasTemplate) {
String customMessage = buildCustomMessage(stream, msg, template);
slackMessage.setCustomMessage(customMessage);
}
// Add attachments
boolean addDetails = configuration.getBoolean(SlackConfiguration.CK_ADD_DETAILS);
if (!shortMode && addDetails) {
buildDetailsAttachment(msg, slackMessage);
}
try {
client.send(slackMessage);
} catch (SlackClient.SlackClientException e) {
throw new RuntimeException("Could not send message to Slack.", e);
}
}
private void buildDetailsAttachment(Message msg, SlackMessage slackMessage) {
slackMessage.addDetailsAttachmentField(new SlackMessage.AttachmentField("Stream Description", stream.getDescription(), false));
slackMessage.addDetailsAttachmentField(new SlackMessage.AttachmentField("Source", msg.getSource(), true));
for (Map.Entry<String, Object> field : msg.getFields().entrySet()) {
if (Message.RESERVED_FIELDS.contains(field.getKey())) continue;
slackMessage.addDetailsAttachmentField(new SlackMessage.AttachmentField(field.getKey(), field.getValue().toString(), true));
}
}
private String buildFullMessageBody(Stream stream, Message msg) {
String graylogUri = configuration.getString(SlackConfiguration.CK_GRAYLOG2_URL);
String titleLink;
if (!isNullOrEmpty(graylogUri)) {
titleLink = "<" + buildStreamLink(graylogUri, stream) + "|" + stream.getTitle() + ">";
} else {
titleLink = "_" + stream.getTitle() + "_";
}
String messageLink;
if (!isNullOrEmpty(graylogUri)) {
String index = "graylog_deflector"; // would use msg.getFieldAs(String.class, "_index"), but it returns null
messageLink = "<" + buildMessageLink(graylogUri, index, msg.getId()) + "|New message>";
} else {
messageLink = "New message";
}
boolean notifyChannel = configuration.getBoolean(SlackConfiguration.CK_NOTIFY_CHANNEL);
String audience = notifyChannel ? "@channel " : "";
return String.format("%s*%s in Graylog stream %s*:\n> %s", audience, messageLink, titleLink, msg.getMessage());
}
private String buildCustomMessage(Stream stream, Message msg, String template) {
Map<String, Object> model = getModel(stream, msg);
try {
return templateEngine.transform(template, model);
} catch (Exception ex) {
return ex.toString();
}
}
private Map<String, Object> getModel(Stream stream, Message msg) {
Map<String, Object> model = new HashMap<>();
String graylogUri = configuration.getString(SlackConfiguration.CK_GRAYLOG2_URL);
model.put("stream", stream);
model.put("message", msg);
if (!isNullOrEmpty(graylogUri)) {
model.put("stream_url", buildStreamLink(graylogUri, stream));
}
return model;
}
private String buildShortMessageBody(Message msg) {
String timeStamp = msg.getTimestamp().toDateTime(DateTimeZone.getDefault()).toString(DateTimeFormat.shortTime());
return String.format("%s: %s", timeStamp, msg.getMessage());
}
@Override
public void write(List<Message> list) {
for (Message message : list) {
write(message);
}
}
public Map<String, Object> getConfiguration() {
return configuration.getSource();
}
@FactoryClass
public interface Factory extends MessageOutput.Factory<SlackMessageOutput> {
@Override
SlackMessageOutput create(Stream stream, Configuration configuration);
@Override
Config getConfig();
@Override
Descriptor getDescriptor();
}
@ConfigClass
public static class Config extends MessageOutput.Config {
@Override
public ConfigurationRequest getRequestedConfiguration() {
return SlackConfigurationRequestFactory.createSlackMessageOutputConfigurationRequest();
}
}
public static class Descriptor extends MessageOutput.Descriptor {
public Descriptor() {
super("Slack Output", false, "", "Writes messages to a Slack chat room.");
}
}
}