@desplega.ai/slack-mock 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -0
- package/dist/body.d.ts +6 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +94 -0
- package/dist/events.d.ts +36 -0
- package/dist/ids.d.ts +8 -0
- package/dist/index-thgwpw70.js +3693 -0
- package/dist/index-v7mbr1cx.js +48 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +17 -0
- package/dist/render/blocks.d.ts +7 -0
- package/dist/render/index.d.ts +25 -0
- package/dist/render/mrkdwn.d.ts +16 -0
- package/dist/screenshot.d.ts +13 -0
- package/dist/screenshot.js +9 -0
- package/dist/server.d.ts +240 -0
- package/dist/socket-mode.d.ts +73 -0
- package/dist/store.d.ts +150 -0
- package/dist/types.d.ts +211 -0
- package/dist/web-api.d.ts +26 -0
- package/package.json +60 -0
- package/src/body.ts +84 -0
- package/src/cli.ts +89 -0
- package/src/events.ts +281 -0
- package/src/ids.ts +29 -0
- package/src/index.ts +13 -0
- package/src/render/blocks.ts +392 -0
- package/src/render/index.ts +846 -0
- package/src/render/mrkdwn.ts +321 -0
- package/src/screenshot.ts +59 -0
- package/src/server.ts +1198 -0
- package/src/socket-mode.ts +261 -0
- package/src/store.ts +654 -0
- package/src/types.ts +159 -0
- package/src/web-api.ts +694 -0
package/src/events.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// Builders for the payloads Slack pushes to apps: Events API callbacks,
|
|
2
|
+
// slash commands and interactive payloads. Shapes follow @slack/types.
|
|
3
|
+
|
|
4
|
+
import { nextTs, nowUnix, slackId } from "./ids.ts";
|
|
5
|
+
import { type Store, wireFile, wireMessage } from "./store.ts";
|
|
6
|
+
import type { SlackChannel, SlackMessage, SlackUser, SlackView } from "./types.ts";
|
|
7
|
+
|
|
8
|
+
export const VERIFICATION_TOKEN = "mock-verification-token";
|
|
9
|
+
|
|
10
|
+
export function eventCallback(
|
|
11
|
+
store: Store,
|
|
12
|
+
event: Record<string, unknown>,
|
|
13
|
+
): Record<string, unknown> {
|
|
14
|
+
return {
|
|
15
|
+
token: VERIFICATION_TOKEN,
|
|
16
|
+
team_id: store.team.id,
|
|
17
|
+
context_team_id: store.team.id,
|
|
18
|
+
context_enterprise_id: null,
|
|
19
|
+
api_app_id: store.app.id,
|
|
20
|
+
event,
|
|
21
|
+
type: "event_callback",
|
|
22
|
+
event_id: slackId("Ev", 9),
|
|
23
|
+
event_time: nowUnix(),
|
|
24
|
+
authorizations: [
|
|
25
|
+
{
|
|
26
|
+
enterprise_id: null,
|
|
27
|
+
team_id: store.team.id,
|
|
28
|
+
user_id: store.bot.userId,
|
|
29
|
+
is_bot: true,
|
|
30
|
+
is_enterprise_install: false,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
is_ext_shared_channel: false,
|
|
34
|
+
event_context: Buffer.from(
|
|
35
|
+
`${store.team.id}:${store.app.id}:${(event.channel as string) ?? ""}`,
|
|
36
|
+
).toString("base64"),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The `message` event for a stored message (any subtype). */
|
|
41
|
+
export function messageEvent(
|
|
42
|
+
store: Store,
|
|
43
|
+
m: SlackMessage,
|
|
44
|
+
channel: SlackChannel,
|
|
45
|
+
): Record<string, unknown> {
|
|
46
|
+
const {
|
|
47
|
+
channel: _c,
|
|
48
|
+
team,
|
|
49
|
+
...rest
|
|
50
|
+
} = wireMessage(m) as Record<string, unknown> & { team: string };
|
|
51
|
+
const event: Record<string, unknown> = {
|
|
52
|
+
...rest,
|
|
53
|
+
type: "message",
|
|
54
|
+
channel: m.channel,
|
|
55
|
+
event_ts: m.ts,
|
|
56
|
+
channel_type: store.channelType(channel),
|
|
57
|
+
team,
|
|
58
|
+
};
|
|
59
|
+
if (m.files?.length && !m.subtype) event.subtype = "file_share";
|
|
60
|
+
if (m.files?.length) {
|
|
61
|
+
event.upload = false;
|
|
62
|
+
event.display_as_bot = false;
|
|
63
|
+
}
|
|
64
|
+
return event;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function appMentionEvent(store: Store, m: SlackMessage): Record<string, unknown> {
|
|
68
|
+
const { channel_type: _ct, ...event } = messageEvent(store, m, store.channel(m.channel));
|
|
69
|
+
return { ...event, type: "app_mention" };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function messageChangedEvent(
|
|
73
|
+
store: Store,
|
|
74
|
+
m: SlackMessage,
|
|
75
|
+
previous: SlackMessage,
|
|
76
|
+
): Record<string, unknown> {
|
|
77
|
+
const channel = store.channel(m.channel);
|
|
78
|
+
const ts = nextTs();
|
|
79
|
+
return {
|
|
80
|
+
type: "message",
|
|
81
|
+
subtype: "message_changed",
|
|
82
|
+
channel: m.channel,
|
|
83
|
+
channel_type: store.channelType(channel),
|
|
84
|
+
hidden: true,
|
|
85
|
+
ts,
|
|
86
|
+
event_ts: ts,
|
|
87
|
+
message: {
|
|
88
|
+
...wireMessage(m),
|
|
89
|
+
channel: undefined,
|
|
90
|
+
source_team: store.team.id,
|
|
91
|
+
user_team: store.team.id,
|
|
92
|
+
},
|
|
93
|
+
previous_message: { ...wireMessage(previous), channel: undefined },
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function messageDeletedEvent(store: Store, m: SlackMessage): Record<string, unknown> {
|
|
98
|
+
const channel = store.channel(m.channel);
|
|
99
|
+
const ts = nextTs();
|
|
100
|
+
return {
|
|
101
|
+
type: "message",
|
|
102
|
+
subtype: "message_deleted",
|
|
103
|
+
channel: m.channel,
|
|
104
|
+
channel_type: store.channelType(channel),
|
|
105
|
+
hidden: true,
|
|
106
|
+
deleted_ts: m.ts,
|
|
107
|
+
ts,
|
|
108
|
+
event_ts: ts,
|
|
109
|
+
previous_message: { ...wireMessage(m), channel: undefined },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function reactionEvent(
|
|
114
|
+
kind: "reaction_added" | "reaction_removed",
|
|
115
|
+
m: SlackMessage,
|
|
116
|
+
name: string,
|
|
117
|
+
user: string,
|
|
118
|
+
): Record<string, unknown> {
|
|
119
|
+
return {
|
|
120
|
+
type: kind,
|
|
121
|
+
user,
|
|
122
|
+
reaction: name,
|
|
123
|
+
item_user: m.user,
|
|
124
|
+
item: { type: "message", channel: m.channel, ts: m.ts },
|
|
125
|
+
event_ts: nextTs(),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function memberJoinedEvent(
|
|
130
|
+
store: Store,
|
|
131
|
+
channel: SlackChannel,
|
|
132
|
+
user: string,
|
|
133
|
+
inviter?: string,
|
|
134
|
+
): Record<string, unknown> {
|
|
135
|
+
return {
|
|
136
|
+
type: "member_joined_channel",
|
|
137
|
+
user,
|
|
138
|
+
channel: channel.id,
|
|
139
|
+
channel_type: store.channelType(channel) === "channel" ? "C" : "G",
|
|
140
|
+
team: store.team.id,
|
|
141
|
+
inviter,
|
|
142
|
+
event_ts: nextTs(),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function assistantThreadStartedEvent(
|
|
147
|
+
store: Store,
|
|
148
|
+
dm: SlackChannel,
|
|
149
|
+
user: string,
|
|
150
|
+
threadTs: string,
|
|
151
|
+
context: Record<string, unknown> = {},
|
|
152
|
+
): Record<string, unknown> {
|
|
153
|
+
return {
|
|
154
|
+
type: "assistant_thread_started",
|
|
155
|
+
assistant_thread: {
|
|
156
|
+
user_id: user,
|
|
157
|
+
context: { team_id: store.team.id, enterprise_id: null, ...context },
|
|
158
|
+
channel_id: dm.id,
|
|
159
|
+
thread_ts: threadTs,
|
|
160
|
+
},
|
|
161
|
+
event_ts: nextTs(),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function assistantThreadContextChangedEvent(
|
|
166
|
+
store: Store,
|
|
167
|
+
dm: SlackChannel,
|
|
168
|
+
user: string,
|
|
169
|
+
threadTs: string,
|
|
170
|
+
context: Record<string, unknown>,
|
|
171
|
+
): Record<string, unknown> {
|
|
172
|
+
return {
|
|
173
|
+
type: "assistant_thread_context_changed",
|
|
174
|
+
assistant_thread: {
|
|
175
|
+
user_id: user,
|
|
176
|
+
context: { team_id: store.team.id, enterprise_id: null, ...context },
|
|
177
|
+
channel_id: dm.id,
|
|
178
|
+
thread_ts: threadTs,
|
|
179
|
+
},
|
|
180
|
+
event_ts: nextTs(),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function slashCommandPayload(
|
|
185
|
+
store: Store,
|
|
186
|
+
input: {
|
|
187
|
+
command: string;
|
|
188
|
+
text: string;
|
|
189
|
+
user: SlackUser;
|
|
190
|
+
channel: SlackChannel;
|
|
191
|
+
responseUrl: string;
|
|
192
|
+
triggerId: string;
|
|
193
|
+
},
|
|
194
|
+
): Record<string, unknown> {
|
|
195
|
+
return {
|
|
196
|
+
token: VERIFICATION_TOKEN,
|
|
197
|
+
team_id: store.team.id,
|
|
198
|
+
team_domain: store.team.domain,
|
|
199
|
+
channel_id: input.channel.id,
|
|
200
|
+
channel_name: input.channel.is_im ? "directmessage" : input.channel.name,
|
|
201
|
+
user_id: input.user.id,
|
|
202
|
+
user_name: input.user.name,
|
|
203
|
+
command: input.command,
|
|
204
|
+
text: input.text,
|
|
205
|
+
api_app_id: store.app.id,
|
|
206
|
+
is_enterprise_install: "false",
|
|
207
|
+
response_url: input.responseUrl,
|
|
208
|
+
trigger_id: input.triggerId,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function blockActionsPayload(
|
|
213
|
+
store: Store,
|
|
214
|
+
input: {
|
|
215
|
+
user: SlackUser;
|
|
216
|
+
channel: SlackChannel;
|
|
217
|
+
message: SlackMessage;
|
|
218
|
+
actions: Array<Record<string, unknown>>;
|
|
219
|
+
responseUrl: string;
|
|
220
|
+
triggerId: string;
|
|
221
|
+
},
|
|
222
|
+
): Record<string, unknown> {
|
|
223
|
+
return {
|
|
224
|
+
type: "block_actions",
|
|
225
|
+
user: {
|
|
226
|
+
id: input.user.id,
|
|
227
|
+
username: input.user.name,
|
|
228
|
+
name: input.user.name,
|
|
229
|
+
team_id: store.team.id,
|
|
230
|
+
},
|
|
231
|
+
api_app_id: store.app.id,
|
|
232
|
+
token: VERIFICATION_TOKEN,
|
|
233
|
+
container: {
|
|
234
|
+
type: "message",
|
|
235
|
+
message_ts: input.message.ts,
|
|
236
|
+
channel_id: input.channel.id,
|
|
237
|
+
is_ephemeral: !!input.message.ephemeral_user,
|
|
238
|
+
...(input.message.thread_ts ? { thread_ts: input.message.thread_ts } : {}),
|
|
239
|
+
},
|
|
240
|
+
trigger_id: input.triggerId,
|
|
241
|
+
team: { id: store.team.id, domain: store.team.domain },
|
|
242
|
+
enterprise: null,
|
|
243
|
+
is_enterprise_install: false,
|
|
244
|
+
channel: { id: input.channel.id, name: input.channel.name },
|
|
245
|
+
message: wireMessage(input.message),
|
|
246
|
+
state: { values: {} },
|
|
247
|
+
response_url: input.responseUrl,
|
|
248
|
+
actions: input.actions.map((a) => ({ action_ts: nextTs(), ...a })),
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function viewSubmissionPayload(
|
|
253
|
+
store: Store,
|
|
254
|
+
input: {
|
|
255
|
+
user: SlackUser;
|
|
256
|
+
view: SlackView;
|
|
257
|
+
values: Record<string, Record<string, unknown>>;
|
|
258
|
+
triggerId: string;
|
|
259
|
+
},
|
|
260
|
+
): Record<string, unknown> {
|
|
261
|
+
const { opened_by: _o, trigger_id: _t, ...view } = input.view;
|
|
262
|
+
return {
|
|
263
|
+
type: "view_submission",
|
|
264
|
+
team: { id: store.team.id, domain: store.team.domain },
|
|
265
|
+
user: {
|
|
266
|
+
id: input.user.id,
|
|
267
|
+
username: input.user.name,
|
|
268
|
+
name: input.user.name,
|
|
269
|
+
team_id: store.team.id,
|
|
270
|
+
},
|
|
271
|
+
api_app_id: store.app.id,
|
|
272
|
+
token: VERIFICATION_TOKEN,
|
|
273
|
+
trigger_id: input.triggerId,
|
|
274
|
+
view: { ...view, state: { values: input.values } },
|
|
275
|
+
response_urls: [],
|
|
276
|
+
is_enterprise_install: false,
|
|
277
|
+
enterprise: null,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export { wireFile };
|
package/src/ids.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Slack-style identifiers and message timestamps.
|
|
2
|
+
|
|
3
|
+
const ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
|
|
4
|
+
|
|
5
|
+
export function slackId(prefix: string, length = 8): string {
|
|
6
|
+
let out = prefix;
|
|
7
|
+
for (let i = 0; i < length; i++) out += ALPHABET[Math.floor(Math.random() * ALPHABET.length)];
|
|
8
|
+
return out;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let lastMicros = 0;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Slack message timestamps look like "1700000000.123456": unix seconds plus a
|
|
15
|
+
* six-digit sequence. They must be unique per channel and strictly increasing,
|
|
16
|
+
* so we derive them from a monotonic microsecond clock.
|
|
17
|
+
*/
|
|
18
|
+
export function nextTs(): string {
|
|
19
|
+
let micros = Date.now() * 1000;
|
|
20
|
+
if (micros <= lastMicros) micros = lastMicros + 1;
|
|
21
|
+
lastMicros = micros;
|
|
22
|
+
const seconds = Math.floor(micros / 1_000_000);
|
|
23
|
+
const fraction = (micros % 1_000_000).toString().padStart(6, "0");
|
|
24
|
+
return `${seconds}.${fraction}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function nowUnix(): number {
|
|
28
|
+
return Math.floor(Date.now() / 1000);
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { findChrome, screenshot } from "./screenshot.ts";
|
|
2
|
+
export type {
|
|
3
|
+
Fault,
|
|
4
|
+
MessageQuery,
|
|
5
|
+
PostMessageInput,
|
|
6
|
+
SlackManifest,
|
|
7
|
+
SlackMockOptions,
|
|
8
|
+
} from "./server.ts";
|
|
9
|
+
export { SlackApiError, SlackMock } from "./server.ts";
|
|
10
|
+
export type { AckResult, DeliveryRecord } from "./socket-mode.ts";
|
|
11
|
+
export type { AddChannelInput, AddUserInput } from "./store.ts";
|
|
12
|
+
export { Store } from "./store.ts";
|
|
13
|
+
export type * from "./types.ts";
|