@capybara-hq/plugin-slack 0.1.0-rc.0 → 0.1.0-rc.2
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/.capybara-plugin/plugin.json +100 -2
- package/README.md +67 -10
- package/app-manifest.yaml +18 -0
- package/dist/index.mjs +283 -69
- package/package.json +1 -1
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
3
|
"name": "slack",
|
|
4
|
-
"version": "0.1.0-rc.
|
|
4
|
+
"version": "0.1.0-rc.2",
|
|
5
5
|
"publisher": "Capybara",
|
|
6
6
|
"description": "Slack (channel@1 bot over Socket Mode with Block Kit cards, identity-source@1 Sign in with Slack, directory@1)",
|
|
7
|
+
"displayName": {
|
|
8
|
+
"zh": "Slack",
|
|
9
|
+
"en": "Slack"
|
|
10
|
+
},
|
|
7
11
|
"homepage": "https://github.com/capybara-hq/plugins/tree/main/plugins/slack#readme",
|
|
8
12
|
"extension": {
|
|
9
13
|
"entry": "./dist/index.mjs",
|
|
10
14
|
"manifest": {
|
|
11
15
|
"name": "slack",
|
|
12
|
-
"version": "0.1.0-rc.
|
|
16
|
+
"version": "0.1.0-rc.2",
|
|
13
17
|
"sdk": "^0.1.0-rc.0",
|
|
14
18
|
"provides": [
|
|
15
19
|
"channel@1",
|
|
@@ -60,6 +64,100 @@
|
|
|
60
64
|
}
|
|
61
65
|
]
|
|
62
66
|
}
|
|
67
|
+
},
|
|
68
|
+
"configSchemas": {
|
|
69
|
+
"deployment": {
|
|
70
|
+
"type": "object",
|
|
71
|
+
"properties": {
|
|
72
|
+
"apiUrl": {
|
|
73
|
+
"default": "https://slack.com/api/",
|
|
74
|
+
"type": "string",
|
|
75
|
+
"format": "uri",
|
|
76
|
+
"x-ui": {
|
|
77
|
+
"label": {
|
|
78
|
+
"zh": "API 地址",
|
|
79
|
+
"en": "API URL"
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"title": "API URL"
|
|
83
|
+
},
|
|
84
|
+
"openidUrl": {
|
|
85
|
+
"default": "https://slack.com/",
|
|
86
|
+
"type": "string",
|
|
87
|
+
"format": "uri",
|
|
88
|
+
"x-ui": {
|
|
89
|
+
"label": {
|
|
90
|
+
"zh": "OpenID 地址",
|
|
91
|
+
"en": "OpenID URL"
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
"title": "OpenID URL"
|
|
95
|
+
},
|
|
96
|
+
"clientId": {
|
|
97
|
+
"type": "string",
|
|
98
|
+
"minLength": 1,
|
|
99
|
+
"x-ui": {
|
|
100
|
+
"label": {
|
|
101
|
+
"zh": "客户端 ID",
|
|
102
|
+
"en": "Client ID"
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
"title": "Client ID"
|
|
106
|
+
},
|
|
107
|
+
"clientSecret": {
|
|
108
|
+
"type": "string",
|
|
109
|
+
"minLength": 1,
|
|
110
|
+
"x-secret": true,
|
|
111
|
+
"x-ui": {
|
|
112
|
+
"label": {
|
|
113
|
+
"zh": "客户端密钥",
|
|
114
|
+
"en": "Client secret"
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
"title": "Client secret"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
"org": {
|
|
122
|
+
"type": "object",
|
|
123
|
+
"properties": {
|
|
124
|
+
"teamId": {
|
|
125
|
+
"type": "string",
|
|
126
|
+
"pattern": "^T[A-Z0-9]+$",
|
|
127
|
+
"x-ui": {
|
|
128
|
+
"label": {
|
|
129
|
+
"zh": "工作区 ID",
|
|
130
|
+
"en": "Workspace ID"
|
|
131
|
+
},
|
|
132
|
+
"help": {
|
|
133
|
+
"zh": "选填:和机器人令牌一起用于把 Slack 成员同步到组织;只用机器人收发消息时不用填。",
|
|
134
|
+
"en": "Optional: used with the bot token to sync Slack members into the org. Leave empty if you only use the bot for chat."
|
|
135
|
+
},
|
|
136
|
+
"placeholder": "T0123ABCD"
|
|
137
|
+
},
|
|
138
|
+
"title": "Workspace ID",
|
|
139
|
+
"description": "Optional: used with the bot token to sync Slack members into the org. Leave empty if you only use the bot for chat."
|
|
140
|
+
},
|
|
141
|
+
"botToken": {
|
|
142
|
+
"type": "string",
|
|
143
|
+
"pattern": "^xoxb-",
|
|
144
|
+
"x-secret": true,
|
|
145
|
+
"x-ui": {
|
|
146
|
+
"label": {
|
|
147
|
+
"zh": "机器人令牌",
|
|
148
|
+
"en": "Bot token"
|
|
149
|
+
},
|
|
150
|
+
"help": {
|
|
151
|
+
"zh": "选填,仅用于成员同步;聊天用的令牌在新建渠道连接时填。",
|
|
152
|
+
"en": "Optional, only for member sync. The tokens for chat go into the channel connection you create under Channels."
|
|
153
|
+
},
|
|
154
|
+
"placeholder": "xoxb-…"
|
|
155
|
+
},
|
|
156
|
+
"title": "Bot token",
|
|
157
|
+
"description": "Optional, only for member sync. The tokens for chat go into the channel connection you create under Channels."
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
63
161
|
}
|
|
64
162
|
}
|
|
65
163
|
}
|
package/README.md
CHANGED
|
@@ -18,11 +18,14 @@ The Slack docs cited here were consulted on 2026-10-20.
|
|
|
18
18
|
1. Open <https://api.slack.com/apps> → **Create New App** → **From a manifest**, pick the workspace, and paste this directory's
|
|
19
19
|
[`app-manifest.yaml`](./app-manifest.yaml) (format: [App manifest reference](https://docs.slack.dev/reference/app-manifest)).
|
|
20
20
|
It turns on Socket Mode and Interactivity, subscribes to the events `message.im`, `app_mention`, `message.channels`,
|
|
21
|
-
`message.groups`, `team_join` and `user_change`,
|
|
21
|
+
`message.groups`, `team_join` and `user_change`, registers the slash commands `/new`, `/env` and
|
|
22
|
+
`/capybara` (see [Commands](#commands)), and requests these bot scopes:
|
|
22
23
|
|
|
23
24
|
| scope | Purpose |
|
|
24
25
|
|---|---|
|
|
25
|
-
| `chat:write` | Send and update replies and cards |
|
|
26
|
+
| `chat:write` | Send and update replies and cards; post the visible echo of a slash command run in a channel |
|
|
27
|
+
| `commands` | Receive the slash commands `/new`, `/env`, `/capybara` |
|
|
28
|
+
| `im:write` | `conversations.open`: tell the bot's own DM from a DM between two people where a slash command was run |
|
|
26
29
|
| `im:history` | Receive DM messages |
|
|
27
30
|
| `app_mentions:read` | Receive messages that @-mention the bot in channels |
|
|
28
31
|
| `channels:history` / `groups:history` | Receive thread replies in public / private channels |
|
|
@@ -32,8 +35,10 @@ The Slack docs cited here were consulted on 2026-10-20.
|
|
|
32
35
|
2. **Basic Information → App-Level Tokens → Generate Token and Scopes**: add the scope `connections:write`
|
|
33
36
|
to get the app-level token `xapp-…` (Socket Mode uses it to call `apps.connections.open`).
|
|
34
37
|
3. **Install App → Install to Workspace** to get the bot token `xoxb-…`.
|
|
35
|
-
4. In the Capybara org settings, enable the `slack` plugin
|
|
36
|
-
|
|
38
|
+
4. In the Capybara org settings, enable the `slack` plugin (its org-level fields are optional and only for
|
|
39
|
+
[directory sync](#directory-directory1): leave them empty if you only chat with the bot), then **create a new channel
|
|
40
|
+
connection** under Channels: pick Slack as the plugin, fill in the app-level token (`appToken`, `xapp-…`) and
|
|
41
|
+
the bot token (`botToken`, `xoxb-…`), and bind an agent. Both are stored in the secret store, and the API only returns "set".
|
|
37
42
|
5. In Slack, DM the bot, or add the bot to a channel and @-mention it. The first conversation gets a binding card; click the button to confirm the binding on the web page.
|
|
38
43
|
|
|
39
44
|
Note: apps that use Socket Mode cannot currently be listed in the public Slack Marketplace, and one app can hold at most 10 concurrent
|
|
@@ -57,7 +62,10 @@ connection → Overview → Inbound mode); the connection page then shows its we
|
|
|
57
62
|
2. In the app settings, turn **Socket Mode** off, then use the webhook URL as the **Event
|
|
58
63
|
Subscriptions → Request URL** (Slack verifies it with a `url_verification` challenge, which the
|
|
59
64
|
plugin answers) and as the **Interactivity & Shortcuts → Request URL** (card buttons).
|
|
60
|
-
3.
|
|
65
|
+
3. Under **Slash Commands**, edit `/new`, `/env` and `/capybara` and set each one's **Request URL**
|
|
66
|
+
to the same webhook URL (the manifest leaves it out, since Socket Mode needs none). The plugin tells
|
|
67
|
+
a slash command from an event by its form body and answers it like Socket Mode does.
|
|
68
|
+
4. `appToken` is optional in webhook mode: only Socket Mode uses it.
|
|
61
69
|
|
|
62
70
|
Both modes parse events the same way (same message and card-click ids), so the kernel's dedupe
|
|
63
71
|
still drops a redelivery that arrives across a mode switch.
|
|
@@ -114,6 +122,51 @@ channel connection uses, but the config is stored separately and goes through th
|
|
|
114
122
|
- When `teamId` / `botToken` are not filled in (the org only uses the bot), the plugin's `configured()` returns false: the kernel does not scan or sync, and records no audit
|
|
115
123
|
or warning; a manual sync returns 409 `directory.not_configured`. It is not treated as an empty directory.
|
|
116
124
|
|
|
125
|
+
## Commands
|
|
126
|
+
|
|
127
|
+
The kernel parses commands from the message text: `/new` (DM: start a new conversation), `/env [name]`
|
|
128
|
+
(DM: list or set your default environment) and `/<skill> args` (invoke a skill). The Slack client,
|
|
129
|
+
however, treats any message starting with `/` as a slash command and refuses names no app registered
|
|
130
|
+
("/env is not a valid command"), so those messages never reach the bot. The app therefore registers
|
|
131
|
+
three slash commands, and the plugin turns each back into the text the kernel parses (it parses no
|
|
132
|
+
command itself):
|
|
133
|
+
|
|
134
|
+
| Slash command | Text the kernel gets |
|
|
135
|
+
|---|---|
|
|
136
|
+
| `/new` | `/new` |
|
|
137
|
+
| `/env [name]` | `/env [name]` |
|
|
138
|
+
| `/capybara <skill> [args]` | `/<skill> [args]` (`/capybara fix-issue 12` → `/fix-issue 12`; also `/capybara github:fix-issue 12`) |
|
|
139
|
+
|
|
140
|
+
`@capybara /fix-issue 12` in a channel still works as plain text, as before. Commands other than `/new`
|
|
141
|
+
and `/env` map like `/capybara`, so an admin may rename it (for example when another app already owns
|
|
142
|
+
`/capybara`). Slack lets several apps register `/new` or `/env`; a person who has two such apps picks
|
|
143
|
+
one in Slack's menu.
|
|
144
|
+
|
|
145
|
+
- **In your DM with the bot**: the command becomes a DM message from you, exactly as if you had typed
|
|
146
|
+
the text (binding, `/new`, `/env` and skills behave the same). Slack shows no slash command in the
|
|
147
|
+
conversation, so the ack carries an ephemeral echo of what you ran ("only visible to you"); the
|
|
148
|
+
kernel's reply follows as usual. The message id is `<DM channel>:cmd-<trigger_id>`.
|
|
149
|
+
- **In a channel**: the bot first posts a visible echo `@you: /capybara fix-issue 12` and the event is
|
|
150
|
+
that message, with `mentionsBot: true` (the same as `@capybara /fix-issue 12`): the kernel applies its
|
|
151
|
+
group rules (`/new` and `/env` are plain text in a group) and replies in the echo's thread, where you
|
|
152
|
+
can follow up by @-mentioning the bot. The bot must be in the channel; otherwise you get an
|
|
153
|
+
ephemeral "invite the app first" hint and nothing is sent to the kernel.
|
|
154
|
+
- **In a DM between two people** (where the bot cannot reply): an ephemeral hint to use your DM with
|
|
155
|
+
the app or a channel; nothing is sent. The plugin finds the bot's own DM with `conversations.open`
|
|
156
|
+
(cached in kv for a day; if the lookup fails it assumes the DM is the bot's).
|
|
157
|
+
- **`/capybara` without a skill**: an ephemeral usage line.
|
|
158
|
+
- **Ack**: Slack wants a slash command acked within 3 seconds and never redelivers one. The plugin acks
|
|
159
|
+
only after `onEvent` resolves, which is quick (the kernel dedupes and enqueues); before it, at most
|
|
160
|
+
one Web API call runs (the echo post, or the DM lookup), each with a 1.5 second timeout, in parallel
|
|
161
|
+
with the profile lookup. If `onEvent` rejects, nothing is acked and Slack shows the person a timeout
|
|
162
|
+
error, so they run the command again. A Socket Mode redelivery of the same command (same
|
|
163
|
+
`trigger_id`) reuses the cached echo, so it is posted once.
|
|
164
|
+
- The fixed hints are bilingual (English · 中文): the org's IM language is not known on inbound.
|
|
165
|
+
|
|
166
|
+
Existing apps: re-paste the manifest (**App Manifest** in the app settings) or add the three commands
|
|
167
|
+
by hand under **Slash Commands**, add the bot scopes `commands` and `im:write`, then **reinstall the
|
|
168
|
+
app** to the workspace (Slack asks for it because the scopes changed). The bot token stays the same.
|
|
169
|
+
|
|
117
170
|
## Behavior
|
|
118
171
|
|
|
119
172
|
- **DMs**: every text message goes to the kernel. **Channels**: only messages that @-mention the bot (a new message gets a thread reply under it), and replies inside threads
|
|
@@ -146,9 +199,13 @@ channel connection uses, but the config is stored separately and goes through th
|
|
|
146
199
|
|
|
147
200
|
| Field | Description |
|
|
148
201
|
|---|---|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
|
202
|
+
Shown in the form with zh / en labels and help.
|
|
203
|
+
|
|
204
|
+
| Field | Label | Description |
|
|
205
|
+
|---|---|---|
|
|
206
|
+
| `appToken` | App-level token / 应用令牌 | `xapp-…` (secret); required for Socket Mode (Basic Information → App-Level Tokens, scope `connections:write`), unused in webhook mode |
|
|
207
|
+
| `botToken` | Bot token / 机器人令牌 | `xoxb-…` (secret; Install App → Bot User OAuth Token) |
|
|
208
|
+
| `signingSecret` | Signing secret / 签名密钥 | Secret; only needed in webhook mode, leave empty for Socket Mode |
|
|
152
209
|
|
|
153
210
|
## Deployment-level config
|
|
154
211
|
|
|
@@ -165,5 +222,5 @@ The deployment-level config lives under the kernel config `extensionConfig.slack
|
|
|
165
222
|
|
|
166
223
|
| Field | Description |
|
|
167
224
|
|---|---|
|
|
168
|
-
| `teamId` |
|
|
169
|
-
| `botToken` |
|
|
225
|
+
| `teamId` | Optional: the Slack workspace id (`T…`) for syncing members into the org; leave empty if you only chat with the bot |
|
|
226
|
+
| `botToken` | Optional, only for member sync (`xoxb-…`, secret); the tokens for chat go into the channel connection |
|
package/app-manifest.yaml
CHANGED
|
@@ -15,6 +15,22 @@ features:
|
|
|
15
15
|
bot_user:
|
|
16
16
|
display_name: capybara
|
|
17
17
|
always_online: true
|
|
18
|
+
# Slack rejects a message starting with an unregistered "/name", so the kernel's commands are
|
|
19
|
+
# registered here. The plugin turns each into the text the kernel parses: /new -> "/new",
|
|
20
|
+
# /env x -> "/env x", /capybara foo bar -> "/foo bar" (skill invocation). No url: Socket Mode
|
|
21
|
+
# delivers them; in webhook mode set each command's Request URL to the webhook URL.
|
|
22
|
+
slash_commands:
|
|
23
|
+
- command: /new
|
|
24
|
+
description: Start a new conversation (direct message)
|
|
25
|
+
should_escape: false
|
|
26
|
+
- command: /env
|
|
27
|
+
description: List or set your default environment (direct message)
|
|
28
|
+
usage_hint: "[name]"
|
|
29
|
+
should_escape: false
|
|
30
|
+
- command: /capybara
|
|
31
|
+
description: Run a skill
|
|
32
|
+
usage_hint: "<skill> [args]"
|
|
33
|
+
should_escape: false
|
|
18
34
|
oauth_config:
|
|
19
35
|
# Sign in with Slack (identity-source@1): uncomment and set to <http.publicUrl>/api/v1/auth/sso/slack/callback.
|
|
20
36
|
# The sign-in flow requests openid / email / profile itself; do not add them as scopes here (Slack wants
|
|
@@ -24,6 +40,8 @@ oauth_config:
|
|
|
24
40
|
scopes:
|
|
25
41
|
bot:
|
|
26
42
|
- chat:write # post and update replies and cards
|
|
43
|
+
- commands # slash commands /new, /env, /capybara
|
|
44
|
+
- im:write # conversations.open: tell the bot's own DM from other DMs a slash command runs in
|
|
27
45
|
- im:history # message.im: direct messages to the bot
|
|
28
46
|
- app_mentions:read # app_mention: @capybara in channels
|
|
29
47
|
- channels:history # message.channels: thread replies in public channels
|
package/dist/index.mjs
CHANGED
|
@@ -7345,25 +7345,25 @@ var require_infra = __commonJS({
|
|
|
7345
7345
|
return input;
|
|
7346
7346
|
}
|
|
7347
7347
|
var nonASCIIRegex = /[^\x00-\x7F]/;
|
|
7348
|
-
function asciiLowercase(
|
|
7349
|
-
return nonASCIIRegex.test(
|
|
7348
|
+
function asciiLowercase(str6) {
|
|
7349
|
+
return nonASCIIRegex.test(str6) ? str6.replace(/[A-Z]+/g, (upper) => upper.toLowerCase()) : str6.toLowerCase();
|
|
7350
7350
|
}
|
|
7351
7351
|
function parseJSONFromBytes(bytes) {
|
|
7352
7352
|
return JSON.parse(utf8DecodeBytes(bytes));
|
|
7353
7353
|
}
|
|
7354
|
-
function removeASCIIWhitespace(
|
|
7355
|
-
return removeChars(
|
|
7354
|
+
function removeASCIIWhitespace(str6, leading = true, trailing = true) {
|
|
7355
|
+
return removeChars(str6, leading, trailing, isASCIIWhitespace);
|
|
7356
7356
|
}
|
|
7357
|
-
function removeChars(
|
|
7357
|
+
function removeChars(str6, leading, trailing, predicate) {
|
|
7358
7358
|
let lead = 0;
|
|
7359
|
-
let trail =
|
|
7359
|
+
let trail = str6.length - 1;
|
|
7360
7360
|
if (leading) {
|
|
7361
|
-
while (lead <
|
|
7361
|
+
while (lead < str6.length && predicate(str6.charCodeAt(lead))) lead++;
|
|
7362
7362
|
}
|
|
7363
7363
|
if (trailing) {
|
|
7364
|
-
while (trail >= lead && predicate(
|
|
7364
|
+
while (trail >= lead && predicate(str6.charCodeAt(trail))) trail--;
|
|
7365
7365
|
}
|
|
7366
|
-
return lead === 0 && trail ===
|
|
7366
|
+
return lead === 0 && trail === str6.length - 1 ? str6 : str6.slice(lead, trail + 1);
|
|
7367
7367
|
}
|
|
7368
7368
|
function serializeJavascriptValueToJSONString(value) {
|
|
7369
7369
|
const result = JSON.stringify(value);
|
|
@@ -7615,8 +7615,8 @@ var require_data_url = __commonJS({
|
|
|
7615
7615
|
function isHTTPWhiteSpace(char) {
|
|
7616
7616
|
return char === 13 || char === 10 || char === 9 || char === 32;
|
|
7617
7617
|
}
|
|
7618
|
-
function removeHTTPWhitespace(
|
|
7619
|
-
return removeChars(
|
|
7618
|
+
function removeHTTPWhitespace(str6, leading = true, trailing = true) {
|
|
7619
|
+
return removeChars(str6, leading, trailing, isHTTPWhiteSpace);
|
|
7620
7620
|
}
|
|
7621
7621
|
function minimizeSupportedMimeType(mimeType) {
|
|
7622
7622
|
switch (mimeType.essence) {
|
|
@@ -9664,11 +9664,11 @@ var require_request2 = __commonJS({
|
|
|
9664
9664
|
} else if (typeof val[i] === "object") {
|
|
9665
9665
|
throw new InvalidArgumentError(`invalid ${key} header`);
|
|
9666
9666
|
} else {
|
|
9667
|
-
const
|
|
9668
|
-
if (!isValidHeaderValue(
|
|
9667
|
+
const str6 = `${val[i]}`;
|
|
9668
|
+
if (!isValidHeaderValue(str6)) {
|
|
9669
9669
|
throw new InvalidArgumentError(`invalid ${key} header`);
|
|
9670
9670
|
}
|
|
9671
|
-
arr.push(
|
|
9671
|
+
arr.push(str6);
|
|
9672
9672
|
}
|
|
9673
9673
|
}
|
|
9674
9674
|
val = arr;
|
|
@@ -13877,7 +13877,7 @@ var require_body = __commonJS({
|
|
|
13877
13877
|
const boundary = getFormDataBoundary(object);
|
|
13878
13878
|
const prefix = `--${boundary}\r
|
|
13879
13879
|
Content-Disposition: form-data`;
|
|
13880
|
-
const formdataEscape = (
|
|
13880
|
+
const formdataEscape = (str6) => str6.replace(/\n/g, "%0A").replace(/\r/g, "%0D").replace(/"/g, "%22");
|
|
13881
13881
|
const normalizeLinefeeds = (value) => value.replace(/\r?\n|\r/g, "\r\n");
|
|
13882
13882
|
const blobParts = [];
|
|
13883
13883
|
const rn = new Uint8Array([13, 10]);
|
|
@@ -32651,9 +32651,9 @@ var require_cookies = __commonJS({
|
|
|
32651
32651
|
webidl.argumentLengthCheck(arguments, 2, "setCookie");
|
|
32652
32652
|
brandCheckHeaders(headers);
|
|
32653
32653
|
cookie = webidl.converters.Cookie(cookie);
|
|
32654
|
-
const
|
|
32655
|
-
if (
|
|
32656
|
-
headers.append("set-cookie",
|
|
32654
|
+
const str6 = stringify(cookie);
|
|
32655
|
+
if (str6) {
|
|
32656
|
+
headers.append("set-cookie", str6, true);
|
|
32657
32657
|
}
|
|
32658
32658
|
}
|
|
32659
32659
|
webidl.converters.DeleteCookieAttributes = webidl.dictionaryConverter([
|
|
@@ -36822,7 +36822,8 @@ var import_web_api = __toESM(require_dist4(), 1);
|
|
|
36822
36822
|
import { createHash } from "node:crypto";
|
|
36823
36823
|
import {
|
|
36824
36824
|
ChannelSendError as ChannelSendError3,
|
|
36825
|
-
secret as secret2
|
|
36825
|
+
secret as secret2,
|
|
36826
|
+
ui as ui2
|
|
36826
36827
|
} from "@capybara-hq/plugin-sdk";
|
|
36827
36828
|
import { z as z2 } from "zod";
|
|
36828
36829
|
|
|
@@ -37391,20 +37392,84 @@ function renderUpdate(message2, locale) {
|
|
|
37391
37392
|
return only;
|
|
37392
37393
|
}
|
|
37393
37394
|
|
|
37395
|
+
// src/commands.ts
|
|
37396
|
+
var str2 = (v) => typeof v === "string" && v !== "" ? v : void 0;
|
|
37397
|
+
var GENERIC_COMMAND = "/capybara";
|
|
37398
|
+
function parseSlashCommand(body) {
|
|
37399
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) return null;
|
|
37400
|
+
const b = body;
|
|
37401
|
+
const command = str2(b.command);
|
|
37402
|
+
const userId = str2(b.user_id);
|
|
37403
|
+
const channelId = str2(b.channel_id);
|
|
37404
|
+
const teamId = str2(b.team_id);
|
|
37405
|
+
const triggerId = str2(b.trigger_id);
|
|
37406
|
+
if (!command?.startsWith("/") || !userId || !channelId || !teamId || !triggerId) return null;
|
|
37407
|
+
return {
|
|
37408
|
+
command,
|
|
37409
|
+
text: typeof b.text === "string" ? b.text : "",
|
|
37410
|
+
userId,
|
|
37411
|
+
userName: str2(b.user_name),
|
|
37412
|
+
channelId,
|
|
37413
|
+
teamId,
|
|
37414
|
+
triggerId
|
|
37415
|
+
};
|
|
37416
|
+
}
|
|
37417
|
+
var unescapeSlack = (s) => s.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
37418
|
+
function slashCommandText(command, text) {
|
|
37419
|
+
const args = unescapeSlack(text).trim();
|
|
37420
|
+
const name = command.toLowerCase();
|
|
37421
|
+
if (name === "/new" || name === "/env") return args === "" ? name : `${name} ${args}`;
|
|
37422
|
+
const rest = args.replace(/^\/+/, "");
|
|
37423
|
+
return rest === "" ? null : `/${rest}`;
|
|
37424
|
+
}
|
|
37425
|
+
var escapeSlack = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
37426
|
+
var typedCommand = (c) => c.text.trim() === "" ? c.command : `${c.command} ${unescapeSlack(c.text).trim()}`;
|
|
37427
|
+
var ephemeral = (text) => ({
|
|
37428
|
+
response_type: "ephemeral",
|
|
37429
|
+
text
|
|
37430
|
+
});
|
|
37431
|
+
var SLASH_REPLIES = {
|
|
37432
|
+
usage: `Usage: ${GENERIC_COMMAND} <skill> [args] · 用法:${GENERIC_COMMAND} <技能> [参数]`,
|
|
37433
|
+
wrongDm: "Run Capybara commands in your direct message with the app, or in a channel it is in. · 请在与 Capybara 应用的私聊里,或在它所在的频道里使用命令。",
|
|
37434
|
+
notInChannel: "Invite the Capybara app to this channel first (/invite), then run the command again. · 请先把 Capybara 应用邀请进这个频道,再运行命令。",
|
|
37435
|
+
failed: "Slack could not take this command right now; try again in a moment. · Slack 暂时无法处理这条命令,请稍后再试。"
|
|
37436
|
+
};
|
|
37437
|
+
|
|
37394
37438
|
// src/config.ts
|
|
37395
|
-
import { secret } from "@capybara-hq/plugin-sdk";
|
|
37439
|
+
import { secret, ui } from "@capybara-hq/plugin-sdk";
|
|
37396
37440
|
import { z } from "zod";
|
|
37397
37441
|
var SlackDeployment = z.object({
|
|
37398
|
-
apiUrl: z.string().url().default("https://slack.com/api/"),
|
|
37399
|
-
|
|
37400
|
-
|
|
37401
|
-
|
|
37442
|
+
apiUrl: ui(z.string().url().default("https://slack.com/api/"), {
|
|
37443
|
+
label: { zh: "API 地址", en: "API URL" }
|
|
37444
|
+
}),
|
|
37445
|
+
openidUrl: ui(z.string().url().default("https://slack.com/"), {
|
|
37446
|
+
label: { zh: "OpenID 地址", en: "OpenID URL" }
|
|
37447
|
+
}),
|
|
37448
|
+
clientId: ui(z.string().min(1).optional(), { label: { zh: "客户端 ID", en: "Client ID" } }),
|
|
37449
|
+
clientSecret: ui(secret(z.string().min(1)), {
|
|
37450
|
+
label: { zh: "客户端密钥", en: "Client secret" }
|
|
37451
|
+
}).optional()
|
|
37402
37452
|
});
|
|
37403
37453
|
var SlackDirectoryConfig = z.object({
|
|
37404
|
-
teamId:
|
|
37405
|
-
|
|
37406
|
-
|
|
37407
|
-
|
|
37454
|
+
teamId: ui(
|
|
37455
|
+
z.string().regex(/^T[A-Z0-9]+$/, "must be a Slack workspace team id such as T0123ABCD").optional(),
|
|
37456
|
+
{
|
|
37457
|
+
label: { zh: "工作区 ID", en: "Workspace ID" },
|
|
37458
|
+
help: {
|
|
37459
|
+
zh: "选填:和机器人令牌一起用于把 Slack 成员同步到组织;只用机器人收发消息时不用填。",
|
|
37460
|
+
en: "Optional: used with the bot token to sync Slack members into the org. Leave empty if you only use the bot for chat."
|
|
37461
|
+
},
|
|
37462
|
+
placeholder: "T0123ABCD"
|
|
37463
|
+
}
|
|
37464
|
+
),
|
|
37465
|
+
botToken: ui(secret(z.string().regex(/^xoxb-/, "must be a bot token starting with xoxb-")), {
|
|
37466
|
+
label: { zh: "机器人令牌", en: "Bot token" },
|
|
37467
|
+
help: {
|
|
37468
|
+
zh: "选填,仅用于成员同步;聊天用的令牌在新建渠道连接时填。",
|
|
37469
|
+
en: "Optional, only for member sync. The tokens for chat go into the channel connection you create under Channels."
|
|
37470
|
+
},
|
|
37471
|
+
placeholder: "xoxb-…"
|
|
37472
|
+
}).optional()
|
|
37408
37473
|
});
|
|
37409
37474
|
var SLACK_ISSUER = "https://slack.com";
|
|
37410
37475
|
function workspaceTokenProblem(auth) {
|
|
@@ -37487,8 +37552,8 @@ function toChannelSendError(e) {
|
|
|
37487
37552
|
|
|
37488
37553
|
// src/inbound.ts
|
|
37489
37554
|
var obj2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v) ? v : void 0;
|
|
37490
|
-
var
|
|
37491
|
-
var
|
|
37555
|
+
var str3 = (v) => typeof v === "string" && v !== "" ? v : void 0;
|
|
37556
|
+
var unescapeSlack2 = (s) => s.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
37492
37557
|
function stripBotMention(text, botUserId) {
|
|
37493
37558
|
const id = botUserId.replace(/[^A-Za-z0-9]/g, "");
|
|
37494
37559
|
return text.replace(new RegExp(`[ \\t]*<@${id}(\\|[^>]*)?>[ \\t]*`, "g"), (m, _label, at, s) => {
|
|
@@ -37503,8 +37568,8 @@ var mentions = (text, botUserId) => new RegExp(`<@${botUserId.replace(/[^A-Za-z0
|
|
|
37503
37568
|
function profileOf(p) {
|
|
37504
37569
|
const o = obj2(p);
|
|
37505
37570
|
if (!o) return void 0;
|
|
37506
|
-
const displayName =
|
|
37507
|
-
const avatarUrl =
|
|
37571
|
+
const displayName = str3(o.display_name) ?? str3(o.real_name) ?? str3(o.name);
|
|
37572
|
+
const avatarUrl = str3(o.image_72) ?? str3(o.image_48) ?? str3(o.image_192);
|
|
37508
37573
|
if (displayName === void 0 && avatarUrl === void 0) return void 0;
|
|
37509
37574
|
return {
|
|
37510
37575
|
...displayName !== void 0 && { displayName },
|
|
@@ -37515,7 +37580,7 @@ function eventToInbound(body, botUserId) {
|
|
|
37515
37580
|
const b = obj2(body);
|
|
37516
37581
|
const e = obj2(b?.event);
|
|
37517
37582
|
if (!b || !e) return null;
|
|
37518
|
-
const teamId =
|
|
37583
|
+
const teamId = str3(b.team_id);
|
|
37519
37584
|
const type = e.type;
|
|
37520
37585
|
if (type === "team_join" || type === "user_change") {
|
|
37521
37586
|
if (!teamId) return null;
|
|
@@ -37527,19 +37592,19 @@ function eventToInbound(body, botUserId) {
|
|
|
37527
37592
|
}
|
|
37528
37593
|
if (e.bot_id !== void 0 || e.bot_profile !== void 0) return null;
|
|
37529
37594
|
if (type === "app_mention" && e.edited !== void 0) return null;
|
|
37530
|
-
const user =
|
|
37531
|
-
const channel =
|
|
37532
|
-
const ts =
|
|
37595
|
+
const user = str3(e.user);
|
|
37596
|
+
const channel = str3(e.channel);
|
|
37597
|
+
const ts = str3(e.ts);
|
|
37533
37598
|
const raw = typeof e.text === "string" ? e.text : "";
|
|
37534
37599
|
if (!user || !channel || !ts || user === botUserId) return null;
|
|
37535
|
-
const tenantId =
|
|
37600
|
+
const tenantId = str3(e.user_team) ?? str3(e.team) ?? teamId;
|
|
37536
37601
|
if (!tenantId) return null;
|
|
37537
37602
|
const mentioned = type === "app_mention" || mentions(raw, botUserId);
|
|
37538
|
-
const threadTs =
|
|
37603
|
+
const threadTs = str3(e.thread_ts);
|
|
37539
37604
|
const inThread = threadTs !== void 0 && threadTs !== ts;
|
|
37540
37605
|
const dm = type === "message" ? e.channel_type === "im" : channel.startsWith("D");
|
|
37541
37606
|
if (!dm && type === "message" && (mentioned || !inThread)) return null;
|
|
37542
|
-
const text =
|
|
37607
|
+
const text = unescapeSlack2(stripBotMention(raw, botUserId));
|
|
37543
37608
|
if (text === "") return null;
|
|
37544
37609
|
const target = dm ? { chatId: channel, chatType: "dm" } : {
|
|
37545
37610
|
chatId: channel,
|
|
@@ -37580,6 +37645,11 @@ function verifySlackSignature(h, body, signingSecret, nowMs) {
|
|
|
37580
37645
|
const got = Buffer.from(sig2);
|
|
37581
37646
|
return got.length === want.length && timingSafeEqual(got, want);
|
|
37582
37647
|
}
|
|
37648
|
+
var slackJsonAck = (body) => ({
|
|
37649
|
+
status: 200,
|
|
37650
|
+
headers: { "content-type": "application/json; charset=utf-8" },
|
|
37651
|
+
body: JSON.stringify(body)
|
|
37652
|
+
});
|
|
37583
37653
|
function readSlackWebhook(config, request) {
|
|
37584
37654
|
if (config.signingSecret === void 0) return { kind: "reject", status: 401 };
|
|
37585
37655
|
if (!verifySlackSignature(request.headers, request.body, config.signingSecret, request.receivedAt)) {
|
|
@@ -37588,8 +37658,12 @@ function readSlackWebhook(config, request) {
|
|
|
37588
37658
|
const text = new TextDecoder().decode(request.body);
|
|
37589
37659
|
const type = (request.headers["content-type"] ?? "").split(";")[0]?.trim().toLowerCase();
|
|
37590
37660
|
if (type === "application/x-www-form-urlencoded") {
|
|
37591
|
-
const
|
|
37592
|
-
|
|
37661
|
+
const form = new URLSearchParams(text);
|
|
37662
|
+
const payload = form.get("payload");
|
|
37663
|
+
if (payload === null) {
|
|
37664
|
+
if (form.get("command") === null) return { kind: "ignore" };
|
|
37665
|
+
return { kind: "command", body: Object.fromEntries(form) };
|
|
37666
|
+
}
|
|
37593
37667
|
let parsed2;
|
|
37594
37668
|
try {
|
|
37595
37669
|
parsed2 = JSON.parse(payload);
|
|
@@ -37627,6 +37701,11 @@ async function handleSlackWebhook(args) {
|
|
|
37627
37701
|
const r = readSlackWebhook(args.config, args.request);
|
|
37628
37702
|
if (r.kind === "respond" || r.kind === "reject") return r;
|
|
37629
37703
|
if (r.kind === "ignore") return { kind: "events", events: [], ack: SLACK_WEBHOOK_ACK };
|
|
37704
|
+
if (r.kind === "command") {
|
|
37705
|
+
const { event: event2, response } = await args.command(r.body);
|
|
37706
|
+
const ack = response !== void 0 ? slackJsonAck(response) : SLACK_WEBHOOK_ACK;
|
|
37707
|
+
return event2 ? { kind: "events", events: [event2], ack } : { kind: "respond", response: ack };
|
|
37708
|
+
}
|
|
37630
37709
|
const bot = r.envelope.type === "events_api" ? await args.botUserId() : "";
|
|
37631
37710
|
const event = await args.toEvent(r.envelope, bot);
|
|
37632
37711
|
return { kind: "events", events: event ? [event] : [], ack: SLACK_WEBHOOK_ACK };
|
|
@@ -37634,11 +37713,34 @@ async function handleSlackWebhook(args) {
|
|
|
37634
37713
|
|
|
37635
37714
|
// src/channel.ts
|
|
37636
37715
|
var SlackConnection = z2.object({
|
|
37637
|
-
appToken:
|
|
37638
|
-
|
|
37716
|
+
appToken: ui2(
|
|
37717
|
+
secret2(
|
|
37718
|
+
z2.string().regex(/^xapp-/, "must be an app-level token starting with xapp-").optional()
|
|
37719
|
+
),
|
|
37720
|
+
{
|
|
37721
|
+
label: { zh: "应用令牌", en: "App-level token" },
|
|
37722
|
+
help: {
|
|
37723
|
+
zh: "xapp-…,Socket Mode 必填(Basic Information → App-Level Tokens,权限 connections:write)。",
|
|
37724
|
+
en: "xapp-…, required for Socket Mode (Basic Information → App-Level Tokens, scope connections:write)."
|
|
37725
|
+
},
|
|
37726
|
+
placeholder: "xapp-…"
|
|
37727
|
+
}
|
|
37639
37728
|
),
|
|
37640
|
-
botToken: secret2(z2.string().regex(/^xoxb-/, "must be a bot token starting with xoxb-")),
|
|
37641
|
-
|
|
37729
|
+
botToken: ui2(secret2(z2.string().regex(/^xoxb-/, "must be a bot token starting with xoxb-")), {
|
|
37730
|
+
label: { zh: "机器人令牌", en: "Bot token" },
|
|
37731
|
+
help: {
|
|
37732
|
+
zh: "xoxb-…(Install App → Bot User OAuth Token)。",
|
|
37733
|
+
en: "xoxb-… (Install App → Bot User OAuth Token)."
|
|
37734
|
+
},
|
|
37735
|
+
placeholder: "xoxb-…"
|
|
37736
|
+
}),
|
|
37737
|
+
signingSecret: ui2(secret2(z2.string().min(1).optional()), {
|
|
37738
|
+
label: { zh: "签名密钥", en: "Signing secret" },
|
|
37739
|
+
help: {
|
|
37740
|
+
zh: "仅 webhook 模式需要;Socket Mode 留空。",
|
|
37741
|
+
en: "Only needed in webhook mode; leave empty for Socket Mode."
|
|
37742
|
+
}
|
|
37743
|
+
})
|
|
37642
37744
|
});
|
|
37643
37745
|
var SOCKET_MODE_NEEDS_APP_TOKEN = "Socket Mode needs an app-level token (xapp-…); set one or switch this connection to webhook mode";
|
|
37644
37746
|
var BOT_USER_CACHE_TTL_SECONDS = 3600;
|
|
@@ -37649,6 +37751,9 @@ var MAX_RECONNECT_DELAY_MS = 3e4;
|
|
|
37649
37751
|
var PROFILE_TTL_MS = 10 * 6e4;
|
|
37650
37752
|
var PROFILE_CACHE_MAX = 1e3;
|
|
37651
37753
|
var PROFILE_TIMEOUT_MS = 1500;
|
|
37754
|
+
var BOT_DM_CACHE_TTL_SECONDS = 86400;
|
|
37755
|
+
var COMMAND_ECHO_TTL_SECONDS = 3600;
|
|
37756
|
+
var COMMAND_CALL_TIMEOUT_MS = 1500;
|
|
37652
37757
|
var pause = (ms, signal) => new Promise((resolve) => {
|
|
37653
37758
|
const s = AbortSignal.any([signal, AbortSignal.timeout(ms)]);
|
|
37654
37759
|
if (s.aborted) resolve();
|
|
@@ -37783,6 +37888,101 @@ function createSlackChannel(opts) {
|
|
|
37783
37888
|
await rememberBotUser(connectionId, botToken, id);
|
|
37784
37889
|
return id;
|
|
37785
37890
|
};
|
|
37891
|
+
const isBotDm = async (c, connectionId, userId, channelId, signal) => {
|
|
37892
|
+
const key = `dm/${connectionId}/${userId}`;
|
|
37893
|
+
const hit = await opts.kv?.get(key).catch(() => null);
|
|
37894
|
+
if (hit) return hit === channelId;
|
|
37895
|
+
try {
|
|
37896
|
+
const s = AbortSignal.any([signal, AbortSignal.timeout(COMMAND_CALL_TIMEOUT_MS)]);
|
|
37897
|
+
const r = await web(c.botToken, s).apiCall("conversations.open", { users: userId });
|
|
37898
|
+
const id = r.channel?.id;
|
|
37899
|
+
if (!id) return true;
|
|
37900
|
+
await opts.kv?.set(key, id, { ttlSeconds: BOT_DM_CACHE_TTL_SECONDS }).catch(() => {
|
|
37901
|
+
});
|
|
37902
|
+
return id === channelId;
|
|
37903
|
+
} catch (e) {
|
|
37904
|
+
log.warn("slack: could not look up the bot's DM for a slash command; assuming it is", {
|
|
37905
|
+
connectionId,
|
|
37906
|
+
error: describe(e, "conversations.open").message
|
|
37907
|
+
});
|
|
37908
|
+
return true;
|
|
37909
|
+
}
|
|
37910
|
+
};
|
|
37911
|
+
const postEcho = async (c, connectionId, channelId, triggerId, text, signal) => {
|
|
37912
|
+
const key = `cmd-echo/${connectionId}/${createHash("sha256").update(triggerId).digest("hex").slice(0, 32)}`;
|
|
37913
|
+
const hit = await opts.kv?.get(key).catch(() => null);
|
|
37914
|
+
if (hit) return { ts: hit };
|
|
37915
|
+
try {
|
|
37916
|
+
const s = AbortSignal.any([signal, AbortSignal.timeout(COMMAND_CALL_TIMEOUT_MS)]);
|
|
37917
|
+
const r = await web(c.botToken, s).apiCall("chat.postMessage", {
|
|
37918
|
+
channel: channelId,
|
|
37919
|
+
text
|
|
37920
|
+
});
|
|
37921
|
+
if (!r.ts) return { error: "no_ts" };
|
|
37922
|
+
await opts.kv?.set(key, r.ts, { ttlSeconds: COMMAND_ECHO_TTL_SECONDS }).catch(() => {
|
|
37923
|
+
});
|
|
37924
|
+
return { ts: r.ts };
|
|
37925
|
+
} catch (e) {
|
|
37926
|
+
return { error: platformErrorCode(e) ?? "request_failed" };
|
|
37927
|
+
}
|
|
37928
|
+
};
|
|
37929
|
+
const toCommand = async (c, connectionId, body, signal) => {
|
|
37930
|
+
const cmd = parseSlashCommand(body);
|
|
37931
|
+
if (!cmd) return { event: null, response: void 0 };
|
|
37932
|
+
const text = slashCommandText(cmd.command, cmd.text);
|
|
37933
|
+
if (text === null) return { event: null, response: ephemeral(SLASH_REPLIES.usage) };
|
|
37934
|
+
const profile = lookupProfile(c.botToken, connectionId, cmd.userId, signal).then(
|
|
37935
|
+
(p2) => p2 ?? (cmd.userName !== void 0 ? { displayName: cmd.userName } : void 0)
|
|
37936
|
+
);
|
|
37937
|
+
const sender = { externalId: cmd.userId, tenantId: cmd.teamId };
|
|
37938
|
+
if (cmd.channelId.startsWith("D")) {
|
|
37939
|
+
const [own, p2] = await Promise.all([
|
|
37940
|
+
isBotDm(c, connectionId, cmd.userId, cmd.channelId, signal),
|
|
37941
|
+
profile
|
|
37942
|
+
]);
|
|
37943
|
+
if (!own) return { event: null, response: ephemeral(SLASH_REPLIES.wrongDm) };
|
|
37944
|
+
return {
|
|
37945
|
+
event: {
|
|
37946
|
+
type: "message",
|
|
37947
|
+
messageId: `${cmd.channelId}:cmd-${cmd.triggerId}`,
|
|
37948
|
+
target: { chatId: cmd.channelId, chatType: "dm" },
|
|
37949
|
+
sender: withProfile(sender, p2),
|
|
37950
|
+
text,
|
|
37951
|
+
mentionsBot: false
|
|
37952
|
+
},
|
|
37953
|
+
response: ephemeral(escapeSlack(typedCommand(cmd)))
|
|
37954
|
+
};
|
|
37955
|
+
}
|
|
37956
|
+
const echoText = `<@${cmd.userId}>: ${escapeSlack(typedCommand(cmd))}`;
|
|
37957
|
+
const [echo, p] = await Promise.all([
|
|
37958
|
+
postEcho(c, connectionId, cmd.channelId, cmd.triggerId, echoText, signal),
|
|
37959
|
+
profile
|
|
37960
|
+
]);
|
|
37961
|
+
if ("error" in echo) {
|
|
37962
|
+
const gone = ["not_in_channel", "channel_not_found", "is_archived"].includes(echo.error);
|
|
37963
|
+
if (!gone) {
|
|
37964
|
+
log.warn("slack: could not post a slash command's echo", {
|
|
37965
|
+
connectionId,
|
|
37966
|
+
error: echo.error
|
|
37967
|
+
});
|
|
37968
|
+
}
|
|
37969
|
+
return {
|
|
37970
|
+
event: null,
|
|
37971
|
+
response: ephemeral(gone ? SLASH_REPLIES.notInChannel : SLASH_REPLIES.failed)
|
|
37972
|
+
};
|
|
37973
|
+
}
|
|
37974
|
+
return {
|
|
37975
|
+
event: {
|
|
37976
|
+
type: "message",
|
|
37977
|
+
messageId: `${cmd.channelId}:${echo.ts}`,
|
|
37978
|
+
target: { chatId: cmd.channelId, chatType: "group" },
|
|
37979
|
+
sender: withProfile(sender, p),
|
|
37980
|
+
text,
|
|
37981
|
+
mentionsBot: true
|
|
37982
|
+
},
|
|
37983
|
+
response: void 0
|
|
37984
|
+
};
|
|
37985
|
+
};
|
|
37786
37986
|
return {
|
|
37787
37987
|
connectionSchema: SlackConnection,
|
|
37788
37988
|
features: { cards: true, edit: true, threads: true, minEditIntervalMs: MIN_EDIT_INTERVAL_MS },
|
|
@@ -37815,9 +38015,12 @@ function createSlackChannel(opts) {
|
|
|
37815
38015
|
await rememberBotUser(connectionId, c.botToken, botUserId);
|
|
37816
38016
|
const handle = async ({ ack, type, body }) => {
|
|
37817
38017
|
let event = null;
|
|
38018
|
+
let response;
|
|
37818
38019
|
try {
|
|
37819
38020
|
if (type === "events_api" || type === "interactive") {
|
|
37820
38021
|
event = await toEvent(c, connectionId, botUserId, { type, body }, signal);
|
|
38022
|
+
} else if (type === "slash_commands") {
|
|
38023
|
+
({ event, response } = await toCommand(c, connectionId, body, signal));
|
|
37821
38024
|
}
|
|
37822
38025
|
} catch (e) {
|
|
37823
38026
|
log.warn("slack: could not read an event; acknowledging and skipping it", {
|
|
@@ -37837,7 +38040,7 @@ function createSlackChannel(opts) {
|
|
|
37837
38040
|
return;
|
|
37838
38041
|
}
|
|
37839
38042
|
}
|
|
37840
|
-
await ack().catch((e) => {
|
|
38043
|
+
await ack(response).catch((e) => {
|
|
37841
38044
|
log.warn("slack: acknowledging an event failed; Slack will redeliver it", {
|
|
37842
38045
|
connectionId,
|
|
37843
38046
|
error: e instanceof Error ? e.message : String(e)
|
|
@@ -37925,6 +38128,17 @@ function createSlackChannel(opts) {
|
|
|
37925
38128
|
config: c,
|
|
37926
38129
|
request,
|
|
37927
38130
|
botUserId: () => botUserIdOf(connectionId, c.botToken, signal),
|
|
38131
|
+
command: async (body) => {
|
|
38132
|
+
try {
|
|
38133
|
+
return await toCommand(c, connectionId, body, signal);
|
|
38134
|
+
} catch (e) {
|
|
38135
|
+
log.warn("slack: could not read a webhook slash command; skipping it", {
|
|
38136
|
+
connectionId,
|
|
38137
|
+
error: e instanceof Error ? e.message : String(e)
|
|
38138
|
+
});
|
|
38139
|
+
return { event: null, response: ephemeral(SLASH_REPLIES.failed) };
|
|
38140
|
+
}
|
|
38141
|
+
},
|
|
37928
38142
|
toEvent: async (envelope, botUserId) => {
|
|
37929
38143
|
try {
|
|
37930
38144
|
return await toEvent(c, connectionId, botUserId, envelope, signal);
|
|
@@ -37991,7 +38205,7 @@ function createSlackChannel(opts) {
|
|
|
37991
38205
|
|
|
37992
38206
|
// src/directory.ts
|
|
37993
38207
|
var obj4 = (v) => typeof v === "object" && v !== null && !Array.isArray(v) ? v : void 0;
|
|
37994
|
-
var
|
|
38208
|
+
var str4 = (v) => typeof v === "string" && v !== "" ? v : void 0;
|
|
37995
38209
|
var pause2 = (ms, signal) => new Promise((resolve, reject) => {
|
|
37996
38210
|
if (signal.aborted) return reject(signal.reason);
|
|
37997
38211
|
const timer = AbortSignal.timeout(ms);
|
|
@@ -38043,7 +38257,7 @@ function createSlackDirectory(opts) {
|
|
|
38043
38257
|
}
|
|
38044
38258
|
const body = obj4(await res.json().catch(() => void 0));
|
|
38045
38259
|
if (body?.ok !== true) {
|
|
38046
|
-
throw new Error(`Slack ${method} failed: ${
|
|
38260
|
+
throw new Error(`Slack ${method} failed: ${str4(body?.error) ?? "malformed response"}`);
|
|
38047
38261
|
}
|
|
38048
38262
|
return body;
|
|
38049
38263
|
}
|
|
@@ -38063,7 +38277,7 @@ function createSlackDirectory(opts) {
|
|
|
38063
38277
|
const teamId = c.teamId;
|
|
38064
38278
|
const budget = { leftMs: maxWait };
|
|
38065
38279
|
const auth = await call("auth.test", c.botToken, {}, signal, budget);
|
|
38066
|
-
const tokenTeam =
|
|
38280
|
+
const tokenTeam = str4(auth.team_id);
|
|
38067
38281
|
const problem = workspaceTokenProblem(auth);
|
|
38068
38282
|
if (problem !== void 0) throw new Error(problem);
|
|
38069
38283
|
if (tokenTeam !== teamId) {
|
|
@@ -38088,23 +38302,23 @@ function createSlackDirectory(opts) {
|
|
|
38088
38302
|
const members = [];
|
|
38089
38303
|
for (const raw of page.members) {
|
|
38090
38304
|
const u = obj4(raw);
|
|
38091
|
-
const id =
|
|
38305
|
+
const id = str4(u?.id);
|
|
38092
38306
|
if (!u || !id || id === SLACKBOT || u.is_bot === true) continue;
|
|
38093
38307
|
if (u.is_restricted === true || u.is_ultra_restricted === true) continue;
|
|
38094
|
-
const ownTeam =
|
|
38308
|
+
const ownTeam = str4(u.team_id);
|
|
38095
38309
|
if (ownTeam !== void 0 && ownTeam !== teamId) continue;
|
|
38096
38310
|
const profile = obj4(u.profile);
|
|
38097
|
-
const email =
|
|
38311
|
+
const email = str4(profile?.email);
|
|
38098
38312
|
members.push({
|
|
38099
38313
|
externalId: id,
|
|
38100
38314
|
tenantId: teamId,
|
|
38101
38315
|
...email !== void 0 && { email },
|
|
38102
38316
|
emailVerified: false,
|
|
38103
|
-
displayName:
|
|
38317
|
+
displayName: str4(profile?.display_name) ?? str4(profile?.real_name) ?? str4(u.real_name) ?? str4(u.name) ?? id,
|
|
38104
38318
|
active: u.deleted !== true
|
|
38105
38319
|
});
|
|
38106
38320
|
}
|
|
38107
|
-
const next =
|
|
38321
|
+
const next = str4(obj4(page.response_metadata)?.next_cursor);
|
|
38108
38322
|
return { members, ...next !== void 0 && { next } };
|
|
38109
38323
|
}
|
|
38110
38324
|
};
|
|
@@ -38597,9 +38811,9 @@ var checkFailed = "check_failed";
|
|
|
38597
38811
|
function invalidDuration() {
|
|
38598
38812
|
throw new TypeError("Invalid time period format");
|
|
38599
38813
|
}
|
|
38600
|
-
function secs(
|
|
38601
|
-
typeof
|
|
38602
|
-
const matched = REGEX.exec(
|
|
38814
|
+
function secs(str6) {
|
|
38815
|
+
typeof str6 != "string" && invalidDuration();
|
|
38816
|
+
const matched = REGEX.exec(str6);
|
|
38603
38817
|
(!matched || matched[4] && matched[1]) && invalidDuration();
|
|
38604
38818
|
const value = parseFloat(matched[2]), numericDate2 = Math.round(value * multipliers[matched[3][0].toLowerCase()]);
|
|
38605
38819
|
return Number.isFinite(numericDate2) || invalidDuration(), matched[1] === "-" || matched[4] === "ago" ? -numericDate2 : numericDate2;
|
|
@@ -38831,7 +39045,7 @@ var SLACK_OIDC_SCOPES = "openid email profile";
|
|
|
38831
39045
|
var TEAM_CLAIM = "https://slack.com/team_id";
|
|
38832
39046
|
var USER_CLAIM = "https://slack.com/user_id";
|
|
38833
39047
|
var REQUEST_TIMEOUT_MS = 1e4;
|
|
38834
|
-
var
|
|
39048
|
+
var str5 = (v) => typeof v === "string" && v !== "" ? v : void 0;
|
|
38835
39049
|
var isHttpUrl2 = (v) => /^https?:\/\//i.test(v);
|
|
38836
39050
|
function loginConfig(config) {
|
|
38837
39051
|
const c = SlackDeployment.parse(config ?? {});
|
|
@@ -38863,7 +39077,7 @@ async function slackPost(apiUrl, method, form, signal, token) {
|
|
|
38863
39077
|
}
|
|
38864
39078
|
const body = await res.json().catch(() => void 0);
|
|
38865
39079
|
if (body?.ok !== true) {
|
|
38866
|
-
throw new Error(`Slack ${method} failed: ${
|
|
39080
|
+
throw new Error(`Slack ${method} failed: ${str5(body?.error) ?? "malformed response"}`);
|
|
38867
39081
|
}
|
|
38868
39082
|
return body;
|
|
38869
39083
|
}
|
|
@@ -38909,8 +39123,8 @@ function createSlackIdentitySource() {
|
|
|
38909
39123
|
},
|
|
38910
39124
|
signal
|
|
38911
39125
|
);
|
|
38912
|
-
const accessToken =
|
|
38913
|
-
const idToken =
|
|
39126
|
+
const accessToken = str5(token.access_token);
|
|
39127
|
+
const idToken = str5(token.id_token);
|
|
38914
39128
|
if (!accessToken || !idToken) {
|
|
38915
39129
|
throw new Error("Slack openid.connect.token failed: no access_token / id_token");
|
|
38916
39130
|
}
|
|
@@ -38928,23 +39142,23 @@ function createSlackIdentitySource() {
|
|
|
38928
39142
|
}
|
|
38929
39143
|
signal.throwIfAborted();
|
|
38930
39144
|
if (claims.nonce !== nonce) throw new Error("Slack ID token rejected: nonce mismatch");
|
|
38931
|
-
const teamId =
|
|
38932
|
-
const userId =
|
|
39145
|
+
const teamId = str5(claims[TEAM_CLAIM]);
|
|
39146
|
+
const userId = str5(claims[USER_CLAIM]) ?? str5(claims.sub);
|
|
38933
39147
|
if (!teamId || !userId) throw new Error("Slack ID token rejected: no team / user id");
|
|
38934
39148
|
const info = await slackPost(c.apiUrl, "openid.connect.userInfo", {}, signal, accessToken);
|
|
38935
|
-
const infoUser =
|
|
38936
|
-
if (infoUser !== userId ||
|
|
39149
|
+
const infoUser = str5(info[USER_CLAIM]) ?? str5(info.sub);
|
|
39150
|
+
if (infoUser !== userId || str5(info[TEAM_CLAIM]) !== teamId) {
|
|
38937
39151
|
throw new Error("Slack userInfo does not match the ID token (user / team)");
|
|
38938
39152
|
}
|
|
38939
|
-
const email =
|
|
38940
|
-
const picture =
|
|
39153
|
+
const email = str5(info.email);
|
|
39154
|
+
const picture = str5(info.picture);
|
|
38941
39155
|
return {
|
|
38942
39156
|
source: `slack:${teamId}`,
|
|
38943
39157
|
externalId: userId,
|
|
38944
39158
|
tenantId: teamId,
|
|
38945
39159
|
...email !== void 0 && { email },
|
|
38946
39160
|
emailVerified: email !== void 0 && info.email_verified === true,
|
|
38947
|
-
displayName:
|
|
39161
|
+
displayName: str5(info.name) ?? str5(info.given_name) ?? userId,
|
|
38948
39162
|
...picture !== void 0 && isHttpUrl2(picture) && { avatarUrl: picture }
|
|
38949
39163
|
};
|
|
38950
39164
|
}
|
|
@@ -38954,7 +39168,7 @@ function createSlackIdentitySource() {
|
|
|
38954
39168
|
// src/index.ts
|
|
38955
39169
|
var index_default = defineExtension({
|
|
38956
39170
|
name: "slack",
|
|
38957
|
-
version: "0.1.0-rc.
|
|
39171
|
+
version: "0.1.0-rc.2",
|
|
38958
39172
|
sdk: "^0.1.0-rc.0",
|
|
38959
39173
|
provides: [CHANNEL_EXTENSION_POINT, IDENTITY_SOURCE_EXTENSION_POINT, DIRECTORY_EXTENSION_POINT],
|
|
38960
39174
|
permissions: { network: ["slack.com", "wss-primary.slack.com"] },
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/plugin-slack",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"description": "Slack (channel@1 bot over Socket Mode with Block Kit cards, identity-source@1 Sign in with Slack, directory@1)",
|