This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathslack.q
407 lines (327 loc) · 12.7 KB
/
slack.q
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
quark *;
package slack 0.1.0;
@doc("A high level API for accessing all aspects of the the slack web service.")
@doc("This includes both regular http and realtime web sockets functionality.")
namespace slack {
@doc("A slack client can be used to make requests or subscribe to events from the slack service.")
class SlackClient extends HTTPHandler {
Runtime runtime;
String token;
SlackClient(String token) {
self.runtime = concurrent.Context.runtime();
self.token = token;
}
void request(String request, Map<String,Object> params, HTTPHandler handler) {
String url = "https://slack.com/api/" + request;
HTTPRequest req = new HTTPRequest(url);
req.setMethod("POST");
params["token"] = self.token;
req.setBody(params.urlencode());
req.setHeader("Content-Type", "application/x-www-form-urlencoded");
self.runtime.request(req, handler);
}
@doc("Post a message to a slack channel.")
void post(String channel, String message) {
self.request("chat.postMessage", {"channel": channel, "text": message}, self);
}
@doc("Subscribe to slack events.")
void subscribe(SlackHandler handler) {
self.request("rtm.start", {}, new Subscription(self, handler));
}
}
class Subscription extends WSHandler, HTTPHandler {
Runtime runtime;
SlackHandler handler;
WebSocket socket = null;
int event_id = 0;
Subscription(SlackClient client, SlackHandler handler) {
self.runtime = client.runtime;
self.handler = handler;
}
void ws_connect(String wsurl) {
self.runtime.open(wsurl, self);
}
void ws_send(String message) {
socket.send(message);
}
void onWSConnected(WebSocket socket) {
self.socket = socket;
print("WebSocket Client connected!");
}
void onWSClose(WebSocket socket) {
print("socket closed");
}
void onWSError(WebSocket socket, WSError error) {
print("socket error" + error.getMessage());
}
event.SlackEvent construct(String type) {
if (type == null) { return new event.MessageSent(); }
if (type == "error") { return new event.SlackError(); }
if (type == "hello") { return new event.Hello(); }
if (type == "message") { return new event.Message(); }
if (type == "user_typing") { return new event.UserTyping(); }
if (type == "channel_marked") { return new event.ChannelMarked(); }
if (type == "channel_created") { return new event.ChannelCreated(); }
if (type == "channel_joined") { return new event.ChannelJoined(); }
if (type == "channel_left") { return new event.ChannelLeft(); }
if (type == "channel_deleted") { return new event.ChannelDeleted(); }
if (type == "channel_rename") { return new event.ChannelRename(); }
if (type == "channel_archive") { return new event.ChannelArchive(); }
if (type == "channel_unarchive") { return new event.ChannelUnarchive(); }
if (type == "channel_history_changed") { return new event.ChannelHistoryChanged(); }
return new event.SlackEvent();
}
void onWSMessage(WebSocket socket, String message) {
JSONObject obj = message.parseJSON();
String type = obj["type"].getString();
event.SlackEvent event = self.construct(type);
event.load(self, obj);
event.dispatch(self.handler);
}
void onHTTPResponse(HTTPRequest request, HTTPResponse response) {
int code = response.getCode();
event.SlackError error = null;
if (code != 200) {
error = new event.SlackError();
error.code = code;
error.dispatch(self.handler);
} else {
JSONObject login_data = response.getBody().parseJSON();
if (login_data["ok"].getBool()) {
// parse login data here
self.ws_connect(login_data["url"]);
} else {
error = new event.SlackError();
error.text = login_data["error"];
error.dispatch(self.handler);
}
}
}
}
@doc("A reference to a user.")
class User {
Subscription subscription;
String user;
User(Subscription subscription, String user) {
self.subscription = subscription;
self.user = user;
}
}
@doc("A reference to a channel.")
class Channel {
Subscription subscription;
String channel;
Channel(Subscription subscription, String channel) {
self.subscription = subscription;
self.channel = channel;
}
@doc("Send a message to a channel.")
void send(String message) {
int id = subscription.event_id;
subscription.event_id = subscription.event_id + 1;
JSONObject msg = new JSONObject();
msg["id"] = id;
msg["type"] = "message";
msg["channel"] = self.channel;
msg["text"] = message;
subscription.ws_send(msg.toString());
}
}
@doc("This namespace contains all slack events.")
namespace event {
@doc("Base class for all slack events.")
class SlackEvent {
@doc("The event type.")
String type;
@doc("The user associated with the event.")
User user = null;
@doc("The channel associated with the event.")
Channel channel = null;
@doc("The timestamp associated with the event.")
String timestamp;
void load(Subscription subscription, JSONObject obj) {
self.type = obj["type"];
String uid = obj["user"];
if (uid != null) {
self.user = new User(subscription, uid);
}
String chid = obj["channel"];
if (chid != null) {
self.channel = new Channel(subscription, chid);
}
self.timestamp = obj["ts"];
}
void dispatch(SlackHandler handler) {
handler.onSlackEvent(self);
}
}
@doc("The server has indicated an error has occurred.")
class SlackError extends SlackEvent {
@doc("The numeric code associated with the error condition.")
int code;
@doc("A text description of the error condition.")
String text;
void load(Subscription subscription, JSONObject obj) {
super.load(subscription, obj);
self.code = obj["code"];
self.text = obj["text"];
}
void dispatch(SlackHandler handler) {
handler.onSlackError(self);
}
}
@doc("The client successfully connected to the server.")
class Hello extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onHello(self);
}
}
@doc("A message was sent to a channel.")
class Message extends SlackEvent {
@doc("The subtype field indicates the type of message.")
String subtype;
bool hidden = false;
String text;
Edited edited;
void load(Subscription subscription, JSONObject obj) {
super.load(subscription, obj);
self.subtype = obj["subtype"];
self.hidden = obj["hidden"];
self.text = obj["text"];
JSONObject edited = obj["edited"];
if (edited.isDefined()) {
self.edited = new Edited();
self.edited.user = new User(subscription, edited["user"]);
self.edited.timestamp = edited["ts"];
}
}
void dispatch(SlackHandler handler) {
handler.onMessage(self);
}
}
@doc("A message we sent has been acknowledged by Slack.")
class MessageSent extends SlackEvent {
String text;
Edited edited;
void load(Subscription subscription, JSONObject obj) {
super.load(subscription, obj);
self.text = obj["text"];
}
void dispatch(SlackHandler handler) {
handler.onMessageSent(self);
}
}
@doc("Metadata about an edit to a message.")
class Edited {
User user;
String timestamp;
}
@doc("A channel member is typing a message.")
class UserTyping extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onUserTyping(self);
}
}
@doc("Your channel read marker was updated.")
class ChannelMarked extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelMarked(self);
}
}
@doc("A team channel was created.")
class ChannelCreated extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelCreated(self);
}
}
@doc("You joined a channel.")
class ChannelJoined extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelJoined(self);
}
}
@doc("You left a channel.")
class ChannelLeft extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelLeft(self);
}
}
@doc("A team channel was deleted.")
class ChannelDeleted extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelDeleted(self);
}
}
@doc("A team channel was renamed.")
class ChannelRename extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelRename(self);
}
}
@doc("A team channel was archived.")
class ChannelArchive extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelArchive(self);
}
}
@doc("A team channel was unarchived.")
class ChannelUnarchive extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelUnarchive(self);
}
}
@doc("Bulk updates were made to a channel's history.")
class ChannelHistoryChanged extends SlackEvent {
void dispatch(SlackHandler handler) {
handler.onChannelHistoryChanged(self);
}
}
}
@doc("Event handler for slack events. All unhandled events")
@doc("are delegated to onSlackEvent by default.")
interface SlackHandler {
void onSlackEvent(event.SlackEvent event) {}
void onHello(event.Hello hello) {
self.onSlackEvent(hello);
}
void onSlackError(event.SlackError error) {
self.onSlackEvent(error);
}
void onMessage(event.Message message) {
self.onSlackEvent(message);
}
void onMessageSent(event.MessageSent message) {
self.onSlackEvent(message);
}
void onUserTyping(event.UserTyping typing) {
self.onSlackEvent(typing);
}
void onChannelMarked(event.ChannelMarked marked) {
self.onSlackEvent(marked);
}
void onChannelCreated(event.ChannelCreated created) {
self.onSlackEvent(created);
}
void onChannelJoined(event.ChannelJoined joined) {
self.onSlackEvent(joined);
}
void onChannelLeft(event.ChannelLeft left) {
self.onSlackEvent(left);
}
void onChannelDeleted(event.ChannelDeleted deleted) {
self.onSlackEvent(deleted);
}
void onChannelRename(event.ChannelRename renamed) {
self.onSlackEvent(renamed);
}
void onChannelArchive(event.ChannelArchive archived) {
self.onSlackEvent(archived);
}
void onChannelUnarchive(event.ChannelUnarchive unarchived) {
self.onSlackEvent(unarchived);
}
void onChannelHistoryChanged(event.ChannelHistoryChanged historyChanged) {
self.onSlackEvent(historyChanged);
}
}
}