@sedge-hq/slack 0.1.1 → 0.2.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 CHANGED
@@ -11,11 +11,14 @@ It uses Socket Mode, so nothing has to be reachable from the internet. Make an a
11
11
  2. **Event Subscriptions**: subscribe to the bot event `message.im`.
12
12
  3. **Interactivity & Shortcuts**: turn it on (needed for buttons).
13
13
  4. **App Home**: allow users to send messages from the Messages tab.
14
- 5. **OAuth & Permissions**: add the bot scopes `chat:write`, `im:history` and `files:write`;
14
+ 5. **OAuth & Permissions**: add the bot scopes `chat:write`, `im:history`, `files:write` and `files:read` (for files you send it);
15
15
  install the app, and copy the bot token (`xoxb-…`).
16
16
 
17
17
  Then in `sedge`, say "set up slack": it asks for both tokens in hidden fields, keeps them in your
18
18
  keychain, and shows you a code. Send the bot `pair <code>` in a DM: that pairs your account, and
19
19
  nobody else gets answers.
20
20
 
21
+ Files you send the bot (with `files:read`) are saved for the agent. It can also post in channels
22
+ the bot is in: the agent's `act` tool has a **`post`** action, behind your permissions.
23
+
21
24
  [Channels and signals](https://github.com/sedge-hq/sedge/blob/main/docs/channels-and-signals.md) · MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sedge-hq/slack",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Slack for sedge: message your agent from Slack, and it answers there.",
5
5
  "keywords": [
6
6
  "sedge",
@@ -30,13 +30,13 @@
30
30
  "test": "bun --bun vitest run --passWithNoTests"
31
31
  },
32
32
  "dependencies": {
33
- "@sedge-hq/kit": "0.3.2"
33
+ "@sedge-hq/kit": "0.4.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@sedge-hq/config": "0.0.0",
37
- "@types/bun": "1.4.4",
37
+ "@types/bun": "1.4.5",
38
38
  "typescript": "7.0.2",
39
- "vitest": "5.1.1"
39
+ "vitest": "5.1.2"
40
40
  },
41
41
  "sedge": {
42
42
  "plugins": [
package/src/slack.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { defineChannel, field, type Reply, secret } from '@sedge-hq/kit';
1
+ import { defineChannel, field, type ReceivedFile, type Reply, secret } from '@sedge-hq/kit';
2
2
  import { DEFAULT_API, makeSlack } from './api';
3
3
  import { toMrkdwn } from './mrkdwn';
4
4
 
@@ -41,6 +41,11 @@ interface Envelope {
41
41
  readonly ts?: string;
42
42
  readonly bot_id?: string;
43
43
  readonly subtype?: string;
44
+ readonly files?: ReadonlyArray<{
45
+ readonly name?: string;
46
+ readonly mimetype?: string;
47
+ readonly url_private_download?: string;
48
+ }>;
44
49
  };
45
50
  readonly type?: string;
46
51
  readonly user?: { readonly id?: string };
@@ -59,7 +64,7 @@ interface Envelope {
59
64
  *
60
65
  * Make an app at api.slack.com/apps: turn on Socket Mode (that gives the app-level token, with
61
66
  * `connections:write`), subscribe to the `message.im` bot event, turn on Interactivity, and add
62
- * the bot scopes `chat:write`, `im:history` and `files:write`. Install it, and give sedge both
67
+ * the bot scopes `chat:write`, `im:history`, `files:write` and `files:read` (for files you send it). Install it, and give sedge both
63
68
  * tokens.
64
69
  *
65
70
  * It only talks to people paired through an invite: they send the bot `pair <code>`, with a code
@@ -75,6 +80,25 @@ export default defineChannel({
75
80
  botToken: secret('The bot token (xoxb-…), from OAuth & Permissions.'),
76
81
  api: field.optional(field.text('The Slack API address. Default: slack.com/api.')),
77
82
  },
83
+ actions: {
84
+ post: {
85
+ description:
86
+ 'Post a message in a Slack channel the bot is in (not a DM with the user: send_message does that). Needs the chat:write scope.',
87
+ input: {
88
+ channel: field.text('The channel: its ID (C0123…) or #name.'),
89
+ text: field.text('The message, in Markdown.'),
90
+ },
91
+ run: async ({ channel, text }, { botToken, api }, kit) => {
92
+ const slack = makeSlack(api ?? DEFAULT_API, kit.signal);
93
+ await slack.call('chat.postMessage', botToken, {
94
+ channel,
95
+ unfurl_links: false,
96
+ ...blocks({ text }),
97
+ });
98
+ return `Posted in ${channel}.`;
99
+ },
100
+ },
101
+ },
78
102
  connect: async ({ appToken, botToken, api }, kit) => {
79
103
  const slack = makeSlack(api ?? DEFAULT_API, kit.signal);
80
104
  const me = await slack.call<{
@@ -111,10 +135,11 @@ export default defineChannel({
111
135
  const event = envelope.payload?.event;
112
136
  if (envelope.type === 'events_api' && event?.type === 'message') {
113
137
  // Direct messages from people only: not the bot's own, not edits, not channels.
138
+ // A message with files is one too (subtype file_share).
114
139
  if (
115
140
  event.channel_type !== 'im' ||
116
141
  event.bot_id ||
117
- event.subtype ||
142
+ (event.subtype && event.subtype !== 'file_share') ||
118
143
  !event.user ||
119
144
  !event.channel
120
145
  )
@@ -136,12 +161,46 @@ export default defineChannel({
136
161
  if (!paired.has(event.user)) return;
137
162
  home = event.channel;
138
163
  await kit.memo.set('home', home);
139
- if (!event.text) return;
164
+ // Files they shared: fetched with the bot token (it needs files:read) and saved.
165
+ const files: Array<ReceivedFile> = [];
166
+ const missed: Array<string> = [];
167
+ for (const file of event.files ?? []) {
168
+ const name = file.name ?? 'file';
169
+ try {
170
+ if (!file.url_private_download) throw new Error('no download link');
171
+ const response = await fetch(file.url_private_download, {
172
+ headers: { authorization: `Bearer ${botToken}` },
173
+ signal: AbortSignal.any([kit.signal, AbortSignal.timeout(120_000)]),
174
+ });
175
+ // Without files:read, Slack answers with its login page instead.
176
+ if (
177
+ !response.ok ||
178
+ response.headers.get('content-type')?.startsWith('text/html')
179
+ )
180
+ throw new Error('the app needs the files:read scope');
181
+ files.push({
182
+ path: await kit.save(
183
+ name,
184
+ new Uint8Array(await response.arrayBuffer()),
185
+ ),
186
+ name,
187
+ ...(file.mimetype ? { type: file.mimetype } : {}),
188
+ });
189
+ } catch (error) {
190
+ missed.push(
191
+ `${name} (${error instanceof Error ? error.message : String(error)})`,
192
+ );
193
+ }
194
+ }
195
+ if (missed.length > 0)
196
+ await post(event.channel, { text: `I couldn't get ${missed.join(', ')}.` });
197
+ if (!event.text && files.length === 0) return;
140
198
  return kit.receive({
141
199
  type: 'message',
142
200
  from: event.channel,
143
- text: event.text,
201
+ text: event.text ?? '',
144
202
  key: event.ts ?? '',
203
+ ...(files.length > 0 ? { files } : {}),
145
204
  });
146
205
  }
147
206
  const payload = envelope.payload;