@im-more/dsh-slack 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +13 -0
- package/lib/index.js +2659 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +66 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +165 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +49 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +54 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +126 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +396 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +61 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +196 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +27 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +35 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +14 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +14 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/modes/mrkdwn.d.ts +17 -0
- package/lib/types/modes/mrkdwn.d.ts.map +1 -0
- package/lib/types/modes/mrkdwn.js +123 -0
- package/lib/types/modes/mrkdwn.js.map +1 -0
- package/lib/types/modes/shared.d.ts +164 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +411 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/mrkdwn.d.ts +41 -0
- package/lib/types/mrkdwn.d.ts.map +1 -0
- package/lib/types/mrkdwn.js +137 -0
- package/lib/types/mrkdwn.js.map +1 -0
- package/lib/types/request.d.ts +225 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +330 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +74 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +200 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/transport/socket.d.ts +58 -0
- package/lib/types/transport/socket.d.ts.map +1 -0
- package/lib/types/transport/socket.js +297 -0
- package/lib/types/transport/socket.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +48 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +210 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types/admin.d.ts +682 -0
- package/lib/types/types/admin.d.ts.map +1 -0
- package/lib/types/types/admin.js +172 -0
- package/lib/types/types/admin.js.map +1 -0
- package/lib/types/types/api.d.ts +20 -0
- package/lib/types/types/api.d.ts.map +1 -0
- package/lib/types/types/api.js +7 -0
- package/lib/types/types/api.js.map +1 -0
- package/lib/types/types/apps.d.ts +138 -0
- package/lib/types/types/apps.d.ts.map +1 -0
- package/lib/types/types/apps.js +28 -0
- package/lib/types/types/apps.js.map +1 -0
- package/lib/types/types/auth.d.ts +36 -0
- package/lib/types/types/auth.d.ts.map +1 -0
- package/lib/types/types/auth.js +10 -0
- package/lib/types/types/auth.js.map +1 -0
- package/lib/types/types/bots.d.ts +32 -0
- package/lib/types/types/bots.d.ts.map +1 -0
- package/lib/types/types/bots.js +7 -0
- package/lib/types/types/bots.js.map +1 -0
- package/lib/types/types/calls.d.ts +82 -0
- package/lib/types/types/calls.d.ts.map +1 -0
- package/lib/types/types/calls.js +22 -0
- package/lib/types/types/calls.js.map +1 -0
- package/lib/types/types/chat.d.ts +208 -0
- package/lib/types/types/chat.d.ts.map +1 -0
- package/lib/types/types/chat.js +34 -0
- package/lib/types/types/chat.js.map +1 -0
- package/lib/types/types/conversations.d.ts +266 -0
- package/lib/types/types/conversations.d.ts.map +1 -0
- package/lib/types/types/conversations.js +58 -0
- package/lib/types/types/conversations.js.map +1 -0
- package/lib/types/types/definition.d.ts +505 -0
- package/lib/types/types/definition.d.ts.map +1 -0
- package/lib/types/types/definition.js +2 -0
- package/lib/types/types/definition.js.map +1 -0
- package/lib/types/types/dialog.d.ts +20 -0
- package/lib/types/types/dialog.d.ts.map +1 -0
- package/lib/types/types/dialog.js +7 -0
- package/lib/types/types/dialog.js.map +1 -0
- package/lib/types/types/dnd.d.ts +70 -0
- package/lib/types/types/dnd.d.ts.map +1 -0
- package/lib/types/types/dnd.js +19 -0
- package/lib/types/types/dnd.js.map +1 -0
- package/lib/types/types/emoji.d.ts +18 -0
- package/lib/types/types/emoji.d.ts.map +1 -0
- package/lib/types/types/emoji.js +7 -0
- package/lib/types/types/emoji.js.map +1 -0
- package/lib/types/types/events/base-events.d.ts +235 -0
- package/lib/types/types/events/base-events.d.ts.map +1 -0
- package/lib/types/types/events/base-events.js +3 -0
- package/lib/types/types/events/base-events.js.map +1 -0
- package/lib/types/types/events/index.d.ts +97 -0
- package/lib/types/types/events/index.d.ts.map +1 -0
- package/lib/types/types/events/index.js +3 -0
- package/lib/types/types/events/index.js.map +1 -0
- package/lib/types/types/events/message-events.d.ts +215 -0
- package/lib/types/types/events/message-events.d.ts.map +1 -0
- package/lib/types/types/events/message-events.js +3 -0
- package/lib/types/types/events/message-events.js.map +1 -0
- package/lib/types/types/files.d.ts +189 -0
- package/lib/types/types/files.d.ts.map +1 -0
- package/lib/types/types/files.js +43 -0
- package/lib/types/types/files.js.map +1 -0
- package/lib/types/types/index.d.ts +82 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +29 -0
- package/lib/types/types/index.js.map +1 -0
- package/lib/types/types/internal.d.ts +17 -0
- package/lib/types/types/internal.d.ts.map +1 -0
- package/lib/types/types/internal.js +60 -0
- package/lib/types/types/internal.js.map +1 -0
- package/lib/types/types/migration.d.ts +25 -0
- package/lib/types/types/migration.d.ts.map +1 -0
- package/lib/types/types/migration.js +7 -0
- package/lib/types/types/migration.js.map +1 -0
- package/lib/types/types/oauth.d.ts +50 -0
- package/lib/types/types/oauth.d.ts.map +1 -0
- package/lib/types/types/oauth.js +13 -0
- package/lib/types/types/oauth.js.map +1 -0
- package/lib/types/types/pins.d.ts +39 -0
- package/lib/types/types/pins.d.ts.map +1 -0
- package/lib/types/types/pins.js +13 -0
- package/lib/types/types/pins.js.map +1 -0
- package/lib/types/types/reactions.d.ts +75 -0
- package/lib/types/types/reactions.d.ts.map +1 -0
- package/lib/types/types/reactions.js +16 -0
- package/lib/types/types/reactions.js.map +1 -0
- package/lib/types/types/reminders.d.ts +64 -0
- package/lib/types/types/reminders.d.ts.map +1 -0
- package/lib/types/types/reminders.js +19 -0
- package/lib/types/types/reminders.js.map +1 -0
- package/lib/types/types/rtm.d.ts +30 -0
- package/lib/types/types/rtm.d.ts.map +1 -0
- package/lib/types/types/rtm.js +7 -0
- package/lib/types/types/rtm.js.map +1 -0
- package/lib/types/types/search.d.ts +24 -0
- package/lib/types/types/search.d.ts.map +1 -0
- package/lib/types/types/search.js +7 -0
- package/lib/types/types/search.js.map +1 -0
- package/lib/types/types/stars.d.ts +56 -0
- package/lib/types/types/stars.d.ts.map +1 -0
- package/lib/types/types/stars.js +13 -0
- package/lib/types/types/stars.js.map +1 -0
- package/lib/types/types/team.d.ts +98 -0
- package/lib/types/types/team.d.ts.map +1 -0
- package/lib/types/types/team.js +19 -0
- package/lib/types/types/team.js.map +1 -0
- package/lib/types/types/usergroups.d.ts +103 -0
- package/lib/types/types/usergroups.d.ts.map +1 -0
- package/lib/types/types/usergroups.js +25 -0
- package/lib/types/types/usergroups.js.map +1 -0
- package/lib/types/types/users.d.ts +169 -0
- package/lib/types/types/users.d.ts.map +1 -0
- package/lib/types/types/users.js +40 -0
- package/lib/types/types/users.js.map +1 -0
- package/lib/types/types/views.d.ts +56 -0
- package/lib/types/types/views.d.ts.map +1 -0
- package/lib/types/types/views.js +16 -0
- package/lib/types/types/views.js.map +1 -0
- package/lib/types/types/workflows.d.ts +45 -0
- package/lib/types/types/workflows.d.ts.map +1 -0
- package/lib/types/types/workflows.js +13 -0
- package/lib/types/types/workflows.js.map +1 -0
- package/package.json +65 -3
- package/src/bot.ts +273 -0
- package/src/config.ts +92 -0
- package/src/decode.ts +489 -0
- package/src/errors.ts +230 -0
- package/src/identity.ts +59 -0
- package/src/index.ts +64 -0
- package/src/modes/mrkdwn.ts +159 -0
- package/src/modes/shared.ts +515 -0
- package/src/mrkdwn.ts +146 -0
- package/src/request.ts +449 -0
- package/src/send.ts +300 -0
- package/src/transport/socket.ts +393 -0
- package/src/transport/webhook.ts +269 -0
- package/src/types/admin.ts +910 -0
- package/src/types/api.ts +28 -0
- package/src/types/apps.ts +175 -0
- package/src/types/auth.ts +49 -0
- package/src/types/bots.ts +40 -0
- package/src/types/calls.ts +110 -0
- package/src/types/chat.ts +255 -0
- package/src/types/conversations.ts +342 -0
- package/src/types/definition.ts +530 -0
- package/src/types/dialog.ts +28 -0
- package/src/types/dnd.ts +94 -0
- package/src/types/emoji.ts +26 -0
- package/src/types/events/base-events.ts +281 -0
- package/src/types/events/index.ts +125 -0
- package/src/types/events/message-events.ts +253 -0
- package/src/types/files.ts +245 -0
- package/src/types/index.ts +84 -0
- package/src/types/internal.ts +60 -0
- package/src/types/migration.ts +34 -0
- package/src/types/oauth.ts +66 -0
- package/src/types/pins.ts +55 -0
- package/src/types/reactions.ts +95 -0
- package/src/types/reminders.ts +88 -0
- package/src/types/rtm.ts +38 -0
- package/src/types/search.ts +32 -0
- package/src/types/stars.ts +72 -0
- package/src/types/team.ts +122 -0
- package/src/types/usergroups.ts +135 -0
- package/src/types/users.ts +221 -0
- package/src/types/views.ts +76 -0
- package/src/types/workflows.ts +61 -0
- package/README.md +0 -3
package/src/bot.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
2
|
+
import { Adapter } from '@deepseek-ai/cordis-plugin-im-core'
|
|
3
|
+
import type {
|
|
4
|
+
ChatRef,
|
|
5
|
+
InboundMessage,
|
|
6
|
+
MembersPage,
|
|
7
|
+
MessageRef,
|
|
8
|
+
Mode,
|
|
9
|
+
PrepareContext,
|
|
10
|
+
ResolvedResource,
|
|
11
|
+
ResourceKind,
|
|
12
|
+
ResourceRef,
|
|
13
|
+
SendContext,
|
|
14
|
+
SentPart,
|
|
15
|
+
Target,
|
|
16
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
17
|
+
import { configError, Config } from './config.ts'
|
|
18
|
+
import { classifySlackError } from './errors.ts'
|
|
19
|
+
import { verifyIdentity } from './identity.ts'
|
|
20
|
+
import { createMrkdwnMode } from './modes/mrkdwn.ts'
|
|
21
|
+
import { fileName } from './modes/shared.ts'
|
|
22
|
+
import type { SlackModeTypes } from './modes/shared.ts'
|
|
23
|
+
import {
|
|
24
|
+
callApi,
|
|
25
|
+
createSlackApi,
|
|
26
|
+
downloadFile,
|
|
27
|
+
putUploadBytes,
|
|
28
|
+
startUpload,
|
|
29
|
+
} from './request.ts'
|
|
30
|
+
import type { SlackApi } from './request.ts'
|
|
31
|
+
import {
|
|
32
|
+
createReaction,
|
|
33
|
+
deletePart,
|
|
34
|
+
deleteReaction,
|
|
35
|
+
getMessage,
|
|
36
|
+
members,
|
|
37
|
+
sendPayload,
|
|
38
|
+
} from './send.ts'
|
|
39
|
+
import type { SlackSendDeps } from './send.ts'
|
|
40
|
+
import { SlackSocketMode } from './transport/socket.ts'
|
|
41
|
+
import { SlackWebhook } from './transport/webhook.ts'
|
|
42
|
+
// The migrated declarations install every documented method on `Internal` when they are imported.
|
|
43
|
+
import './types/index.ts'
|
|
44
|
+
import { Internal } from './types/internal.ts'
|
|
45
|
+
|
|
46
|
+
/** The single mode this adapter always registers. */
|
|
47
|
+
export interface SlackModes {
|
|
48
|
+
[mode: string]: Mode<SlackModeTypes>
|
|
49
|
+
mrkdwn: Mode<SlackModeTypes>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A transport `connect` starts and `disconnect` stops. */
|
|
53
|
+
interface SlackTransport {
|
|
54
|
+
start(): Promise<void>
|
|
55
|
+
stop(): Promise<void>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The `files.info` fields this adapter reads. */
|
|
59
|
+
interface FilesInfoAnswer {
|
|
60
|
+
ok?: boolean
|
|
61
|
+
file?: { url_private?: string }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Message of any caught value, for a log line. */
|
|
65
|
+
function messageOf(error: unknown): string {
|
|
66
|
+
return error instanceof Error ? error.message : String(error)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Name of an attachment whose element declared none.
|
|
71
|
+
* @param ref - the unresolved resource.
|
|
72
|
+
* @returns the source's file name, else one naming the kind.
|
|
73
|
+
*/
|
|
74
|
+
function uploadName(ref: ResourceRef): string {
|
|
75
|
+
const named = fileName(undefined, ref.src)
|
|
76
|
+
return named === 'file' ? `slack-${ref.kind}` : named
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Slack adapter.
|
|
81
|
+
*
|
|
82
|
+
* Identity is the configured `selfId`, checked against `auth.test` on connect; Socket Mode and the Events API are the
|
|
83
|
+
* two transports; `mrkdwn` is the only mode, because Slack message text is the only way the upstream adapter ever
|
|
84
|
+
* rendered content. Inbound media is served from the bot's own `satori:` route, because `url_private` needs the bot
|
|
85
|
+
* token and the host does not have it.
|
|
86
|
+
*/
|
|
87
|
+
export class SlackBot extends Adapter<Config, SlackModes> {
|
|
88
|
+
/** The adapter needs the Satori service and the isolated `http` service. */
|
|
89
|
+
static override inject = ['satori', 'http']
|
|
90
|
+
/** The Loader applies this schema to a profile row's `config` before the constructor runs. */
|
|
91
|
+
static readonly Config = Config
|
|
92
|
+
|
|
93
|
+
readonly modes: SlackModes
|
|
94
|
+
readonly modeOrder = ['mrkdwn'] as const
|
|
95
|
+
|
|
96
|
+
/** Endpoints shared by every Web API call of this bot. */
|
|
97
|
+
readonly api: SlackApi
|
|
98
|
+
/** Full typed Slack Web API surface registered by the migrated declarations. */
|
|
99
|
+
override readonly internal: Internal
|
|
100
|
+
/** `team_id` of the installation, once `auth.test` answered. */
|
|
101
|
+
teamId: string | undefined
|
|
102
|
+
|
|
103
|
+
private transport: SlackTransport | undefined
|
|
104
|
+
|
|
105
|
+
constructor(ctx: Context, config: Config) {
|
|
106
|
+
// A profile whose protocol lacks its credential cannot work at all; refusing it here fails at load instead of at
|
|
107
|
+
// the first dial or the first callback.
|
|
108
|
+
const invalid = configError(config)
|
|
109
|
+
if (invalid !== undefined) throw invalid
|
|
110
|
+
super(ctx, config, {
|
|
111
|
+
id: `slack:${config.selfId}`,
|
|
112
|
+
platform: 'slack',
|
|
113
|
+
adapter: 'slack',
|
|
114
|
+
})
|
|
115
|
+
this.selfId = config.selfId
|
|
116
|
+
this.api = createSlackApi(ctx, config)
|
|
117
|
+
// The migrated client addresses the API root with absolute paths, so the http service it uses carries the root as
|
|
118
|
+
// its base URL (the upstream adapter injected the same base).
|
|
119
|
+
this.internal = new Internal(
|
|
120
|
+
this,
|
|
121
|
+
this.ctx.http.extend({ baseUrl: `${this.api.base}/` }),
|
|
122
|
+
)
|
|
123
|
+
this.modes = { mrkdwn: createMrkdwnMode(config.replyBroadcast) }
|
|
124
|
+
this.defineInternalRoute('/files/:fileId', async ({ params }) =>
|
|
125
|
+
this.serveFile(params.fileId),
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
override async connect(): Promise<void> {
|
|
130
|
+
try {
|
|
131
|
+
const identity = await verifyIdentity(this.api, this.config.selfId)
|
|
132
|
+
this.selfId = identity.userId
|
|
133
|
+
this.teamId = identity.teamId
|
|
134
|
+
this.user = {
|
|
135
|
+
id: identity.userId,
|
|
136
|
+
name: identity.userName === '' ? identity.userId : identity.userName,
|
|
137
|
+
isBot: true,
|
|
138
|
+
}
|
|
139
|
+
} catch (error) {
|
|
140
|
+
// `Bot.start` only stores a connect failure on `bot.error`, so log it here.
|
|
141
|
+
this.ctx.logger.error('slack: connect failed: %s', messageOf(error))
|
|
142
|
+
throw error
|
|
143
|
+
}
|
|
144
|
+
this.transport =
|
|
145
|
+
this.config.protocol === 'http'
|
|
146
|
+
? new SlackWebhook(this)
|
|
147
|
+
: new SlackSocketMode(this)
|
|
148
|
+
await this.transport.start()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
override async disconnect(): Promise<void> {
|
|
152
|
+
const transport = this.transport
|
|
153
|
+
this.transport = undefined
|
|
154
|
+
await transport?.stop()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Upload one outbound attachment through the two-step file API.
|
|
159
|
+
*
|
|
160
|
+
* `files.upload` is retired, so the bytes go to a pre-signed URL and the returned file id is what
|
|
161
|
+
* `files.completeUploadExternal` shares into the channel.
|
|
162
|
+
*/
|
|
163
|
+
override async upload(
|
|
164
|
+
ref: ResourceRef,
|
|
165
|
+
_scope: string,
|
|
166
|
+
ctx: PrepareContext,
|
|
167
|
+
): Promise<ResolvedResource> {
|
|
168
|
+
const fetched = await ctx.fetch(ref.src, ctx.signal)
|
|
169
|
+
const name = ref.meta.name ?? fetched.name ?? uploadName(ref)
|
|
170
|
+
const ticket = await startUpload(
|
|
171
|
+
this.api,
|
|
172
|
+
{ name, length: fetched.data.byteLength },
|
|
173
|
+
ctx.signal,
|
|
174
|
+
)
|
|
175
|
+
await putUploadBytes(this.api, ticket, fetched.data, ctx.signal)
|
|
176
|
+
return { key: ticket.fileId, extra: { name } }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
override resourceScope(_target: Target, _kind: ResourceKind): string {
|
|
180
|
+
return this.id
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
override async sendPayload(
|
|
184
|
+
payload: unknown,
|
|
185
|
+
target: Target,
|
|
186
|
+
ctx: SendContext,
|
|
187
|
+
): Promise<SentPart> {
|
|
188
|
+
return await sendPayload(
|
|
189
|
+
this.sendDeps(),
|
|
190
|
+
payload as Parameters<typeof sendPayload>[1],
|
|
191
|
+
target,
|
|
192
|
+
ctx,
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
override async deletePart(
|
|
197
|
+
part: SentPart,
|
|
198
|
+
handle: Parameters<typeof deletePart>[2],
|
|
199
|
+
): Promise<void> {
|
|
200
|
+
await deletePart(this.sendDeps(), part, handle)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
override async members(
|
|
204
|
+
chat: ChatRef,
|
|
205
|
+
cursor?: string,
|
|
206
|
+
limit?: number,
|
|
207
|
+
): Promise<MembersPage> {
|
|
208
|
+
return await members(this.sendDeps(), chat, cursor, limit)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
override async getMessage(
|
|
212
|
+
chat: ChatRef,
|
|
213
|
+
id: string,
|
|
214
|
+
): Promise<InboundMessage> {
|
|
215
|
+
return await getMessage(this.sendDeps(), this, chat, id)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
override async createReaction(
|
|
219
|
+
message: MessageRef,
|
|
220
|
+
emoji: string,
|
|
221
|
+
): Promise<void> {
|
|
222
|
+
await createReaction(this.sendDeps(), message, emoji)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
override async deleteReaction(
|
|
226
|
+
message: MessageRef,
|
|
227
|
+
emoji: string,
|
|
228
|
+
): Promise<void> {
|
|
229
|
+
await deleteReaction(this.sendDeps(), message, emoji)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
override classify(error: unknown) {
|
|
233
|
+
return classifySlackError(error)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private sendDeps(): SlackSendDeps {
|
|
237
|
+
return { api: this.api, teamId: this.teamId }
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Resolve one `satori:` file route.
|
|
242
|
+
*
|
|
243
|
+
* `files.info` names the download URL, which is fetched with the bot token only when its host belongs to Slack; the
|
|
244
|
+
* bytes are streamed back to the host without buffering.
|
|
245
|
+
*/
|
|
246
|
+
private async serveFile(fileId: string): Promise<Response> {
|
|
247
|
+
try {
|
|
248
|
+
const answer = await callApi<FilesInfoAnswer>(this.api, 'filesInfo', {
|
|
249
|
+
file: fileId,
|
|
250
|
+
})
|
|
251
|
+
const src = answer.file?.url_private
|
|
252
|
+
if (src === undefined || src === '') {
|
|
253
|
+
return new Response(null, { status: 404 })
|
|
254
|
+
}
|
|
255
|
+
const response = await downloadFile(this.api, src)
|
|
256
|
+
const headers = new Headers()
|
|
257
|
+
const mime = response.headers.get('content-type')
|
|
258
|
+
if (mime !== null) headers.set('content-type', mime)
|
|
259
|
+
return new Response(response.body, { status: response.status, headers })
|
|
260
|
+
} catch (error) {
|
|
261
|
+
this.ctx.logger.warn(
|
|
262
|
+
'slack: file %s could not be served: %s',
|
|
263
|
+
fileId,
|
|
264
|
+
messageOf(error),
|
|
265
|
+
)
|
|
266
|
+
return new Response(null, { status: 502 })
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export { Config }
|
|
272
|
+
|
|
273
|
+
export default SlackBot
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery'
|
|
2
|
+
|
|
3
|
+
/** How the adapter carries Slack events: Socket Mode (outbound) or the Events API (inbound HTTP). */
|
|
4
|
+
export type SlackProtocol = 'ws' | 'http'
|
|
5
|
+
|
|
6
|
+
/** One Slack workspace installation. */
|
|
7
|
+
export interface Config {
|
|
8
|
+
/** Bot user id (`U…`), checked against `auth.test` on connect. */
|
|
9
|
+
selfId: string
|
|
10
|
+
/** Bot token (`xoxb-…`); every Web API call and the file download present it. */
|
|
11
|
+
botToken: string
|
|
12
|
+
/** App-level token (`xapp-…`); only `apps.connections.open` uses it. Required by `protocol: 'ws'`. */
|
|
13
|
+
token: string
|
|
14
|
+
/** Events API signing secret. Required by `protocol: 'http'`. */
|
|
15
|
+
signing: string
|
|
16
|
+
protocol: SlackProtocol
|
|
17
|
+
/** Inbound route of `protocol: 'http'`: absolute, no trailing slash. */
|
|
18
|
+
path: string
|
|
19
|
+
/** Web API root. */
|
|
20
|
+
apiBaseUrl: string
|
|
21
|
+
/** Post a thread reply to the channel as well (`chat.postMessage`'s `reply_broadcast`). */
|
|
22
|
+
replyBroadcast: boolean
|
|
23
|
+
ws: {
|
|
24
|
+
/** Fast-retry budget of the Socket Mode reconnect loop; it never gives up. */
|
|
25
|
+
retryTimes: number
|
|
26
|
+
/** Delay between dials while the fast-retry budget lasts. */
|
|
27
|
+
retryInterval: number
|
|
28
|
+
/** Delay between dials once the fast-retry budget is spent. */
|
|
29
|
+
retryLazy: number
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Validated Slack adapter configuration. */
|
|
34
|
+
export const Config: z<Config> = z.object({
|
|
35
|
+
selfId: z.string().required(),
|
|
36
|
+
botToken: z.string().role('secret').required(),
|
|
37
|
+
token: z.string().role('secret').default(''),
|
|
38
|
+
signing: z.string().role('secret').default(''),
|
|
39
|
+
protocol: z.union(['ws', 'http']).required(),
|
|
40
|
+
path: z.string().default('/slack'),
|
|
41
|
+
apiBaseUrl: z.string().role('url').default('https://slack.com/api'),
|
|
42
|
+
replyBroadcast: z.boolean().default(false),
|
|
43
|
+
ws: z
|
|
44
|
+
.object({
|
|
45
|
+
retryTimes: z.natural().default(6),
|
|
46
|
+
retryInterval: z.natural().role('ms').default(5_000),
|
|
47
|
+
retryLazy: z.natural().role('ms').default(60_000),
|
|
48
|
+
})
|
|
49
|
+
.default({}),
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The configuration the selected protocol cannot work from.
|
|
54
|
+
*
|
|
55
|
+
* The two transports need different credentials: Socket Mode dials with the app-level token, the Events API verifies
|
|
56
|
+
* with the signing secret. A profile that leaves the needed one out is refused when the plugin loads, not when the
|
|
57
|
+
* first dial or the first callback fails.
|
|
58
|
+
*
|
|
59
|
+
* @param config - the validated configuration.
|
|
60
|
+
* @returns the configuration error, or undefined when the selection is complete.
|
|
61
|
+
*/
|
|
62
|
+
export function configError(config: Config): Error | undefined {
|
|
63
|
+
if (config.selfId === '') {
|
|
64
|
+
return new Error(
|
|
65
|
+
'slack: selfId is required (the bot user id "U…"; read it from auth.test)',
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
if (config.botToken === '') {
|
|
69
|
+
return new Error('slack: botToken is required (the "xoxb-…" bot token)')
|
|
70
|
+
}
|
|
71
|
+
if (config.protocol === 'ws' && config.token === '') {
|
|
72
|
+
return new Error(
|
|
73
|
+
'slack: protocol "ws" needs token (the "xapp-…" app-level token for apps.connections.open)',
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
if (config.protocol === 'http' && config.signing === '') {
|
|
77
|
+
return new Error(
|
|
78
|
+
'slack: protocol "http" needs signing (the Events API signing secret)',
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
return undefined
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Normalize an inbound route path: absolute, no trailing slash.
|
|
86
|
+
* @param path - the configured `path`.
|
|
87
|
+
* @returns the pathname to register.
|
|
88
|
+
*/
|
|
89
|
+
export function routePath(path: string): string {
|
|
90
|
+
const withSlash = path.startsWith('/') ? path : `/${path}`
|
|
91
|
+
return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash
|
|
92
|
+
}
|