@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/errors.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack failures and their mapping onto the core platform taxonomy.
|
|
3
|
+
*
|
|
4
|
+
* A Slack Web API answer reports failure in two unrelated ways: an HTTP status (429 with `Retry-After`, 5xx, a
|
|
5
|
+
* transport failure that produced no status at all) and a JSON envelope that says `ok: false` with a symbolic `error`.
|
|
6
|
+
* Only the second is a certain refusal; the first can leave the outcome unknown, so the two are classified separately.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
10
|
+
|
|
11
|
+
/** Fields a failed Slack call carries for classification. */
|
|
12
|
+
export interface SlackApiErrorInit {
|
|
13
|
+
/** Slack method whose call failed (`chat.postMessage`). */
|
|
14
|
+
method: string
|
|
15
|
+
/** HTTP status of the answer; `0` when the request produced none. */
|
|
16
|
+
status?: number
|
|
17
|
+
/** Slack's `error` string of an `ok: false` answer. */
|
|
18
|
+
code?: string
|
|
19
|
+
/** `Retry-After` of a 429 answer, in milliseconds. */
|
|
20
|
+
retryAfterMs?: number
|
|
21
|
+
cause?: unknown
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** One failed Slack Web API call. */
|
|
25
|
+
export class SlackApiError extends Error {
|
|
26
|
+
readonly method: string
|
|
27
|
+
readonly status: number
|
|
28
|
+
readonly code: string | undefined
|
|
29
|
+
readonly retryAfterMs: number | undefined
|
|
30
|
+
|
|
31
|
+
constructor(message: string, init: SlackApiErrorInit) {
|
|
32
|
+
super(message, init.cause === undefined ? undefined : { cause: init.cause })
|
|
33
|
+
this.name = 'SlackApiError'
|
|
34
|
+
this.method = init.method
|
|
35
|
+
this.status = init.status ?? 0
|
|
36
|
+
this.code = init.code
|
|
37
|
+
this.retryAfterMs = init.retryAfterMs
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Whether a caught value is the abort error of a cancelled request. */
|
|
42
|
+
function isAbortError(error: unknown): boolean {
|
|
43
|
+
return (
|
|
44
|
+
(error instanceof Error || error instanceof DOMException) &&
|
|
45
|
+
error.name === 'AbortError'
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A refusal that only an account or app change can fix.
|
|
51
|
+
*
|
|
52
|
+
* The socket transport stops its reconnect loop on one of these instead of dialing again, because the same credential
|
|
53
|
+
* would be refused again.
|
|
54
|
+
*/
|
|
55
|
+
export class SlackFatalError extends Error {
|
|
56
|
+
constructor(message: string, options: { cause?: unknown } = {}) {
|
|
57
|
+
super(
|
|
58
|
+
message,
|
|
59
|
+
options.cause === undefined ? undefined : { cause: options.cause },
|
|
60
|
+
)
|
|
61
|
+
this.name = 'SlackFatalError'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** How one Slack `error` code is classified. */
|
|
66
|
+
type Verdict = Pick<PlatformError, 'kind' | 'published'>
|
|
67
|
+
|
|
68
|
+
const RATE_LIMIT = new Set(['ratelimited', 'rate_limited'])
|
|
69
|
+
|
|
70
|
+
const RETRYABLE_UNKNOWN = new Set([
|
|
71
|
+
'fatal_error',
|
|
72
|
+
'internal_error',
|
|
73
|
+
'service_unavailable',
|
|
74
|
+
'request_timeout',
|
|
75
|
+
])
|
|
76
|
+
|
|
77
|
+
const PERMISSION = new Set([
|
|
78
|
+
'not_authed',
|
|
79
|
+
'invalid_auth',
|
|
80
|
+
'token_expired',
|
|
81
|
+
'token_revoked',
|
|
82
|
+
'account_inactive',
|
|
83
|
+
'not_allowed_token_type',
|
|
84
|
+
'missing_scope',
|
|
85
|
+
'no_permission',
|
|
86
|
+
'not_in_channel',
|
|
87
|
+
'ekm_access_denied',
|
|
88
|
+
'team_access_not_granted',
|
|
89
|
+
'org_login_required',
|
|
90
|
+
'enterprise_is_restricted',
|
|
91
|
+
'two_factor_setup_required',
|
|
92
|
+
'messages_tab_disabled',
|
|
93
|
+
'posting_to_channel_denied',
|
|
94
|
+
])
|
|
95
|
+
|
|
96
|
+
const NOT_FOUND = new Set([
|
|
97
|
+
'channel_not_found',
|
|
98
|
+
'invalid_channel_id',
|
|
99
|
+
'message_not_found',
|
|
100
|
+
'team_not_found',
|
|
101
|
+
'file_not_found',
|
|
102
|
+
'file_is_deleted',
|
|
103
|
+
'file_deleted',
|
|
104
|
+
])
|
|
105
|
+
|
|
106
|
+
const LIMIT = new Set([
|
|
107
|
+
'no_text',
|
|
108
|
+
'msg_too_long',
|
|
109
|
+
'msg_blocks_too_long',
|
|
110
|
+
'too_many_attachments',
|
|
111
|
+
'too_many_contact_cards',
|
|
112
|
+
'attachment_payload_limit_exceeded',
|
|
113
|
+
'message_limit_exceeded',
|
|
114
|
+
])
|
|
115
|
+
|
|
116
|
+
const INVALID = new Set([
|
|
117
|
+
'invalid_arguments',
|
|
118
|
+
'invalid_arg_name',
|
|
119
|
+
'invalid_array_arg',
|
|
120
|
+
'invalid_blocks',
|
|
121
|
+
'invalid_blocks_format',
|
|
122
|
+
'invalid_attachments',
|
|
123
|
+
'invalid_form_data',
|
|
124
|
+
'invalid_post_type',
|
|
125
|
+
'invalid_charset',
|
|
126
|
+
'invalid_name',
|
|
127
|
+
'missing_argument',
|
|
128
|
+
'missing_post_type',
|
|
129
|
+
'cant_broadcast_message',
|
|
130
|
+
'is_inactive',
|
|
131
|
+
'is_archived',
|
|
132
|
+
'duplicate_channel_not_found',
|
|
133
|
+
'duplicate_message_not_found',
|
|
134
|
+
'deprecated_endpoint',
|
|
135
|
+
'method_deprecated',
|
|
136
|
+
'restricted_action_thread_only_channel',
|
|
137
|
+
'restricted_action_read_only_channel',
|
|
138
|
+
'restricted_action_non_threadable_channel',
|
|
139
|
+
'restricted_action_thread_locked',
|
|
140
|
+
])
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Classify one Slack `error` code.
|
|
144
|
+
*
|
|
145
|
+
* A code the platform documented as "the operation may have partly succeeded" leaves the outcome unknown; every other
|
|
146
|
+
* listed code was refused before anything was written. An unlisted code is not guessed: it is retryable with an
|
|
147
|
+
* unknown outcome, because turning it into a safe retry would duplicate a message the platform may have accepted.
|
|
148
|
+
*
|
|
149
|
+
* @param code - Slack's `error` string.
|
|
150
|
+
* @returns the platform error kind and whether anything was published.
|
|
151
|
+
*/
|
|
152
|
+
export function classifyCode(code: string): Verdict {
|
|
153
|
+
if (RATE_LIMIT.has(code)) return { kind: 'rate-limit', published: 'no' }
|
|
154
|
+
if (RETRYABLE_UNKNOWN.has(code))
|
|
155
|
+
return { kind: 'retryable', published: 'unknown' }
|
|
156
|
+
if (PERMISSION.has(code)) return { kind: 'permission', published: 'no' }
|
|
157
|
+
if (NOT_FOUND.has(code)) return { kind: 'not-found', published: 'no' }
|
|
158
|
+
if (LIMIT.has(code)) return { kind: 'limit', published: 'no' }
|
|
159
|
+
if (INVALID.has(code)) return { kind: 'invalid', published: 'no' }
|
|
160
|
+
// `restricted_action…` is Slack's family of policy refusals; the four thread variants above are argument refusals.
|
|
161
|
+
if (code.startsWith('restricted_action')) {
|
|
162
|
+
return { kind: 'permission', published: 'no' }
|
|
163
|
+
}
|
|
164
|
+
return { kind: 'retryable', published: 'unknown' }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Map any Slack failure onto the core platform taxonomy.
|
|
169
|
+
*
|
|
170
|
+
* @param error - the caught failure.
|
|
171
|
+
* @returns the classified platform error.
|
|
172
|
+
*/
|
|
173
|
+
export function classifySlackError(error: unknown): PlatformError {
|
|
174
|
+
if (error instanceof PlatformError) return error
|
|
175
|
+
|
|
176
|
+
if (isAbortError(error)) {
|
|
177
|
+
return new PlatformError(
|
|
178
|
+
'fatal',
|
|
179
|
+
error instanceof Error ? error.message : 'slack request aborted',
|
|
180
|
+
{ published: 'unknown', cause: error },
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (error instanceof SlackApiError) {
|
|
185
|
+
const init = {
|
|
186
|
+
...(error.code === undefined ? {} : { code: error.code }),
|
|
187
|
+
...(error.retryAfterMs === undefined
|
|
188
|
+
? {}
|
|
189
|
+
: { retryAfterMs: error.retryAfterMs }),
|
|
190
|
+
cause: error,
|
|
191
|
+
}
|
|
192
|
+
// A 429 is the rate limiter refusing the call, whatever the envelope below it says.
|
|
193
|
+
if (error.status === 429) {
|
|
194
|
+
return new PlatformError('rate-limit', error.message, {
|
|
195
|
+
...init,
|
|
196
|
+
published: 'no',
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
if (error.code !== undefined) {
|
|
200
|
+
const verdict = classifyCode(error.code)
|
|
201
|
+
return new PlatformError(verdict.kind, error.message, {
|
|
202
|
+
...init,
|
|
203
|
+
published: verdict.published,
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
if (error.status >= 500) {
|
|
207
|
+
return new PlatformError('retryable', error.message, {
|
|
208
|
+
...init,
|
|
209
|
+
published: 'unknown',
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
if (error.status >= 400) {
|
|
213
|
+
return new PlatformError('invalid', error.message, {
|
|
214
|
+
...init,
|
|
215
|
+
published: 'no',
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
// No status (the request never completed) or a 2xx without an envelope: the platform may have acted.
|
|
219
|
+
return new PlatformError('retryable', error.message, {
|
|
220
|
+
...init,
|
|
221
|
+
published: 'unknown',
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
226
|
+
return new PlatformError('retryable', `slack request failed: ${message}`, {
|
|
227
|
+
published: 'unknown',
|
|
228
|
+
cause: error,
|
|
229
|
+
})
|
|
230
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { SlackApi } from './request.ts'
|
|
2
|
+
import { callApi } from './request.ts'
|
|
3
|
+
|
|
4
|
+
/** The configured `selfId` and the account the bot token belongs to disagree. */
|
|
5
|
+
export class SlackIdentityError extends Error {
|
|
6
|
+
constructor(message: string) {
|
|
7
|
+
super(message)
|
|
8
|
+
this.name = 'SlackIdentityError'
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** What `auth.test` reports about the installed app. */
|
|
13
|
+
export interface SlackIdentity {
|
|
14
|
+
userId: string
|
|
15
|
+
userName: string
|
|
16
|
+
teamId: string
|
|
17
|
+
teamName: string
|
|
18
|
+
botId?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Compare `auth.test.user_id` with the configured account.
|
|
23
|
+
*
|
|
24
|
+
* The bot user id of a Slack app is not part of its token, so it has to be configured; a mismatch is a configuration
|
|
25
|
+
* error that reconnecting cannot fix, and the id the host keys everything on (`ctx.bots`, `sources.bot`, the intake
|
|
26
|
+
* dedupe key) would be wrong for every message.
|
|
27
|
+
*
|
|
28
|
+
* @param api - the request dependencies.
|
|
29
|
+
* @param selfId - the configured bot user id.
|
|
30
|
+
* @returns the identity of the matching account.
|
|
31
|
+
* @throws SlackIdentityError when the token belongs to another account.
|
|
32
|
+
*/
|
|
33
|
+
export async function verifyIdentity(
|
|
34
|
+
api: SlackApi,
|
|
35
|
+
selfId: string,
|
|
36
|
+
): Promise<SlackIdentity> {
|
|
37
|
+
const answer = await callApi<
|
|
38
|
+
{ ok?: boolean } & {
|
|
39
|
+
user_id?: string
|
|
40
|
+
user?: string
|
|
41
|
+
team_id?: string
|
|
42
|
+
team?: string
|
|
43
|
+
bot_id?: string
|
|
44
|
+
}
|
|
45
|
+
>(api, 'authTest')
|
|
46
|
+
const userId = answer.user_id ?? ''
|
|
47
|
+
if (userId !== selfId) {
|
|
48
|
+
throw new SlackIdentityError(
|
|
49
|
+
`slack: selfId is configured as ${selfId} but the bot token belongs to ${userId === '' ? 'an unknown user' : userId}; fix selfId and restart`,
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
userId,
|
|
54
|
+
userName: answer.user ?? '',
|
|
55
|
+
teamId: answer.team_id ?? '',
|
|
56
|
+
teamName: answer.team ?? '',
|
|
57
|
+
...(answer.bot_id === undefined ? {} : { botId: answer.bot_id }),
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export {
|
|
2
|
+
Config,
|
|
3
|
+
configError,
|
|
4
|
+
routePath,
|
|
5
|
+
type Config as SlackConfig,
|
|
6
|
+
type SlackProtocol,
|
|
7
|
+
} from './config.ts'
|
|
8
|
+
export { SlackBot, SlackBot as default, type SlackModes } from './bot.ts'
|
|
9
|
+
export {
|
|
10
|
+
classifyCode,
|
|
11
|
+
classifySlackError,
|
|
12
|
+
SlackApiError,
|
|
13
|
+
SlackFatalError,
|
|
14
|
+
type SlackApiErrorInit,
|
|
15
|
+
} from './errors.ts'
|
|
16
|
+
export {
|
|
17
|
+
SlackIdentityError,
|
|
18
|
+
verifyIdentity,
|
|
19
|
+
type SlackIdentity,
|
|
20
|
+
} from './identity.ts'
|
|
21
|
+
export { escape, parseMrkdwn, unescape } from './mrkdwn.ts'
|
|
22
|
+
export {
|
|
23
|
+
decodeEvent,
|
|
24
|
+
decodeMessage,
|
|
25
|
+
type SlackEnvelopeLike,
|
|
26
|
+
type SlackFile,
|
|
27
|
+
type SlackMessageLike,
|
|
28
|
+
type SlackReferrer,
|
|
29
|
+
} from './decode.ts'
|
|
30
|
+
export {
|
|
31
|
+
callApi,
|
|
32
|
+
createSlackApi,
|
|
33
|
+
downloadFile,
|
|
34
|
+
ensureOk,
|
|
35
|
+
finishUpload,
|
|
36
|
+
isSlackFileUrl,
|
|
37
|
+
putUploadBytes,
|
|
38
|
+
retryAfterOf,
|
|
39
|
+
startUpload,
|
|
40
|
+
type SlackApi,
|
|
41
|
+
type SlackEnvelope,
|
|
42
|
+
type SlackMethod,
|
|
43
|
+
type SlackParams,
|
|
44
|
+
type SlackUploadedFile,
|
|
45
|
+
type SlackUploadTicket,
|
|
46
|
+
} from './request.ts'
|
|
47
|
+
export {
|
|
48
|
+
SLACK_LIMITS,
|
|
49
|
+
type SlackDraft,
|
|
50
|
+
type SlackMark,
|
|
51
|
+
type SlackModeTypes,
|
|
52
|
+
type SlackNode,
|
|
53
|
+
type SlackPayload,
|
|
54
|
+
type SlackRun,
|
|
55
|
+
} from './modes/shared.ts'
|
|
56
|
+
export { createMrkdwnMode } from './modes/mrkdwn.ts'
|
|
57
|
+
export { SlackSocketMode } from './transport/socket.ts'
|
|
58
|
+
export {
|
|
59
|
+
SlackWebhook,
|
|
60
|
+
matchesSignature,
|
|
61
|
+
signatureOf,
|
|
62
|
+
} from './transport/webhook.ts'
|
|
63
|
+
export { Internal } from './types/internal.ts'
|
|
64
|
+
export * as Slack from './types/index.ts'
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single `mrkdwn` mode: Slack message text with its mrkdwn markup.
|
|
3
|
+
*
|
|
4
|
+
* Native tags are the ones the upstream encoder writes (`text`, `p`, `br`, `b`, `i`, `s`, `code`, `a`, `at`, `channel`,
|
|
5
|
+
* `emoji`) plus the block structure Slack renders as text (headings, lists, quotes, code fences, a divider) and the
|
|
6
|
+
* attachments that travel as their own message. Everything else takes the core fallback, which keeps its text — the
|
|
7
|
+
* platform has no way to render it, so there is no second mode to offer.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { defineMode, ElementRegistry } from '@deepseek-ai/cordis-plugin-im-core'
|
|
11
|
+
import type { Mode } from '@deepseek-ai/cordis-plugin-im-core'
|
|
12
|
+
import { escape } from '../mrkdwn.ts'
|
|
13
|
+
import {
|
|
14
|
+
assembleDraft,
|
|
15
|
+
BREAK,
|
|
16
|
+
mediaNode,
|
|
17
|
+
serializeDraft,
|
|
18
|
+
SLACK_LIMITS,
|
|
19
|
+
textNode,
|
|
20
|
+
withoutLeadingBreaks,
|
|
21
|
+
withMark,
|
|
22
|
+
} from './shared.ts'
|
|
23
|
+
import type { SlackMark, SlackModeTypes, SlackNode } from './shared.ts'
|
|
24
|
+
|
|
25
|
+
const BOLD: SlackMark = { kind: 'bold' }
|
|
26
|
+
const ITALIC: SlackMark = { kind: 'italic' }
|
|
27
|
+
const STRIKE: SlackMark = { kind: 'strike' }
|
|
28
|
+
const CODE: SlackMark = { kind: 'code' }
|
|
29
|
+
|
|
30
|
+
/** A heading: Slack has no heading syntax, so a bold line stands in. */
|
|
31
|
+
function heading(nodes: readonly SlackNode[]): SlackNode[] {
|
|
32
|
+
return [BREAK, ...withMark(nodes, BOLD), BREAK]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A code block: a fenced block with the language on the opening fence line. */
|
|
36
|
+
function fenced(
|
|
37
|
+
nodes: readonly SlackNode[],
|
|
38
|
+
lang: string | undefined,
|
|
39
|
+
): SlackNode[] {
|
|
40
|
+
return withMark(
|
|
41
|
+
[BREAK, ...nodes, BREAK],
|
|
42
|
+
lang === undefined
|
|
43
|
+
? { kind: 'fence', lang: undefined }
|
|
44
|
+
: { kind: 'fence', lang },
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Prefix every line of a subtree with the quote marker. */
|
|
49
|
+
function quoteLines(nodes: readonly SlackNode[]): SlackNode[] {
|
|
50
|
+
const quoted: SlackNode[] = []
|
|
51
|
+
let atLineStart = true
|
|
52
|
+
for (const node of nodes) {
|
|
53
|
+
if (node.type !== 'text') {
|
|
54
|
+
quoted.push(node)
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
let text = ''
|
|
58
|
+
for (const char of node.run.text) {
|
|
59
|
+
if (atLineStart && char !== '\n') text += '> '
|
|
60
|
+
text += char
|
|
61
|
+
atLineStart = char === '\n'
|
|
62
|
+
}
|
|
63
|
+
quoted.push({ type: 'text', run: { text, marks: node.run.marks } })
|
|
64
|
+
}
|
|
65
|
+
return quoted
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Build the `mrkdwn` mode.
|
|
70
|
+
* @param replyBroadcast - `replyBroadcast` of the profile row.
|
|
71
|
+
* @returns the only mode of this adapter.
|
|
72
|
+
*/
|
|
73
|
+
export function createMrkdwnMode(
|
|
74
|
+
replyBroadcast: boolean,
|
|
75
|
+
): Mode<SlackModeTypes> {
|
|
76
|
+
const elements = new ElementRegistry<SlackModeTypes>()
|
|
77
|
+
.register('text', (el) => textNode(escape(el.attrs.content)))
|
|
78
|
+
.register('p', (el, ctx) => [BREAK, ...ctx.render(el.children), BREAK])
|
|
79
|
+
.register('br', () => textNode('\n'))
|
|
80
|
+
.register('b', (el, ctx) => withMark(ctx.render(el.children), BOLD))
|
|
81
|
+
.register('i', (el, ctx) => withMark(ctx.render(el.children), ITALIC))
|
|
82
|
+
.register('s', (el, ctx) => withMark(ctx.render(el.children), STRIKE))
|
|
83
|
+
.register('code', (el, ctx) => withMark(ctx.render(el.children), CODE))
|
|
84
|
+
.register('a', (el, ctx) => {
|
|
85
|
+
const children = ctx.render(el.children)
|
|
86
|
+
const attachments = children.filter((node) => node.type === 'media')
|
|
87
|
+
if (el.attrs.href === '') return children
|
|
88
|
+
// A Slack link carries plain text only, so the label is the subtree's text and the attachments follow the link.
|
|
89
|
+
const label = escape(ctx.text(el.children))
|
|
90
|
+
return [
|
|
91
|
+
textNode(
|
|
92
|
+
label === '' ? `<${el.attrs.href}>` : `<${el.attrs.href}|${label}>`,
|
|
93
|
+
),
|
|
94
|
+
...attachments,
|
|
95
|
+
]
|
|
96
|
+
})
|
|
97
|
+
.register(
|
|
98
|
+
'at',
|
|
99
|
+
(el) => {
|
|
100
|
+
switch (el.attrs.type) {
|
|
101
|
+
case 'all':
|
|
102
|
+
return textNode('<!everyone>')
|
|
103
|
+
case 'here':
|
|
104
|
+
return textNode('<!here>')
|
|
105
|
+
default:
|
|
106
|
+
return el.attrs.id === undefined
|
|
107
|
+
? textNode(`@${el.attrs.name ?? ''}`)
|
|
108
|
+
: textNode(`<@${el.attrs.id}>`)
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
{ variants: ['user', 'all', 'here'] },
|
|
112
|
+
)
|
|
113
|
+
.register('channel', (el) => textNode(`<#${el.attrs.id}>`))
|
|
114
|
+
.register('emoji', (el) =>
|
|
115
|
+
el.attrs.id === ''
|
|
116
|
+
? textNode(el.attrs.name ?? '')
|
|
117
|
+
: textNode(`:${el.attrs.id}:`),
|
|
118
|
+
)
|
|
119
|
+
.register('h1', (el, ctx) => heading(ctx.render(el.children)))
|
|
120
|
+
.register('h2', (el, ctx) => heading(ctx.render(el.children)))
|
|
121
|
+
.register('h3', (el, ctx) => heading(ctx.render(el.children)))
|
|
122
|
+
.register('h4', (el, ctx) => heading(ctx.render(el.children)))
|
|
123
|
+
.register('h5', (el, ctx) => heading(ctx.render(el.children)))
|
|
124
|
+
.register('h6', (el, ctx) => heading(ctx.render(el.children)))
|
|
125
|
+
.register('ul', (el, ctx) => [BREAK, ...ctx.render(el.children), BREAK])
|
|
126
|
+
.register('ol', (el, ctx) => [BREAK, ...ctx.render(el.children), BREAK])
|
|
127
|
+
.register('li', (el, ctx) => {
|
|
128
|
+
const parent = ctx.ancestors[ctx.ancestors.length - 1]
|
|
129
|
+
const ordered = parent?.type === 'ol'
|
|
130
|
+
const siblings = parent === undefined ? [] : parent.children
|
|
131
|
+
const index = Math.max(siblings.indexOf(el), 0)
|
|
132
|
+
const start =
|
|
133
|
+
ordered && parent.attrs.start !== undefined ? parent.attrs.start : 1
|
|
134
|
+
return [
|
|
135
|
+
textNode(ordered ? `${start + index}. ` : '• '),
|
|
136
|
+
...withoutLeadingBreaks(ctx.render(el.children)),
|
|
137
|
+
BREAK,
|
|
138
|
+
]
|
|
139
|
+
})
|
|
140
|
+
.register('blockquote', (el, ctx) => quoteLines(ctx.render(el.children)))
|
|
141
|
+
.register(
|
|
142
|
+
'pre',
|
|
143
|
+
(el, ctx) => fenced(ctx.render(el.children), el.attrs.lang),
|
|
144
|
+
{ variants: ['lang'] },
|
|
145
|
+
)
|
|
146
|
+
.register('hr', () => [BREAK, textNode('———'), BREAK])
|
|
147
|
+
.register('img', (el, ctx) => mediaNode(el, ctx), { variants: ['inline'] })
|
|
148
|
+
.register('audio', (el, ctx) => mediaNode(el, ctx))
|
|
149
|
+
.register('video', (el, ctx) => mediaNode(el, ctx))
|
|
150
|
+
.register('file', (el, ctx) => mediaNode(el, ctx))
|
|
151
|
+
|
|
152
|
+
return defineMode<SlackModeTypes>()({
|
|
153
|
+
id: 'mrkdwn',
|
|
154
|
+
elements,
|
|
155
|
+
limits: { 'limit.text.chars': SLACK_LIMITS.text },
|
|
156
|
+
assemble: (nodes) => assembleDraft(nodes),
|
|
157
|
+
serialize: (prepared, ctx) => serializeDraft(prepared, ctx, replyBroadcast),
|
|
158
|
+
})
|
|
159
|
+
}
|