-
Notifications
You must be signed in to change notification settings - Fork 10
/
SlackUtil.java
180 lines (161 loc) · 6.42 KB
/
SlackUtil.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
package com.katalon.plugin.slack;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.google.gson.stream.JsonReader;
public class SlackUtil {
public static final String RES_IS_OK = "isOk";
public static final String RES_ERROR_MSG = "errorMsg";
/**
* Send message to Slack for Team Collaboration
*
* @param msg String message to send
* @throws SlackException
*/
public static void sendMessage(String token, String channel, String msg) throws SlackException {
try {
Map<String, Object> response = getResponseFromSendingMsg(buildSlackUri(token, channel, msg));
boolean isOk = (boolean) response.get(RES_IS_OK);
if (!isOk) {
String errorMsg = (String) response.get(RES_ERROR_MSG);
throw new SlackException(errorMsg);
}
} catch (IOException | URISyntaxException e) {
throw new SlackException(e);
}
}
/**
* Slack URI builder which will encode user info and message into URI
*
* @param token the authentication token
* @param channel Channel name
* @param username Bot name
* @param asIssuedTokenUser if true, Bot name will be ignore. Otherwise, message will be sent using Bot name
* @param msg Message to send
* @return URI
* @throws URISyntaxException
* @throws Exception UnsupportedEncodingException
* @see UnsupportedEncodingException
* @see <a href="https://api.slack.com/web">Create Slack auth token</a>
*/
public static URI buildSlackUri(String token, String channel, String msg) throws URISyntaxException {
// scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
URIBuilder uriBuilder = new URIBuilder().setScheme("https")
.setHost("slack.com")
.setPath("/api/chat.postMessage")
.addParameter("token", token)
.addParameter("channel", channel);
uriBuilder.addParameter("text", msg);
return uriBuilder.build();
}
/**
* Get response data from sending message to Slack
*
* @param uri URI to send GET request
* @return A Map object with 2 keys SlackUtil.RES_IS_OK and SlackUtil.RES_ERROR_MSG
* @throws ClientProtocolException
* @throws IOException
*/
public static Map<String, Object> getResponseFromSendingMsg(URI uri) throws ClientProtocolException, IOException {
boolean isOk = false;
String errorMsg = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(uri);
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStreamReader in = new InputStreamReader(entity.getContent());
try {
JsonReader reader = new JsonReader(in);
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (StringUtils.equals(name, "ok")) {
isOk = reader.nextBoolean();
} else if (StringUtils.equals(name, "error")) {
errorMsg = reader.nextString();
} else {
reader.skipValue(); // avoid some unhandled events
}
}
reader.endObject();
reader.close();
} finally {
in.close();
}
}
} finally {
response.close();
}
Map<String, Object> res = new HashMap<String, Object>();
res.put(RES_IS_OK, isOk);
res.put(RES_ERROR_MSG, errorMsg);
return res;
}
/**
* Slack Bold format text message
*
* @param msg String text message
* @return String Bold format text message
*/
public String fmtBold(String msg) {
return "*" + msg + "*";
}
/**
* Slack Italic format text message
*
* @param msg String text message
* @return String Italic format text message
*/
public String fmtItalic(String msg) {
return "_" + msg + "_";
}
/**
* Slack responded message status
*/
public static class SlackMsgStatus {
private static SlackMsgStatus _instance;
private static Map<String, String> msgMap;
public static SlackMsgStatus getInstance() {
if (_instance == null) {
_instance = new SlackMsgStatus();
}
return _instance;
}
public SlackMsgStatus() {
msgMap = new HashMap<String, String>();
msgMap.put("channel_not_found", StringConstants.SLACK_ERROR_MSG_CHANNEL_NOT_FOUND);
msgMap.put("not_in_channel", StringConstants.SLACK_ERROR_MSG_NOT_IN_CHANNEL);
msgMap.put("is_archived", StringConstants.SLACK_ERROR_MSG_IS_ARCHIVED);
msgMap.put("msg_too_long", StringConstants.SLACK_ERROR_MSG_MSG_TOO_LONG);
msgMap.put("no_text", StringConstants.SLACK_ERROR_MSG_NO_TEXT);
msgMap.put("rate_limited", StringConstants.SLACK_ERROR_MSG_RATE_LIMITED);
msgMap.put("not_authed", StringConstants.SLACK_ERROR_MSG_NOT_AUTHED);
msgMap.put("invalid_auth", StringConstants.SLACK_ERROR_MSG_INVALID_AUTH);
msgMap.put("account_inactive", StringConstants.SLACK_ERROR_MSG_ACCOUNT_INACTIVE);
}
public Map<String, String> getMsgMap() {
return msgMap;
}
public String getMsgDescription(String msgCode) {
if (getMsgMap().get(msgCode) == null) {
// No message description found
return msgCode;
}
return getMsgMap().get(msgCode);
}
}
}