@zhin.js/adapter-slack 4.0.1 → 4.1.1
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/CHANGELOG.md +59 -0
- package/README.md +77 -160
- package/adapters/slack.ts +34 -0
- package/{skills/slack/SKILL.md → agent/skills/slack.md} +2 -0
- package/agent/tools/add_reaction.ts +26 -0
- package/agent/tools/archive_channel.ts +24 -0
- package/agent/tools/edit_message.ts +28 -0
- package/agent/tools/invite_to_channel.ts +26 -0
- package/agent/tools/pin_message.ts +26 -0
- package/agent/tools/remove_reaction.ts +26 -0
- package/agent/tools/set_purpose.ts +26 -0
- package/agent/tools/set_topic.ts +26 -0
- package/agent/tools/unarchive.ts +24 -0
- package/agent/tools/unpin_message.ts +26 -0
- package/agent/tools/user_info.ts +31 -0
- package/lib/endpoint.d.ts +133 -94
- package/lib/endpoint.js +270 -612
- package/lib/index.d.ts +11 -10
- package/lib/index.js +11 -252
- package/lib/markdown-to-mrkdwn.d.ts +9 -0
- package/lib/markdown-to-mrkdwn.js +121 -0
- package/lib/mrkdwn-to-markdown.d.ts +4 -0
- package/lib/mrkdwn-to-markdown.js +30 -0
- package/lib/platform-permit.d.ts +1 -2
- package/lib/platform-permit.js +4 -2
- package/lib/protocol.d.ts +143 -0
- package/lib/protocol.js +225 -0
- package/lib/slack-agent-deps.d.ts +28 -0
- package/lib/slack-agent-deps.js +30 -0
- package/lib/slack-inbound-filter.d.ts +12 -0
- package/lib/slack-inbound-filter.js +46 -0
- package/lib/slack-message-ref.d.ts +7 -0
- package/lib/slack-message-ref.js +17 -0
- package/lib/slack-outbound.d.ts +24 -0
- package/lib/slack-outbound.js +109 -0
- package/lib/slack-reaction.d.ts +1 -0
- package/lib/slack-reaction.js +26 -0
- package/lib/slack-response-url.d.ts +5 -0
- package/lib/slack-response-url.js +16 -0
- package/lib/webhook.d.ts +14 -0
- package/lib/webhook.js +69 -0
- package/package.json +53 -14
- package/plugin.ts +13 -0
- package/schema.json +38 -0
- package/src/endpoint.ts +339 -647
- package/src/index.ts +65 -277
- package/src/markdown-to-mrkdwn.ts +117 -0
- package/src/mrkdwn-to-markdown.ts +29 -0
- package/src/platform-permit.ts +1 -2
- package/src/protocol.ts +380 -0
- package/src/slack-agent-deps.ts +57 -0
- package/src/slack-inbound-filter.ts +60 -0
- package/src/slack-message-ref.ts +18 -0
- package/src/slack-outbound.ts +167 -0
- package/src/slack-reaction.ts +23 -0
- package/src/slack-response-url.ts +26 -0
- package/src/webhook.ts +103 -0
- package/lib/adapter.d.ts +0 -19
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -46
- package/lib/adapter.js.map +0 -1
- package/lib/endpoint.d.ts.map +0 -1
- package/lib/endpoint.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/platform-permit.d.ts.map +0 -1
- package/lib/platform-permit.js.map +0 -1
- package/lib/segment-mapper.d.ts +0 -2
- package/lib/segment-mapper.d.ts.map +0 -1
- package/lib/segment-mapper.js +0 -2
- package/lib/segment-mapper.js.map +0 -1
- package/lib/types.d.ts +0 -17
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/adapter.ts +0 -54
- package/src/segment-mapper.ts +0 -1
- package/src/types.ts +0 -18
- /package/{skills/slack → agent}/PERMITS.md +0 -0
package/lib/protocol.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack protocol helpers — no legacy Adapter/Endpoint / segment-mapper.
|
|
3
|
+
* Canonicalization is owned by gateway/core before endpoint.send.
|
|
4
|
+
*/
|
|
5
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
6
|
+
import { mrkdwnToMarkdown } from './mrkdwn-to-markdown.js';
|
|
7
|
+
const SLACK_SIG_VERSION = 'v0';
|
|
8
|
+
const MAX_TIMESTAMP_DRIFT_SECONDS = 300;
|
|
9
|
+
export function resolveSlackConfig(config = {}) {
|
|
10
|
+
const entry = config.endpoints?.find((item) => item.context === 'slack');
|
|
11
|
+
const token = config.token
|
|
12
|
+
?? entry?.token
|
|
13
|
+
?? process.env.SLACK_BOT_TOKEN
|
|
14
|
+
?? process.env.SLACK_TOKEN;
|
|
15
|
+
if (!token) {
|
|
16
|
+
throw new TypeError('Slack adapter requires token (plugins.<key>.token or endpoints with context: slack)');
|
|
17
|
+
}
|
|
18
|
+
const name = (typeof config.name === 'string' && config.name)
|
|
19
|
+
|| (typeof entry?.name === 'string' && entry.name)
|
|
20
|
+
|| process.env.SLACK_BOT_NAME
|
|
21
|
+
|| 'slack-bot';
|
|
22
|
+
const socketMode = config.socketMode ?? entry?.socketMode;
|
|
23
|
+
// Prefer Socket Mode (default true) — no public URL required.
|
|
24
|
+
const mode = socketMode === false ? 'http' : 'socket';
|
|
25
|
+
const signingSecret = config.signingSecret
|
|
26
|
+
?? entry?.signingSecret
|
|
27
|
+
?? process.env.SLACK_SIGNING_SECRET
|
|
28
|
+
?? '';
|
|
29
|
+
const appToken = config.appToken
|
|
30
|
+
?? entry?.appToken
|
|
31
|
+
?? process.env.SLACK_APP_TOKEN
|
|
32
|
+
?? undefined;
|
|
33
|
+
if (mode === 'socket' && !appToken) {
|
|
34
|
+
throw new TypeError('Slack Socket Mode requires appToken (xapp-...); set socketMode: false for HTTP Events API');
|
|
35
|
+
}
|
|
36
|
+
if (mode === 'http' && !signingSecret) {
|
|
37
|
+
throw new TypeError('Slack HTTP Events API requires signingSecret');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
context: 'slack',
|
|
41
|
+
name,
|
|
42
|
+
token,
|
|
43
|
+
mode,
|
|
44
|
+
signingSecret,
|
|
45
|
+
appToken,
|
|
46
|
+
webhookPath: normalizeWebhookPath(config.webhookPath ?? entry?.webhookPath ?? '/slack/events'),
|
|
47
|
+
clientPingTimeout: config.clientPingTimeout
|
|
48
|
+
?? entry?.clientPingTimeout
|
|
49
|
+
?? 15_000,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function normalizeWebhookPath(path) {
|
|
53
|
+
const trimmed = path.trim() || '/slack/events';
|
|
54
|
+
return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
55
|
+
}
|
|
56
|
+
export function resolveSlackChannelType(event) {
|
|
57
|
+
return event.channel_type === 'im' ? 'private' : 'group';
|
|
58
|
+
}
|
|
59
|
+
/** Build inbound text for MessageGateway.receive. */
|
|
60
|
+
export function formatInboundContent(event) {
|
|
61
|
+
const text = typeof event.text === 'string' ? event.text : '';
|
|
62
|
+
if (text)
|
|
63
|
+
return mrkdwnToMarkdown(text);
|
|
64
|
+
if ('files' in event && Array.isArray(event.files) && event.files.length > 0) {
|
|
65
|
+
return '[file]';
|
|
66
|
+
}
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
export function formatInteractionContent(payload) {
|
|
70
|
+
const action = payload.actions?.[0];
|
|
71
|
+
if (!action)
|
|
72
|
+
return '[action]';
|
|
73
|
+
const label = action.text?.text ?? action.value ?? action.action_id;
|
|
74
|
+
return `[action: ${action.action_id}${label ? ` ${label}` : ''}]`;
|
|
75
|
+
}
|
|
76
|
+
export function formatSlashContent(cmd) {
|
|
77
|
+
return `${cmd.command} ${cmd.text}`.trim();
|
|
78
|
+
}
|
|
79
|
+
export function inboundMessageId(event) {
|
|
80
|
+
return `${event.channel}:${event.ts}`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Wire-encode an already-rendered outbound payload into Slack text + Block Kit blocks.
|
|
84
|
+
* Segment canonicalization is intentionally not done here.
|
|
85
|
+
*/
|
|
86
|
+
export function formatOutboundWire(payload) {
|
|
87
|
+
if (typeof payload === 'string') {
|
|
88
|
+
return { text: payload, blocks: [], attachments: [], files: [] };
|
|
89
|
+
}
|
|
90
|
+
const items = Array.isArray(payload)
|
|
91
|
+
? payload
|
|
92
|
+
: payload && typeof payload === 'object' && 'type' in payload
|
|
93
|
+
? [payload]
|
|
94
|
+
: [];
|
|
95
|
+
if (items.length === 0) {
|
|
96
|
+
const text = payload == null
|
|
97
|
+
? ''
|
|
98
|
+
: typeof payload === 'object'
|
|
99
|
+
? JSON.stringify(payload)
|
|
100
|
+
: String(payload);
|
|
101
|
+
return { text, blocks: [], attachments: [], files: [] };
|
|
102
|
+
}
|
|
103
|
+
let text = '';
|
|
104
|
+
const blocks = [];
|
|
105
|
+
const attachments = [];
|
|
106
|
+
const files = [];
|
|
107
|
+
for (const item of items) {
|
|
108
|
+
if (typeof item === 'string') {
|
|
109
|
+
text += item;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const data = item.data ?? {};
|
|
113
|
+
switch (item.type) {
|
|
114
|
+
case 'text':
|
|
115
|
+
text += String(data.text ?? data.content ?? '');
|
|
116
|
+
break;
|
|
117
|
+
case 'at':
|
|
118
|
+
case 'mention':
|
|
119
|
+
text += `<@${String(data.id ?? data.target ?? '')}>`;
|
|
120
|
+
break;
|
|
121
|
+
case 'channel_mention':
|
|
122
|
+
text += `<#${String(data.id ?? '')}>`;
|
|
123
|
+
break;
|
|
124
|
+
case 'link':
|
|
125
|
+
if (data.text && data.text !== data.url) {
|
|
126
|
+
text += `<${String(data.url)}|${String(data.text)}>`;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
text += `<${String(data.url ?? '')}>`;
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
case 'image':
|
|
133
|
+
if (typeof data.url === 'string' && data.url) {
|
|
134
|
+
attachments.push({
|
|
135
|
+
image_url: data.url,
|
|
136
|
+
title: String(data.name ?? data.title ?? ''),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else if (data.media && typeof data.media === 'object') {
|
|
140
|
+
files.push(resolveMediaToFile(data.media, String(data.alt ?? 'image')));
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
case 'audio':
|
|
144
|
+
case 'video':
|
|
145
|
+
case 'file':
|
|
146
|
+
if (data.media && typeof data.media === 'object') {
|
|
147
|
+
files.push(resolveMediaToFile(data.media, String(data.name ?? item.type)));
|
|
148
|
+
}
|
|
149
|
+
else if (data.file || data.url) {
|
|
150
|
+
files.push({
|
|
151
|
+
path: typeof data.file === 'string' ? data.file : undefined,
|
|
152
|
+
url: typeof data.url === 'string' ? data.url : undefined,
|
|
153
|
+
name: String(data.name ?? item.type),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case 'keyboard':
|
|
158
|
+
blocks.push(...keyboardToBlockKitBlocks(data));
|
|
159
|
+
break;
|
|
160
|
+
default:
|
|
161
|
+
text += String(data.text ?? `[${item.type}]`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return { text, blocks, attachments, files };
|
|
165
|
+
}
|
|
166
|
+
export function keyboardToBlockKitBlocks(data) {
|
|
167
|
+
const rows = data.rows;
|
|
168
|
+
if (!rows?.length)
|
|
169
|
+
return [];
|
|
170
|
+
const blocks = [];
|
|
171
|
+
for (const row of rows) {
|
|
172
|
+
const elements = row.slice(0, 5).map((btn, index) => ({
|
|
173
|
+
type: 'button',
|
|
174
|
+
text: { type: 'plain_text', text: String(btn.label ?? btn.text ?? 'button').slice(0, 75) },
|
|
175
|
+
action_id: String(btn.id ?? btn.action_id ?? `btn_${blocks.length}_${index}`),
|
|
176
|
+
...(btn.value != null ? { value: String(btn.value) } : {}),
|
|
177
|
+
...(btn.style === 'primary' ? { style: 'primary' } : {}),
|
|
178
|
+
...(btn.style === 'danger' ? { style: 'danger' } : {}),
|
|
179
|
+
}));
|
|
180
|
+
if (elements.length > 0) {
|
|
181
|
+
blocks.push({ type: 'actions', elements });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return blocks;
|
|
185
|
+
}
|
|
186
|
+
function resolveMediaToFile(media, name) {
|
|
187
|
+
if (media.kind === 'base64') {
|
|
188
|
+
return { buffer: Buffer.from(media.value, 'base64'), name };
|
|
189
|
+
}
|
|
190
|
+
return { url: media.value, name };
|
|
191
|
+
}
|
|
192
|
+
export function verifySlackSignature(signingSecret, rawBody, timestamp, signature) {
|
|
193
|
+
const now = Math.floor(Date.now() / 1000);
|
|
194
|
+
const ts = parseInt(timestamp, 10);
|
|
195
|
+
if (Number.isNaN(ts) || Math.abs(now - ts) > MAX_TIMESTAMP_DRIFT_SECONDS) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
const baseString = `${SLACK_SIG_VERSION}:${timestamp}:${rawBody}`;
|
|
199
|
+
const hmac = createHmac('sha256', signingSecret).update(baseString).digest('hex');
|
|
200
|
+
const expected = `${SLACK_SIG_VERSION}=${hmac}`;
|
|
201
|
+
if (expected.length !== signature.length)
|
|
202
|
+
return false;
|
|
203
|
+
return timingSafeEqual(Buffer.from(expected), Buffer.from(signature));
|
|
204
|
+
}
|
|
205
|
+
export async function readTextBody(request, options = {}) {
|
|
206
|
+
const limit = options.limit ?? 1_048_576;
|
|
207
|
+
const chunks = [];
|
|
208
|
+
let size = 0;
|
|
209
|
+
for await (const chunk of request) {
|
|
210
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
211
|
+
size += buffer.length;
|
|
212
|
+
if (size > limit) {
|
|
213
|
+
request.destroy();
|
|
214
|
+
throw new Error(`Request body exceeds ${limit} bytes`);
|
|
215
|
+
}
|
|
216
|
+
chunks.push(buffer);
|
|
217
|
+
}
|
|
218
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
219
|
+
}
|
|
220
|
+
export function headerValue(headers, name) {
|
|
221
|
+
const raw = headers[name.toLowerCase()];
|
|
222
|
+
if (Array.isArray(raw))
|
|
223
|
+
return raw[0] ?? '';
|
|
224
|
+
return raw ?? '';
|
|
225
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent tool deps for slack.
|
|
3
|
+
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
+
*/
|
|
5
|
+
export interface SlackAgentEndpoint {
|
|
6
|
+
inviteToChannel(channel: string, users: string[]): Promise<boolean>;
|
|
7
|
+
kickFromChannel(channel: string, user: string): Promise<boolean>;
|
|
8
|
+
setChannelTopic(channel: string, topic: string): Promise<boolean>;
|
|
9
|
+
setChannelPurpose(channel: string, purpose: string): Promise<boolean>;
|
|
10
|
+
archiveChannel(channel: string): Promise<boolean>;
|
|
11
|
+
unarchiveChannel(channel: string): Promise<boolean>;
|
|
12
|
+
renameChannel(channel: string, name: string): Promise<boolean>;
|
|
13
|
+
getChannelMembers(channel: string): Promise<string[]>;
|
|
14
|
+
getChannelInfo(channel: string): Promise<unknown>;
|
|
15
|
+
getUserInfo(user: string): Promise<unknown>;
|
|
16
|
+
addReaction(channel: string, timestamp: string, name: string): Promise<boolean>;
|
|
17
|
+
removeReaction(channel: string, timestamp: string, name: string): Promise<boolean>;
|
|
18
|
+
pinMessage(channel: string, timestamp: string): Promise<boolean>;
|
|
19
|
+
unpinMessage(channel: string, timestamp: string): Promise<boolean>;
|
|
20
|
+
editMessage(channel: string, messageTs: string, content: unknown): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export interface SlackAgentDeps {
|
|
23
|
+
getEndpoint: (endpointId: string) => SlackAgentEndpoint;
|
|
24
|
+
}
|
|
25
|
+
export declare function registerSlackAgentEndpoint(endpointId: string, endpoint: SlackAgentEndpoint): () => void;
|
|
26
|
+
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
27
|
+
export declare function setSlackAgentDeps(deps: SlackAgentDeps | null): void;
|
|
28
|
+
export declare function getSlackAgentDeps(): SlackAgentDeps;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent tool deps for slack.
|
|
3
|
+
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
+
*/
|
|
5
|
+
const endpoints = new Map();
|
|
6
|
+
let override = null;
|
|
7
|
+
export function registerSlackAgentEndpoint(endpointId, endpoint) {
|
|
8
|
+
endpoints.set(endpointId, endpoint);
|
|
9
|
+
return () => {
|
|
10
|
+
if (endpoints.get(endpointId) === endpoint) {
|
|
11
|
+
endpoints.delete(endpointId);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
16
|
+
export function setSlackAgentDeps(deps) {
|
|
17
|
+
override = deps;
|
|
18
|
+
}
|
|
19
|
+
export function getSlackAgentDeps() {
|
|
20
|
+
if (override)
|
|
21
|
+
return override;
|
|
22
|
+
return {
|
|
23
|
+
getEndpoint(endpointId) {
|
|
24
|
+
const registered = endpoints.get(endpointId);
|
|
25
|
+
if (!registered)
|
|
26
|
+
throw new Error(`Endpoint ${endpointId} 不存在`);
|
|
27
|
+
return registered;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SlackMessageEvent } from './protocol.js';
|
|
2
|
+
export interface SlackInboundFilterState {
|
|
3
|
+
seenInbound: Map<string, number>;
|
|
4
|
+
}
|
|
5
|
+
export declare function createSlackInboundFilterState(): SlackInboundFilterState;
|
|
6
|
+
/**
|
|
7
|
+
* 过滤不应进入 IM 管道的 Slack 入站消息。
|
|
8
|
+
* - 跳过 Bot 自身消息
|
|
9
|
+
* - 频道内 @Bot:只保留 app_mention,丢弃重复的 message 事件
|
|
10
|
+
* - channel:ts 去重(message + app_mention 等同一条)
|
|
11
|
+
*/
|
|
12
|
+
export declare function shouldDropSlackInboundMessage(event: SlackMessageEvent, state: SlackInboundFilterState, botUserId?: string): boolean;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const DEDUPE_TTL_MS = 60_000;
|
|
2
|
+
export function createSlackInboundFilterState() {
|
|
3
|
+
return { seenInbound: new Map() };
|
|
4
|
+
}
|
|
5
|
+
function pruneSeen(state, now) {
|
|
6
|
+
if (state.seenInbound.size < 512)
|
|
7
|
+
return;
|
|
8
|
+
for (const [key, at] of state.seenInbound) {
|
|
9
|
+
if (now - at > DEDUPE_TTL_MS) {
|
|
10
|
+
state.seenInbound.delete(key);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 过滤不应进入 IM 管道的 Slack 入站消息。
|
|
16
|
+
* - 跳过 Bot 自身消息
|
|
17
|
+
* - 频道内 @Bot:只保留 app_mention,丢弃重复的 message 事件
|
|
18
|
+
* - channel:ts 去重(message + app_mention 等同一条)
|
|
19
|
+
*/
|
|
20
|
+
export function shouldDropSlackInboundMessage(event, state, botUserId) {
|
|
21
|
+
if (event.subtype === 'bot_message' || event.subtype === 'message_changed') {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (event.bot_id)
|
|
25
|
+
return true;
|
|
26
|
+
if (botUserId && event.user === botUserId)
|
|
27
|
+
return true;
|
|
28
|
+
const channelType = event.channel_type ?? 'channel';
|
|
29
|
+
const text = event.text ?? '';
|
|
30
|
+
if (event.type === 'message' && channelType !== 'im' && botUserId) {
|
|
31
|
+
if (text.includes(`<@${botUserId}>`)) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (!event.channel || !event.ts)
|
|
36
|
+
return true;
|
|
37
|
+
const key = `${event.channel}:${event.ts}`;
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
const seenAt = state.seenInbound.get(key);
|
|
40
|
+
if (seenAt != null && now - seenAt < DEDUPE_TTL_MS) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
state.seenInbound.set(key, now);
|
|
44
|
+
pruneSeen(state, now);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Slack 消息定位:channel + ts(Activity Feedback / recall / reaction 共用) */
|
|
2
|
+
export declare function formatSlackMessageRef(channel: string, ts: string): string;
|
|
3
|
+
export declare function parseSlackMessageRef(ref: string): {
|
|
4
|
+
channel: string;
|
|
5
|
+
ts: string;
|
|
6
|
+
} | null;
|
|
7
|
+
export declare function slackMessageTs(ref: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Slack 消息定位:channel + ts(Activity Feedback / recall / reaction 共用) */
|
|
2
|
+
export function formatSlackMessageRef(channel, ts) {
|
|
3
|
+
return `${channel}:${ts}`;
|
|
4
|
+
}
|
|
5
|
+
export function parseSlackMessageRef(ref) {
|
|
6
|
+
const sep = ref.indexOf(':');
|
|
7
|
+
if (sep <= 0)
|
|
8
|
+
return null;
|
|
9
|
+
const channel = ref.slice(0, sep);
|
|
10
|
+
const ts = ref.slice(sep + 1);
|
|
11
|
+
if (!channel || !ts)
|
|
12
|
+
return null;
|
|
13
|
+
return { channel, ts };
|
|
14
|
+
}
|
|
15
|
+
export function slackMessageTs(ref) {
|
|
16
|
+
return parseSlackMessageRef(ref)?.ts ?? ref;
|
|
17
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack outbound — chat.postMessage + Block Kit + files.uploadV2
|
|
3
|
+
*/
|
|
4
|
+
import type { Logger } from '@zhin.js/logger';
|
|
5
|
+
import { keyboardToBlockKitBlocks } from './protocol.js';
|
|
6
|
+
export interface SlackOutboundResult {
|
|
7
|
+
ts: string;
|
|
8
|
+
}
|
|
9
|
+
export interface SlackSendOptions {
|
|
10
|
+
channel: string;
|
|
11
|
+
threadTs?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface SlackChatClient {
|
|
14
|
+
chat: {
|
|
15
|
+
postMessage(opts: Record<string, unknown>): Promise<{
|
|
16
|
+
ts?: string;
|
|
17
|
+
}>;
|
|
18
|
+
update(opts: Record<string, unknown>): Promise<unknown>;
|
|
19
|
+
};
|
|
20
|
+
filesUploadV2?(opts: Record<string, unknown>): Promise<unknown>;
|
|
21
|
+
}
|
|
22
|
+
export declare function sendSlackContent(client: SlackChatClient, content: unknown, opts: SlackSendOptions, logger: Logger): Promise<SlackOutboundResult>;
|
|
23
|
+
export declare function editSlackContent(client: SlackChatClient, channel: string, ts: string, content: unknown): Promise<void>;
|
|
24
|
+
export { keyboardToBlockKitBlocks };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { markdownToMrkdwn, mrkdwnToPlainFallback, splitMrkdwnText } from './markdown-to-mrkdwn.js';
|
|
2
|
+
import { formatOutboundWire, keyboardToBlockKitBlocks } from './protocol.js';
|
|
3
|
+
const SLACK_MAX_BLOCKS_PER_MESSAGE = 48;
|
|
4
|
+
export async function sendSlackContent(client, content, opts, logger) {
|
|
5
|
+
const wire = formatOutboundWire(content);
|
|
6
|
+
const blocks = [...wire.blocks];
|
|
7
|
+
const textDelivery = applyTextMrkdwnBlocks(wire.text, blocks);
|
|
8
|
+
for (const pf of wire.files) {
|
|
9
|
+
try {
|
|
10
|
+
await uploadFile(client, opts.channel, pf, opts.threadTs, logger);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
logger.error('Failed to upload file:', e);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return postSlackMessage(client, {
|
|
17
|
+
channel: opts.channel,
|
|
18
|
+
threadTs: opts.threadTs,
|
|
19
|
+
blocks,
|
|
20
|
+
attachments: wire.attachments,
|
|
21
|
+
fallbackText: textDelivery.fallbackText,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export async function editSlackContent(client, channel, ts, content) {
|
|
25
|
+
const wire = formatOutboundWire(content);
|
|
26
|
+
const blocks = [...wire.blocks];
|
|
27
|
+
const textDelivery = applyTextMrkdwnBlocks(wire.text, blocks);
|
|
28
|
+
const payloadBlocks = blocks.slice(0, SLACK_MAX_BLOCKS_PER_MESSAGE);
|
|
29
|
+
const updateOpts = {
|
|
30
|
+
channel,
|
|
31
|
+
ts,
|
|
32
|
+
text: textDelivery.fallbackText || ' ',
|
|
33
|
+
};
|
|
34
|
+
if (payloadBlocks.length > 0)
|
|
35
|
+
updateOpts.blocks = payloadBlocks;
|
|
36
|
+
await client.chat.update(updateOpts);
|
|
37
|
+
}
|
|
38
|
+
function applyTextMrkdwnBlocks(textContent, blocks) {
|
|
39
|
+
const trimmed = textContent.trim();
|
|
40
|
+
if (!trimmed)
|
|
41
|
+
return { fallbackText: '' };
|
|
42
|
+
const mrkdwn = markdownToMrkdwn(trimmed);
|
|
43
|
+
const sections = splitMrkdwnText(mrkdwn).map((chunk) => ({
|
|
44
|
+
type: 'section',
|
|
45
|
+
text: { type: 'mrkdwn', text: chunk },
|
|
46
|
+
}));
|
|
47
|
+
if (blocks.length > 0) {
|
|
48
|
+
blocks.unshift(...sections);
|
|
49
|
+
return { fallbackText: '' };
|
|
50
|
+
}
|
|
51
|
+
blocks.push(...sections);
|
|
52
|
+
return { fallbackText: mrkdwnToPlainFallback(mrkdwn) };
|
|
53
|
+
}
|
|
54
|
+
async function postSlackMessage(client, opts) {
|
|
55
|
+
const { channel, blocks, attachments, fallbackText } = opts;
|
|
56
|
+
let threadTs = opts.threadTs;
|
|
57
|
+
let firstTs = '';
|
|
58
|
+
if (blocks.length === 0) {
|
|
59
|
+
const result = await client.chat.postMessage({
|
|
60
|
+
channel,
|
|
61
|
+
text: fallbackText || 'Message',
|
|
62
|
+
...(threadTs ? { thread_ts: threadTs } : {}),
|
|
63
|
+
...(attachments.length > 0 ? { attachments } : {}),
|
|
64
|
+
});
|
|
65
|
+
return { ts: result.ts ?? '' };
|
|
66
|
+
}
|
|
67
|
+
for (let offset = 0; offset < blocks.length; offset += SLACK_MAX_BLOCKS_PER_MESSAGE) {
|
|
68
|
+
const chunk = blocks.slice(offset, offset + SLACK_MAX_BLOCKS_PER_MESSAGE);
|
|
69
|
+
const result = await client.chat.postMessage({
|
|
70
|
+
channel,
|
|
71
|
+
text: offset === 0 ? (fallbackText || ' ') : ' ',
|
|
72
|
+
blocks: chunk,
|
|
73
|
+
...(threadTs ? { thread_ts: threadTs } : {}),
|
|
74
|
+
...(offset === 0 && attachments.length > 0 ? { attachments } : {}),
|
|
75
|
+
});
|
|
76
|
+
const ts = result.ts ?? '';
|
|
77
|
+
if (!firstTs)
|
|
78
|
+
firstTs = ts;
|
|
79
|
+
if (!threadTs)
|
|
80
|
+
threadTs = ts;
|
|
81
|
+
}
|
|
82
|
+
return { ts: firstTs };
|
|
83
|
+
}
|
|
84
|
+
async function uploadFile(client, channel, file, threadTs, logger) {
|
|
85
|
+
if (!client.filesUploadV2)
|
|
86
|
+
return;
|
|
87
|
+
try {
|
|
88
|
+
let buffer = file.buffer;
|
|
89
|
+
if (!buffer && file.path) {
|
|
90
|
+
const { readFile } = await import('node:fs/promises');
|
|
91
|
+
buffer = await readFile(file.path);
|
|
92
|
+
}
|
|
93
|
+
if (!buffer && file.url) {
|
|
94
|
+
const res = await fetch(file.url);
|
|
95
|
+
if (!res.ok)
|
|
96
|
+
throw new Error(`fetch ${file.url}: ${res.status}`);
|
|
97
|
+
buffer = Buffer.from(await res.arrayBuffer());
|
|
98
|
+
}
|
|
99
|
+
if (!buffer)
|
|
100
|
+
return;
|
|
101
|
+
await client.filesUploadV2(threadTs
|
|
102
|
+
? { channel_id: channel, file: buffer, filename: file.name ?? 'file', thread_ts: threadTs }
|
|
103
|
+
: { channel_id: channel, file: buffer, filename: file.name ?? 'file' });
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
logger?.error('File upload failed:', e);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export { keyboardToBlockKitBlocks };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function normalizeSlackReactionName(emoji: string): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** 将 activity-feedback / 工具入参规范为 Slack reactions.add 的 name(不含冒号) */
|
|
2
|
+
const UNICODE_TO_SLACK = {
|
|
3
|
+
'⏳': 'hourglass_flowing_sand',
|
|
4
|
+
'✅': 'white_check_mark',
|
|
5
|
+
'❌': 'x',
|
|
6
|
+
'⏰': 'alarm_clock',
|
|
7
|
+
'🤔': 'thinking_face',
|
|
8
|
+
'👀': 'eyes',
|
|
9
|
+
};
|
|
10
|
+
export function normalizeSlackReactionName(emoji) {
|
|
11
|
+
const trimmed = emoji.trim();
|
|
12
|
+
if (!trimmed)
|
|
13
|
+
return 'hourglass_flowing_sand';
|
|
14
|
+
if (UNICODE_TO_SLACK[trimmed])
|
|
15
|
+
return UNICODE_TO_SLACK[trimmed];
|
|
16
|
+
if (trimmed.startsWith(':') && trimmed.endsWith(':') && trimmed.length > 2) {
|
|
17
|
+
return trimmed.slice(1, -1);
|
|
18
|
+
}
|
|
19
|
+
let start = 0;
|
|
20
|
+
let end = trimmed.length;
|
|
21
|
+
while (start < end && trimmed[start] === ':')
|
|
22
|
+
start++;
|
|
23
|
+
while (end > start && trimmed[end - 1] === ':')
|
|
24
|
+
end--;
|
|
25
|
+
return trimmed.slice(start, end);
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function postSlackEphemeral(responseUrl, text, logger) {
|
|
2
|
+
if (!responseUrl?.trim())
|
|
3
|
+
return;
|
|
4
|
+
const body = JSON.stringify({
|
|
5
|
+
response_type: 'ephemeral',
|
|
6
|
+
text,
|
|
7
|
+
replace_original: false,
|
|
8
|
+
});
|
|
9
|
+
fetch(responseUrl, {
|
|
10
|
+
method: 'POST',
|
|
11
|
+
headers: { 'Content-Type': 'application/json' },
|
|
12
|
+
body,
|
|
13
|
+
}).catch((err) => {
|
|
14
|
+
logger?.debug(`Slack response_url failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
15
|
+
});
|
|
16
|
+
}
|
package/lib/webhook.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack webhook HTTP: signature → parse → admit.
|
|
3
|
+
*/
|
|
4
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
+
import type { HttpHost, HttpRouteRegistration } from '@zhin.js/host-http';
|
|
6
|
+
import { type ResolvedSlackConfig, type SlackInteractionPayload, type SlackSlashCommand } from './protocol.js';
|
|
7
|
+
export interface SlackWebhookHandler {
|
|
8
|
+
readonly config: ResolvedSlackConfig;
|
|
9
|
+
handleEnvelope(body: unknown): void;
|
|
10
|
+
admitInteraction(payload: SlackInteractionPayload): void;
|
|
11
|
+
admitSlashCommand(cmd: SlackSlashCommand): void;
|
|
12
|
+
}
|
|
13
|
+
export declare function registerSlackWebhookRoutes(http: HttpHost, handler: SlackWebhookHandler): HttpRouteRegistration[];
|
|
14
|
+
export declare function handleSlackWebhookRequest(request: IncomingMessage, response: ServerResponse, handler: SlackWebhookHandler): Promise<void>;
|
package/lib/webhook.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { getLogger } from '@zhin.js/logger';
|
|
2
|
+
import { headerValue, readTextBody, verifySlackSignature, } from './protocol.js';
|
|
3
|
+
const logger = getLogger('slack');
|
|
4
|
+
export function registerSlackWebhookRoutes(http, handler) {
|
|
5
|
+
const path = handler.config.webhookPath;
|
|
6
|
+
return [
|
|
7
|
+
http.route('POST', path, async (request, response) => {
|
|
8
|
+
await handleSlackWebhookRequest(request, response, handler);
|
|
9
|
+
}, { summary: 'Slack Events API / Interactivity', tags: ['slack'] }),
|
|
10
|
+
];
|
|
11
|
+
}
|
|
12
|
+
export async function handleSlackWebhookRequest(request, response, handler) {
|
|
13
|
+
try {
|
|
14
|
+
const rawBody = await readTextBody(request);
|
|
15
|
+
const timestamp = headerValue(request.headers, 'x-slack-request-timestamp');
|
|
16
|
+
const signature = headerValue(request.headers, 'x-slack-signature');
|
|
17
|
+
if (!verifySlackSignature(handler.config.signingSecret, rawBody, timestamp, signature)) {
|
|
18
|
+
response.writeHead(401, { 'Content-Type': 'text/plain' });
|
|
19
|
+
response.end('Invalid signature');
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const contentType = headerValue(request.headers, 'content-type');
|
|
23
|
+
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
24
|
+
const params = new URLSearchParams(rawBody);
|
|
25
|
+
const payloadStr = params.get('payload');
|
|
26
|
+
if (payloadStr) {
|
|
27
|
+
response.writeHead(200);
|
|
28
|
+
response.end('');
|
|
29
|
+
handler.admitInteraction(JSON.parse(payloadStr));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const body = Object.fromEntries(params);
|
|
33
|
+
if (body.command) {
|
|
34
|
+
response.writeHead(200);
|
|
35
|
+
response.end('');
|
|
36
|
+
handler.admitSlashCommand(body);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let envelope;
|
|
41
|
+
try {
|
|
42
|
+
envelope = JSON.parse(rawBody);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
response.writeHead(200);
|
|
46
|
+
response.end('');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (envelope.type === 'url_verification') {
|
|
50
|
+
const challenge = envelope.challenge;
|
|
51
|
+
response.writeHead(200, { 'Content-Type': 'application/json' });
|
|
52
|
+
response.end(JSON.stringify({ challenge }));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (envelope.type === 'event_callback' && envelope.event) {
|
|
56
|
+
response.writeHead(200);
|
|
57
|
+
response.end('');
|
|
58
|
+
handler.handleEnvelope(envelope);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
response.writeHead(200);
|
|
62
|
+
response.end('');
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
logger.error('Slack webhook error:', error);
|
|
66
|
+
response.writeHead(200);
|
|
67
|
+
response.end('');
|
|
68
|
+
}
|
|
69
|
+
}
|