@objectstack/connector-slack 7.4.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/.turbo/turbo-build.log +22 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE +93 -0
- package/dist/index.d.mts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +166 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -0
- package/src/connector-slack-plugin.test.ts +78 -0
- package/src/connector-slack-plugin.ts +79 -0
- package/src/index.ts +28 -0
- package/src/slack-connector.test.ts +132 -0
- package/src/slack-connector.ts +210 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import type { Connector } from '@objectstack/spec/integration';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Slack connector — a *concrete* connector (ADR-0018 §Addendum) and the second
|
|
7
|
+
* reference implementation after `@objectstack/connector-rest`. It produces a
|
|
8
|
+
* {@link Connector} definition plus handlers for a small set of Slack Web API
|
|
9
|
+
* actions, which the baseline `connector_action` node dispatches to.
|
|
10
|
+
*
|
|
11
|
+
* Scope (ADR-0022 "raw API call" path): this is the *integration mechanism*
|
|
12
|
+
* for talking to Slack's API — "post this exact text to this channel". It is
|
|
13
|
+
* deliberately **not** the human-notification layer: there is no preference
|
|
14
|
+
* matrix, inbox, outbox, or thread/session semantics here. Those belong to a
|
|
15
|
+
* `MessagingChannel` (ADR-0012/0013), which may itself delegate its transport
|
|
16
|
+
* to this connector.
|
|
17
|
+
*
|
|
18
|
+
* Open-source scope: **static** auth only — a Slack **bot token** (`xoxb-…`),
|
|
19
|
+
* supplied by the caller and sent as a bearer credential. OAuth2 install/refresh,
|
|
20
|
+
* credential vaulting, and multi-tenant connection lifecycle are the enterprise
|
|
21
|
+
* tier (see `../cloud/docs/design/connector-tiering.md`) and are out of scope.
|
|
22
|
+
*
|
|
23
|
+
* Slack quirk: the Web API returns HTTP `200` even on logical failure, with the
|
|
24
|
+
* real outcome in the JSON body's `ok` field (and `error` on failure). Handlers
|
|
25
|
+
* therefore surface `ok` from the payload, not from the HTTP status, and never
|
|
26
|
+
* throw on a logical failure — the flow author branches on `${node.ok}`.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export interface SlackConnectorOptions {
|
|
30
|
+
/** Connector machine name (snake_case). Defaults to `slack`. */
|
|
31
|
+
name?: string;
|
|
32
|
+
/** Human-readable label. Defaults to `Slack`. */
|
|
33
|
+
label?: string;
|
|
34
|
+
/** Slack bot token (`xoxb-…`), sent as `Authorization: Bearer <token>`. */
|
|
35
|
+
token: string;
|
|
36
|
+
/** Web API base URL. Defaults to `https://slack.com/api`. */
|
|
37
|
+
baseUrl?: string;
|
|
38
|
+
/** Headers merged into every request (request-level headers win). */
|
|
39
|
+
defaultHeaders?: Record<string, string>;
|
|
40
|
+
/** Injected for tests; defaults to the global `fetch`. */
|
|
41
|
+
fetchImpl?: typeof fetch;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Input accepted by `chat.postMessage`. */
|
|
45
|
+
export interface SlackPostMessageInput {
|
|
46
|
+
/** Channel id (`C…`), user id (`U…`), or channel name (`#general`). */
|
|
47
|
+
channel: string;
|
|
48
|
+
/** Message text (fallback when `blocks` are present). */
|
|
49
|
+
text?: string;
|
|
50
|
+
/** Thread root `ts` to reply into an existing thread. */
|
|
51
|
+
thread_ts?: string;
|
|
52
|
+
/** Block Kit blocks. */
|
|
53
|
+
blocks?: unknown[];
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Generic Slack Web API result envelope. */
|
|
58
|
+
export interface SlackResult {
|
|
59
|
+
/** Slack's logical success flag (from the JSON body, not the HTTP status). */
|
|
60
|
+
ok: boolean;
|
|
61
|
+
/** HTTP status (almost always 200 for the Slack Web API). */
|
|
62
|
+
status: number;
|
|
63
|
+
/** Full parsed Slack payload. */
|
|
64
|
+
body: Record<string, unknown>;
|
|
65
|
+
/** Slack error code when `ok` is false (e.g. `channel_not_found`). */
|
|
66
|
+
error?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A connector definition paired with its action handlers, ready for registerConnector(). */
|
|
70
|
+
export interface SlackConnectorBundle {
|
|
71
|
+
def: Connector;
|
|
72
|
+
handlers: Record<
|
|
73
|
+
string,
|
|
74
|
+
(input: Record<string, unknown>, ctx: unknown) => Promise<Record<string, unknown>>
|
|
75
|
+
>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function createSlackConnector(opts: SlackConnectorOptions): SlackConnectorBundle {
|
|
79
|
+
const name = opts.name ?? 'slack';
|
|
80
|
+
const baseUrl = (opts.baseUrl ?? 'https://slack.com/api').replace(/\/+$/, '');
|
|
81
|
+
const doFetch = opts.fetchImpl ?? fetch;
|
|
82
|
+
|
|
83
|
+
const def: Connector = {
|
|
84
|
+
name,
|
|
85
|
+
label: opts.label ?? 'Slack',
|
|
86
|
+
type: 'api',
|
|
87
|
+
description: 'Slack Web API connector (static bot-token auth). Post and update messages, or call any Web API method.',
|
|
88
|
+
icon: 'slack',
|
|
89
|
+
authentication: { type: 'bearer', token: opts.token },
|
|
90
|
+
// Defaulted by ConnectorSchema; set explicitly so the literal satisfies
|
|
91
|
+
// the (post-parse) Connector output type.
|
|
92
|
+
status: 'active',
|
|
93
|
+
enabled: true,
|
|
94
|
+
connectionTimeoutMs: 30000,
|
|
95
|
+
requestTimeoutMs: 30000,
|
|
96
|
+
actions: [
|
|
97
|
+
{
|
|
98
|
+
key: 'chat.postMessage',
|
|
99
|
+
label: 'Post Message',
|
|
100
|
+
description: 'Post a message to a channel, DM, or thread (Slack chat.postMessage).',
|
|
101
|
+
inputSchema: {
|
|
102
|
+
type: 'object',
|
|
103
|
+
required: ['channel'],
|
|
104
|
+
properties: {
|
|
105
|
+
channel: { type: 'string', description: 'Channel id, user id, or #name' },
|
|
106
|
+
text: { type: 'string', description: 'Message text' },
|
|
107
|
+
thread_ts: { type: 'string', description: 'Thread root ts to reply into' },
|
|
108
|
+
blocks: { type: 'array', description: 'Block Kit blocks' },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
outputSchema: slackOutputSchema(),
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
key: 'chat.update',
|
|
115
|
+
label: 'Update Message',
|
|
116
|
+
description: 'Edit an existing message (Slack chat.update).',
|
|
117
|
+
inputSchema: {
|
|
118
|
+
type: 'object',
|
|
119
|
+
required: ['channel', 'ts'],
|
|
120
|
+
properties: {
|
|
121
|
+
channel: { type: 'string', description: 'Channel id' },
|
|
122
|
+
ts: { type: 'string', description: 'Timestamp of the message to update' },
|
|
123
|
+
text: { type: 'string', description: 'New message text' },
|
|
124
|
+
blocks: { type: 'array', description: 'New Block Kit blocks' },
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
outputSchema: slackOutputSchema(),
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
key: 'call',
|
|
131
|
+
label: 'Call Web API Method',
|
|
132
|
+
description: 'Escape hatch — call any Slack Web API method with arbitrary params.',
|
|
133
|
+
inputSchema: {
|
|
134
|
+
type: 'object',
|
|
135
|
+
required: ['method'],
|
|
136
|
+
properties: {
|
|
137
|
+
method: { type: 'string', description: 'Web API method, e.g. conversations.list' },
|
|
138
|
+
params: { type: 'object', description: 'Method parameters (JSON body)' },
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
outputSchema: slackOutputSchema(),
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** POST a JSON body to a Slack Web API method and normalise the result. */
|
|
147
|
+
async function callSlack(method: string, params: Record<string, unknown>): Promise<SlackResult> {
|
|
148
|
+
const headers: Record<string, string> = {
|
|
149
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
150
|
+
...opts.defaultHeaders,
|
|
151
|
+
Authorization: `Bearer ${opts.token}`,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const response = await doFetch(`${baseUrl}/${method}`, {
|
|
155
|
+
method: 'POST',
|
|
156
|
+
headers,
|
|
157
|
+
body: JSON.stringify(params),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// The Slack Web API always answers with JSON; `ok` is the real outcome.
|
|
161
|
+
const body = (await response.json()) as Record<string, unknown>;
|
|
162
|
+
const ok = body.ok === true;
|
|
163
|
+
return {
|
|
164
|
+
ok,
|
|
165
|
+
status: response.status,
|
|
166
|
+
body,
|
|
167
|
+
error: ok ? undefined : (body.error as string | undefined),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function postMessage(input: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
172
|
+
return toRecord(await callSlack('chat.postMessage', input));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function update(input: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
176
|
+
return toRecord(await callSlack('chat.update', input));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function call(input: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
180
|
+
const method = String(input.method ?? '');
|
|
181
|
+
if (!method) throw new Error("slack 'call' action: 'method' is required");
|
|
182
|
+
const params = (input.params as Record<string, unknown>) ?? {};
|
|
183
|
+
return toRecord(await callSlack(method, params));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
def,
|
|
188
|
+
handlers: {
|
|
189
|
+
'chat.postMessage': postMessage,
|
|
190
|
+
'chat.update': update,
|
|
191
|
+
call,
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function toRecord(r: SlackResult): Record<string, unknown> {
|
|
197
|
+
return { ok: r.ok, status: r.status, body: r.body, error: r.error };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function slackOutputSchema(): Record<string, unknown> {
|
|
201
|
+
return {
|
|
202
|
+
type: 'object',
|
|
203
|
+
properties: {
|
|
204
|
+
ok: { type: 'boolean', description: "Slack's logical success flag" },
|
|
205
|
+
status: { type: 'number', description: 'HTTP status' },
|
|
206
|
+
body: { type: 'object', description: 'Full Slack response payload' },
|
|
207
|
+
error: { type: 'string', description: 'Slack error code when ok is false' },
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|