-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slack.java
252 lines (241 loc) · 9.82 KB
/
Slack.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package redditBotCreator;
import javafx.fxml.FXMLLoader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
public class Slack {
String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36";
private HttpClient client = HttpClientBuilder.create().build();
String token;
void setToken(String token){
this.token = token;
}
/**
* This function joins a slack channel. If the slack channel doesn't exist, it will create it.
* @param slackChannel the name of the slack channel to join
*/
String joinSlackChannel(String slackChannel){
String url = "https://slack.com/api/channels.join?token=" + token + "&name=" + slackChannel;
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response;
JSONObject o = null;
try {
response = client.execute(request);
//Create the response
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
//System.out.println(result.toString());
o = new JSONObject(result.toString());
request.releaseConnection();
boolean isOkay = (boolean)o.get("ok");
if(!isOkay){
String error = (String)o.get("error");
if(error.equals("is_archived")){
return "ARCHIVED";
}
}
return (String)o.getJSONObject("channel").get("id");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "ERROR";
}
/**
* This function attemps to unarchive a slack channel. The slack channel name is provided by the slackChannel parameter
* If the slackChannel was indeed unarchived, the function returns true, otherwise it returns false.
* This is necessary because slack does not delete channels, only archives them. This would cause errors if a user attempts
* to create a slack channel with the same name as an archived channel.
*/
void unarchiveChannel(String slackChannel){
String channelID = getChannelID(slackChannel);
String url = "https://slack.com/api/channels.unarchive?token=" + token + "&channel=" + channelID;
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response;
JSONObject o = null;
try {
response = client.execute(request);
request.releaseConnection();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
}*/
/**
* This function sends all of the links to the users slack channel
* @param links
* @param slackChannel
* @return the number of links sent
*/
int sendLinksToUsers(ArrayList<String> links, String slackChannel){
String channelID = joinSlackChannel(slackChannel);
if(channelID.equals("ARCHIVED")){
channelID = slackChannel;
}
String baseURL = "https://slack.com/api/chat.postMessage?token=" + token + "&channel=" + slackChannel + "&text=";
URI url;
HttpPost request = new HttpPost();
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response;
JSONObject o = null;
for(int i = 0; i < links.size(); i++) {
url = URI.create(baseURL + links.get(i));
request.setURI(url);
try {
response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
o = new JSONObject(result.toString());
System.out.println(o.toString());
request.releaseConnection();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return links.size();
}
/**
* This function returns the id of the slack channel with the given slack channel name
* @param checkChannel name of string of channel
* @return
*/
String getChannelID(String checkChannel){
String url = "https://slack.com/api/channels.list?token=" + token;
HttpGet request = new HttpGet(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response;
JSONObject o = null;
try {
response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
o = new JSONObject(result.toString());
JSONArray channels;
channels = o.getJSONArray("channels");
for (int i = 0; i < channels.length(); i++) {
String channelName = (String)channels.getJSONObject(i).get("name");
if(channelName.equals(checkChannel)){
return (String)channels.getJSONObject(i).get("id");
}
}
request.releaseConnection();
return "NOTFOUND";
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "ERROR";
}
/**
* This function authorizes the token the user pastes into the textbox.
* If the token is valid, it slack class will store it.
*/
boolean authorizeToken(String token){
String url = "https://slack.com/api/auth.test?token=" + token;
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response;
JSONObject o = null;
try {
response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
o = new JSONObject(result.toString());
request.releaseConnection();
boolean validAuth = (boolean)(o.get("ok"));
if(validAuth){
this.setToken(token); //set the token
}
return validAuth;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
boolean hasToken(){
return this.token != null;
}
/**
* This function opens up the helper app in the user's web browser and has cross-platform support
* @throws IOException
*/
void crossPlatformOpenWebApp() throws IOException {
String os = System.getProperty("os.name").toLowerCase();
String url = "http://www.redditbotcreator.byethost6.com/apps/myapp/install.php";
Runtime rt = Runtime.getRuntime();
//WINDOWS
if(os.indexOf("win") >= 0){
rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
}
//OS MAC
else if(os.indexOf("mac") >= 0){
rt.exec("open " + url);
}
//LINUX
else if(os.indexOf("nix") >= 0 || os.indexOf(("nux")) >= 0){
rt.exec("xdg-open " + url);
}
else{
utilities.getInstance().makeAlert("Esoteric OS", "Could not open web-browser", "You are using an OS" +
"that is not supported with this operation. Please open up your web browser and navigate to this link: " +
"\'http://www.redditbotcreator.byethost6.com/apps/myapp/install.php\'.");
}
}
}