@meetopenbot/slack 1.3.0 → 1.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/README.md +6 -6
- package/dist/config.js +6 -1
- package/dist/index.js +10 -10
- package/dist/invoke.js +29 -30
- package/dist/message-kind.js +19 -0
- package/dist/webhook.js +20 -16
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Slack specialist agent for OpenBot: Events API ingress, simple LLM replies, and
|
|
|
15
15
|
A workspace owner clicks **Connect** on the Slack agent and approves "Add to Slack". That installs OpenBot's Slack app in their Slack workspace. OpenBot keeps the bot token, and the agent gets it on every run. Nothing to paste. The plugin asks for these bot scopes:
|
|
16
16
|
|
|
17
17
|
- `app_mentions:read`, `im:history`: mentions and DMs
|
|
18
|
-
- `channels:read`, `channels:history`, `users:read`, `users.profile:read`: reading channels, threads and people
|
|
18
|
+
- `channels:read`, `channels:history`, `groups:history`, `users:read`, `users.profile:read`: reading channels, threads (public and private) and people
|
|
19
19
|
- `chat:write`, `reactions:write`: replying and reacting
|
|
20
20
|
|
|
21
21
|
On **OpenBot Cloud** this works as is. A **self-hosted** runtime brings its own Slack app:
|
|
@@ -37,7 +37,7 @@ On cloud, the model runs on your workspace credits by default. For BYOK, add `AN
|
|
|
37
37
|
|
|
38
38
|
### Channel routing (webhook ingress)
|
|
39
39
|
|
|
40
|
-
When a Slack message arrives via the Events API webhook, the
|
|
40
|
+
When a Slack message arrives via the Events API webhook, the runtime answers Slack at once and opens an OpenBot thread for the Slack thread. The Slack agent hands the message to the OpenBot **system** agent there and posts each reply back into the Slack thread. Set `channelMappings` as comma-separated `slackChannelId:openbotChannelId` pairs to pick the OpenBot channel:
|
|
41
41
|
|
|
42
42
|
```
|
|
43
43
|
C01234567:engineering,C89ABCDEF:support,D0789ABC:slack-dms
|
|
@@ -52,7 +52,9 @@ This applies only to **webhook ingress delegation**, not Slack MCP tools.
|
|
|
52
52
|
|
|
53
53
|
### Events (inbound mentions)
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
The bot answers when it is tagged and in DMs. Once tagged in a thread, it also answers later replies in that thread without a tag. It stays quiet on replies that tag someone else, and never joins threads it wasn't tagged in.
|
|
56
|
+
|
|
57
|
+
On **OpenBot Cloud**, mentions, DMs and thread replies arrive on their own once the workspace connects Slack. OpenBot's Slack app sends every event to OpenBot Cloud. The cloud checks Slack's signature and forwards the event to the workspace that connected that Slack team, marked `verified`. Disconnecting stops the events. So does removing the app from Slack.
|
|
56
58
|
|
|
57
59
|
A **self-hosted** app sends its events straight to the runtime:
|
|
58
60
|
|
|
@@ -64,9 +66,7 @@ A **self-hosted** app sends its events straight to the runtime:
|
|
|
64
66
|
https://<your-host>/api/webhooks/slack
|
|
65
67
|
```
|
|
66
68
|
|
|
67
|
-
3. Subscribe to **`app_mention`** for
|
|
68
|
-
|
|
69
|
-
Do **not** subscribe to `message.channels` or `message.groups` — that causes duplicate replies alongside `app_mention`.
|
|
69
|
+
3. Subscribe the bot to **`app_mention`** for mentions, **`message.im`** for DMs, and **`message.channels`** / **`message.groups`** for untagged replies in threads the bot is in (public / private channels). The plugin handles a tagged reply once, even though Slack sends it as both `app_mention` and `message`.
|
|
70
70
|
|
|
71
71
|
## Build
|
|
72
72
|
|
package/dist/config.js
CHANGED
|
@@ -54,12 +54,17 @@ export function readSlackConfig(config) {
|
|
|
54
54
|
export function resolveSlackSigningSecret() {
|
|
55
55
|
return lookupSecret({ envKeys: ["SLACK_SIGNING_SECRET"] });
|
|
56
56
|
}
|
|
57
|
-
/**
|
|
57
|
+
/**
|
|
58
|
+
* Bot scopes asked for at sign-in: the MCP tools plus mention, DM and thread-reply ingress.
|
|
59
|
+
* `channels:history` and `groups:history` let the bot read replies in public and private
|
|
60
|
+
* channel threads it is in.
|
|
61
|
+
*/
|
|
58
62
|
export const SLACK_BOT_SCOPES = [
|
|
59
63
|
"app_mentions:read",
|
|
60
64
|
"channels:history",
|
|
61
65
|
"channels:read",
|
|
62
66
|
"chat:write",
|
|
67
|
+
"groups:history",
|
|
63
68
|
"im:history",
|
|
64
69
|
"reactions:write",
|
|
65
70
|
"users:read",
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { definePlugin
|
|
1
|
+
import { definePlugin } from "@meetopenbot/plugin-sdk";
|
|
2
2
|
import { pluginConfigSchema, readSlackConfig, SLACK_BOT_SCOPES, slackCredentials, } from "./config.js";
|
|
3
|
-
import { handleSlackInvoke
|
|
3
|
+
import { handleSlackInvoke } from "./invoke.js";
|
|
4
4
|
import { handleSlackWebhook } from "./webhook.js";
|
|
5
5
|
export const plugin = definePlugin({
|
|
6
6
|
name: "Slack",
|
|
@@ -23,6 +23,14 @@ export const plugin = definePlugin({
|
|
|
23
23
|
event: event,
|
|
24
24
|
ctx: { state },
|
|
25
25
|
config: slackConfig,
|
|
26
|
+
agentId: context.agentId,
|
|
27
|
+
hasThread: async (channelId, threadId) => {
|
|
28
|
+
const details = await context.storage
|
|
29
|
+
.getThreadDetails({ channelId, threadId })
|
|
30
|
+
.catch(() => null);
|
|
31
|
+
const state = details?.state;
|
|
32
|
+
return !!state && typeof state === "object" && Object.keys(state).length > 0;
|
|
33
|
+
},
|
|
26
34
|
});
|
|
27
35
|
},
|
|
28
36
|
async *run({ event, handlerCtx, context }) {
|
|
@@ -37,13 +45,5 @@ export const plugin = definePlugin({
|
|
|
37
45
|
publicBaseUrl: context.publicBaseUrl,
|
|
38
46
|
});
|
|
39
47
|
},
|
|
40
|
-
async *onEvent(event, context) {
|
|
41
|
-
if (event.type !== "agent:output")
|
|
42
|
-
return;
|
|
43
|
-
yield* handleSlackOutbound({
|
|
44
|
-
event: event,
|
|
45
|
-
credentials: slackCredentials(context),
|
|
46
|
-
});
|
|
47
|
-
},
|
|
48
48
|
});
|
|
49
49
|
export default plugin;
|
package/dist/invoke.js
CHANGED
|
@@ -92,13 +92,8 @@ async function* bridgeSystemAgentRun(args) {
|
|
|
92
92
|
}
|
|
93
93
|
export async function* handleSlackInvoke(args) {
|
|
94
94
|
const fromWebhook = isSlackWebhookIngress(args.event.meta);
|
|
95
|
-
if (
|
|
96
|
-
if (!shouldHandleInvoke(args.event, ROUTING_AGENT_ID))
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
else if (!shouldHandleInvoke(args.event, args.agentId)) {
|
|
95
|
+
if (!shouldHandleInvoke(args.event, args.agentId))
|
|
100
96
|
return;
|
|
101
|
-
}
|
|
102
97
|
if (args.event.meta?.threadId) {
|
|
103
98
|
args.ctx.state.threadId = args.event.meta.threadId;
|
|
104
99
|
}
|
|
@@ -157,28 +152,52 @@ export async function* handleSlackInvoke(args) {
|
|
|
157
152
|
// Fall back to the raw mention if Slack history is unavailable.
|
|
158
153
|
}
|
|
159
154
|
}
|
|
155
|
+
// Each reply shows in the OpenBot thread and is posted to the Slack thread it answers.
|
|
156
|
+
const reply = async (output) => {
|
|
157
|
+
if (!slackMeta?.channel || !output.data.content?.trim())
|
|
158
|
+
return;
|
|
159
|
+
try {
|
|
160
|
+
await postSlackMessage({
|
|
161
|
+
token: credentials.botToken,
|
|
162
|
+
channel: slackMeta.channel,
|
|
163
|
+
text: output.data.content,
|
|
164
|
+
threadTs: slackMeta.threadTs ?? slackMeta.ts,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
console.warn("[slack] could not post reply", {
|
|
169
|
+
channel: slackMeta.channel,
|
|
170
|
+
error: error instanceof Error ? error.message : String(error),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
};
|
|
160
174
|
try {
|
|
161
|
-
|
|
175
|
+
for await (const output of bridgeSystemAgentRun({
|
|
162
176
|
host: args.host,
|
|
163
177
|
channelId,
|
|
164
178
|
threadId,
|
|
165
179
|
userMessage: prompt,
|
|
166
180
|
meta: args.event.meta,
|
|
167
181
|
publicBaseUrl: args.publicBaseUrl,
|
|
168
|
-
})
|
|
182
|
+
})) {
|
|
183
|
+
yield output;
|
|
184
|
+
await reply(output);
|
|
185
|
+
}
|
|
169
186
|
}
|
|
170
187
|
catch (error) {
|
|
171
188
|
const message = error instanceof Error ? error.message : String(error);
|
|
172
|
-
|
|
189
|
+
const output = {
|
|
173
190
|
type: "agent:output",
|
|
174
191
|
data: { content: `Slack agent error: ${message}` },
|
|
175
192
|
meta: {
|
|
176
193
|
agentId: ROUTING_AGENT_ID,
|
|
177
194
|
threadId,
|
|
178
195
|
source: "slack",
|
|
179
|
-
slack:
|
|
196
|
+
slack: slackMeta,
|
|
180
197
|
},
|
|
181
198
|
};
|
|
199
|
+
yield output;
|
|
200
|
+
await reply(output);
|
|
182
201
|
}
|
|
183
202
|
return;
|
|
184
203
|
}
|
|
@@ -225,23 +244,3 @@ export async function* handleSlackInvoke(args) {
|
|
|
225
244
|
});
|
|
226
245
|
}
|
|
227
246
|
}
|
|
228
|
-
export async function* handleSlackOutbound(args) {
|
|
229
|
-
if (args.event.meta?.source !== "slack")
|
|
230
|
-
return;
|
|
231
|
-
if (args.event.meta?.agentId !== ROUTING_AGENT_ID)
|
|
232
|
-
return;
|
|
233
|
-
const slack = getSlackThreadMeta(args.event.meta);
|
|
234
|
-
if (!slack?.channel || !args.credentials)
|
|
235
|
-
return;
|
|
236
|
-
try {
|
|
237
|
-
await postSlackMessage({
|
|
238
|
-
token: args.credentials.botToken,
|
|
239
|
-
channel: slack.channel,
|
|
240
|
-
text: args.event.data.content,
|
|
241
|
-
threadTs: slack.threadTs ?? slack.ts,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
catch {
|
|
245
|
-
// Avoid failing the bus run if Slack delivery fails.
|
|
246
|
-
}
|
|
247
|
-
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Someone tagged in the text: a person (`<@U…>`) or a user group (`<!subteam^…>`). */
|
|
2
|
+
const MENTION = /<@[^>]+>|<!subteam\^[^>]+>/;
|
|
3
|
+
export function slackMessageKind(slackEvent) {
|
|
4
|
+
if (slackEvent.bot_id)
|
|
5
|
+
return null;
|
|
6
|
+
// "Also send to channel" replies carry a subtype but are still replies.
|
|
7
|
+
if (slackEvent.subtype && slackEvent.subtype !== "thread_broadcast")
|
|
8
|
+
return null;
|
|
9
|
+
if (slackEvent.type === "app_mention")
|
|
10
|
+
return "mention";
|
|
11
|
+
if (slackEvent.type !== "message")
|
|
12
|
+
return null;
|
|
13
|
+
if (slackEvent.channel_type === "im")
|
|
14
|
+
return "direct";
|
|
15
|
+
const inThread = !!slackEvent.thread_ts && slackEvent.thread_ts !== slackEvent.ts;
|
|
16
|
+
if (inThread && !MENTION.test(slackEvent.text ?? ""))
|
|
17
|
+
return "threadReply";
|
|
18
|
+
return null;
|
|
19
|
+
}
|
package/dist/webhook.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { decodeWebhookRawBody, getWebhookHeader, webhookHttpResponse, } from "@meetopenbot/plugin-sdk";
|
|
2
|
-
import { DEFAULT_OPENBOT_CHANNEL_ID,
|
|
2
|
+
import { DEFAULT_OPENBOT_CHANNEL_ID, SLACK_USER_DISPLAY_NAME, resolveOpenBotChannelId, resolveSlackSigningSecret, } from "./config.js";
|
|
3
|
+
import { slackMessageKind } from "./message-kind.js";
|
|
3
4
|
import { verifySlackSignature } from "./verify.js";
|
|
4
5
|
const seenSlackEvents = new Map();
|
|
5
6
|
const SLACK_EVENT_DEDUP_MS = 5 * 60 * 1000;
|
|
@@ -17,17 +18,14 @@ function markSlackEventSeen(dedupKey) {
|
|
|
17
18
|
seenSlackEvents.set(dedupKey, now);
|
|
18
19
|
return false;
|
|
19
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* One key per Slack message. A mention inside a thread arrives twice, as `app_mention`
|
|
23
|
+
* and as `message`, with different event ids.
|
|
24
|
+
*/
|
|
20
25
|
function slackEventDedupKey(payload, slackEvent) {
|
|
21
|
-
if (
|
|
22
|
-
return
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
function shouldProcessSlackEvent(slackEvent) {
|
|
26
|
-
if (slackEvent.bot_id || slackEvent.subtype)
|
|
27
|
-
return false;
|
|
28
|
-
if (slackEvent.type === "app_mention")
|
|
29
|
-
return true;
|
|
30
|
-
return slackEvent.type === "message" && slackEvent.channel_type === "im";
|
|
26
|
+
if (slackEvent.channel && slackEvent.ts)
|
|
27
|
+
return `${slackEvent.channel}:${slackEvent.ts}`;
|
|
28
|
+
return payload.event_id ?? "";
|
|
31
29
|
}
|
|
32
30
|
function normalizeSlackText(text) {
|
|
33
31
|
return text.replace(/<@[^>]+>/g, "").replace(/\s+/g, " ").trim();
|
|
@@ -68,7 +66,16 @@ export async function* handleSlackWebhook(args) {
|
|
|
68
66
|
return;
|
|
69
67
|
}
|
|
70
68
|
const slackEvent = payload.type === "event_callback" ? payload.event : undefined;
|
|
71
|
-
|
|
69
|
+
const kind = slackEvent ? slackMessageKind(slackEvent) : null;
|
|
70
|
+
if (!slackEvent || !kind) {
|
|
71
|
+
yield webhookHttpResponse({ status: 200, body: {} });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const openBotChannelId = slackEvent.channel
|
|
75
|
+
? resolveOpenBotChannelId(slackEvent.channel, args.config)
|
|
76
|
+
: DEFAULT_OPENBOT_CHANNEL_ID;
|
|
77
|
+
if (kind === "threadReply" &&
|
|
78
|
+
!(await args.hasThread(openBotChannelId, slackEvent.thread_ts))) {
|
|
72
79
|
yield webhookHttpResponse({ status: 200, body: {} });
|
|
73
80
|
return;
|
|
74
81
|
}
|
|
@@ -91,15 +98,12 @@ export async function* handleSlackWebhook(args) {
|
|
|
91
98
|
ts: slackEvent.ts,
|
|
92
99
|
threadTs: slackEvent.thread_ts,
|
|
93
100
|
};
|
|
94
|
-
const openBotChannelId = slackEvent.channel
|
|
95
|
-
? resolveOpenBotChannelId(slackEvent.channel, args.config)
|
|
96
|
-
: DEFAULT_OPENBOT_CHANNEL_ID;
|
|
97
101
|
args.ctx.state.channelId = openBotChannelId;
|
|
98
102
|
yield webhookHttpResponse({ status: 200, body: {} });
|
|
99
103
|
yield {
|
|
100
104
|
type: "agent:invoke",
|
|
101
105
|
data: {
|
|
102
|
-
agentId:
|
|
106
|
+
agentId: args.agentId,
|
|
103
107
|
role: "user",
|
|
104
108
|
content: text,
|
|
105
109
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meetopenbot/slack",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Slack Events API ingress, simple agent replies, and thread delivery for OpenBot",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "tsc && node ../../scripts/write-plugin-declaration.mjs",
|
|
30
30
|
"dev": "tsc --watch --preserveWatchOutput",
|
|
31
|
-
"typecheck": "tsc --noEmit"
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"test": "node --experimental-strip-types --test src/*.test.ts"
|
|
32
33
|
}
|
|
33
34
|
}
|