-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontext.w
147 lines (130 loc) · 3.5 KB
/
context.w
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
bring "./message.w" as msg;
bring "./util.w" as helpers;
bring "./events.w" as events;
/// The bahvioral interface of a thread
pub inflight interface IThread {
postMessage(message: msg.Message): Json;
post(message: str): Json;
}
/// Represents the context of a slack channel
pub inflight class Channel impl IThread {
/// The channel id
pub id: str;
token: str;
new(channel: str, token: str) {
this.id = channel;
this.token = token;
}
/// Post a message block to a channel
pub inflight postMessage(message: msg.Message): Json {
return helpers.SlackUtils.post(
{
channel: this.id,
blocks: message.toJson(),
},
this.token
);
}
/// Post raw text to a channel
pub inflight post(message: str): Json {
return helpers.SlackUtils.post(
{
channel: this.id,
text: message
},
this.token
);
}
}
/// Represents the context of a slack thread
pub inflight class Thread impl IThread {
/// The channel context
pub channel: Channel;
/// The thread timestamp
pub timestamp: str;
token: str;
new(channel: str, thread_ts: str, token: str) {
this.channel = new Channel(channel, token);
this.timestamp = thread_ts;
this.token = token;
}
/// Post a message to a thread
pub inflight postMessage(message: msg.Message): Json {
return helpers.SlackUtils.post(
{
channel: this.channel.id,
thread_ts: this.timestamp,
blocks: message.toJson()
},
this.token
);
}
/// Post raw text to a thread
pub inflight post(message: str): Json {
return helpers.SlackUtils.post(
{
channel: this.channel.id,
thread_ts: this.timestamp,
text: message
},
this.token
);
}
}
/// Only used for internal testing
pub inflight class MockChannel extends Channel {
new(channel: str, token: str) {
super(channel, token);
}
pub inflight post(message: str): Json {
return {
status: 200,
body: "Totally sent that string to the channel: {this.id} :)"
};
}
pub inflight postMessage(message: msg.Message): Json {
return {
status: 200,
body: "Totally sent that message to the channel: {this.id} :)"
};
}
}
/// Only used for internal testing
inflight class MockThread extends Thread {
new(channel: str, thread_ts: str, token: str) {
super(channel, thread_ts, token);
}
pub inflight post(message: str): Json {
return {
status: 200,
body: "Totally sent that string to the thread :)"
};
}
pub inflight postMessage(message: msg.Message): Json {
return {
status: 200,
body: "Totally sent that message to the thread :)"
};
}
}
/// Represents the context of an event callback
pub inflight class EventContext {
pub thread: Thread;
pub channel: Channel;
new(rawContext: Json, token: str) {
let callBackEvent = events.CallbackEvent.fromJson(rawContext["event"]);
this.thread = new Thread(callBackEvent.channel, callBackEvent.event_ts, token);
this.channel = new Channel(callBackEvent.channel, token);
}
}
/// Internally used for mocking event context
pub inflight class EventContext_Mock extends EventContext {
pub thread: Thread;
pub channel: Channel;
new(rawContext: Json, token: str) {
super(rawContext, "");
let callBackEvent = events.CallbackEvent.fromJson(rawContext["event"]);
this.thread = new MockThread(callBackEvent.channel, callBackEvent.event_ts, token);
this.channel = new MockChannel(callBackEvent.channel, token);
}
}