@im-more/dsh-kook 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 +12 -0
- package/lib/index.js +2380 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +53 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +138 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +46 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +52 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +88 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +564 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +46 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +159 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +17 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +24 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +15 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +15 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/kmarkdown.d.ts +76 -0
- package/lib/types/kmarkdown.d.ts.map +1 -0
- package/lib/types/kmarkdown.js +201 -0
- package/lib/types/kmarkdown.js.map +1 -0
- package/lib/types/modes/kmarkdown.d.ts +17 -0
- package/lib/types/modes/kmarkdown.d.ts.map +1 -0
- package/lib/types/modes/kmarkdown.js +166 -0
- package/lib/types/modes/kmarkdown.js.map +1 -0
- package/lib/types/modes/shared.d.ts +192 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +371 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/request.d.ts +53 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +147 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +57 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +141 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/types.d.ts +807 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +168 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/webhook.d.ts +35 -0
- package/lib/types/webhook.d.ts.map +1 -0
- package/lib/types/webhook.js +132 -0
- package/lib/types/webhook.js.map +1 -0
- package/lib/types/ws.d.ts +54 -0
- package/lib/types/ws.d.ts.map +1 -0
- package/lib/types/ws.js +358 -0
- package/lib/types/ws.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +217 -0
- package/src/config.ts +84 -0
- package/src/decode.ts +660 -0
- package/src/errors.ts +188 -0
- package/src/identity.ts +32 -0
- package/src/index.ts +80 -0
- package/src/kmarkdown.ts +246 -0
- package/src/modes/kmarkdown.ts +204 -0
- package/src/modes/shared.ts +498 -0
- package/src/request.ts +196 -0
- package/src/send.ts +216 -0
- package/src/types.ts +764 -0
- package/src/webhook.ts +170 -0
- package/src/ws.ts +431 -0
- package/README.md +0 -3
package/src/request.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KOOK API transport: one request path for every v3 endpoint, the `{ code, message, data }` envelope and its error
|
|
3
|
+
* classification input, and the rate-limit headers.
|
|
4
|
+
*
|
|
5
|
+
* The typed method surface lives on `Internal` (`types.ts`); this module owns the wire mechanics so the migrated
|
|
6
|
+
* client and the adapter's own calls cannot diverge.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
10
|
+
import type {} from '@deepseek-ai/cordis-plugin-http'
|
|
11
|
+
import { KookApiError } from './errors.ts'
|
|
12
|
+
|
|
13
|
+
/** Endpoints and credentials shared by every KOOK API request of one bot. */
|
|
14
|
+
export interface KookRequest {
|
|
15
|
+
readonly ctx: Context
|
|
16
|
+
/** API root without a trailing slash. */
|
|
17
|
+
readonly baseUrl: string
|
|
18
|
+
/** Bot token, sent as `Authorization: Bot <token>`. */
|
|
19
|
+
readonly token: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The KOOK response envelope: `code === 0` means the call succeeded and `data` carries its answer. */
|
|
23
|
+
export interface KookEnvelope<T = unknown> {
|
|
24
|
+
code?: number
|
|
25
|
+
message?: string
|
|
26
|
+
data?: T
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Build the request endpoints for one bot.
|
|
31
|
+
* @param ctx - context whose `http` service performs requests.
|
|
32
|
+
* @param apiBaseUrl - API root (`https://www.kookapp.cn/api/v3`).
|
|
33
|
+
* @param token - bot token.
|
|
34
|
+
* @returns the shared request endpoints.
|
|
35
|
+
*/
|
|
36
|
+
export function createKookRequest(
|
|
37
|
+
ctx: Context,
|
|
38
|
+
apiBaseUrl: string,
|
|
39
|
+
token: string,
|
|
40
|
+
): KookRequest {
|
|
41
|
+
return { ctx, baseUrl: apiBaseUrl.replace(/\/+$/, ''), token }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Whether a caught value is the abort error of a cancelled request. */
|
|
45
|
+
function isAbortError(error: unknown): boolean {
|
|
46
|
+
return (
|
|
47
|
+
(error instanceof Error || error instanceof DOMException) &&
|
|
48
|
+
error.name === 'AbortError'
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Deliver the cancellation of a request: the signal's reason when it aborted, else the error `ctx.http` produced.
|
|
54
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
55
|
+
* @param signal - the cancellation signal handed to the request.
|
|
56
|
+
* @returns the error to rethrow.
|
|
57
|
+
*/
|
|
58
|
+
function cancellation(
|
|
59
|
+
cause: unknown,
|
|
60
|
+
signal: AbortSignal | undefined,
|
|
61
|
+
): unknown {
|
|
62
|
+
return signal?.aborted === true ? signal.reason : cause
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Whether a failed request was cancelled, by its signal or by the error the transport produced. */
|
|
66
|
+
function isCancellation(
|
|
67
|
+
cause: unknown,
|
|
68
|
+
signal: AbortSignal | undefined,
|
|
69
|
+
): boolean {
|
|
70
|
+
if (signal?.aborted === true) return true
|
|
71
|
+
if (isAbortError(cause)) return true
|
|
72
|
+
return signal?.reason !== undefined && cause === signal.reason
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Delay a rate-limited answer asked for, in milliseconds.
|
|
77
|
+
*
|
|
78
|
+
* KOOK documents `X-Rate-Limit-Reset` (seconds) and says nothing about `Retry-After`; both are read, the standard
|
|
79
|
+
* header first, because an undocumented one is only useful while it is present.
|
|
80
|
+
*
|
|
81
|
+
* @param response - the answer.
|
|
82
|
+
* @returns the delay in milliseconds, or undefined when neither header is present or readable.
|
|
83
|
+
*/
|
|
84
|
+
export function retryAfterOf(response: Response): number | undefined {
|
|
85
|
+
const standard = response.headers.get('retry-after')
|
|
86
|
+
if (standard !== null) {
|
|
87
|
+
const seconds = Number(standard)
|
|
88
|
+
if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1_000
|
|
89
|
+
}
|
|
90
|
+
const reset = response.headers.get('x-rate-limit-reset')
|
|
91
|
+
if (reset !== null) {
|
|
92
|
+
const seconds = Number(reset)
|
|
93
|
+
if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1_000
|
|
94
|
+
}
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Read a JSON answer; an unreadable body is reported as absence rather than a parse error. */
|
|
99
|
+
async function jsonOf(response: Response): Promise<KookEnvelope | undefined> {
|
|
100
|
+
let body: unknown
|
|
101
|
+
try {
|
|
102
|
+
body = await response.json()
|
|
103
|
+
} catch (error) {
|
|
104
|
+
// The answer carried no JSON (a proxy error page, a compressed body): the caller reports the status instead.
|
|
105
|
+
error
|
|
106
|
+
return undefined
|
|
107
|
+
}
|
|
108
|
+
if (typeof body !== 'object' || body === null) return undefined
|
|
109
|
+
return body
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The URL of one request. */
|
|
113
|
+
function urlOf(request: KookRequest, path: string): string {
|
|
114
|
+
return `${request.baseUrl}${path.startsWith('/') ? path : `/${path}`}`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Call one KOOK API endpoint and return its envelope's `data`.
|
|
119
|
+
*
|
|
120
|
+
* @param request - endpoints for this bot.
|
|
121
|
+
* @param method - HTTP method (`GET`, `POST`, `DELETE`).
|
|
122
|
+
* @param path - endpoint below the API root (`/message/create`).
|
|
123
|
+
* @param payload - query parameters of `GET`/`DELETE`, else the request body (JSON or a multipart form).
|
|
124
|
+
* @param signal - cancellation.
|
|
125
|
+
* @returns the endpoint's `data` field.
|
|
126
|
+
* @throws KookApiError for a transport failure, a non-2xx answer or a non-zero business code.
|
|
127
|
+
*/
|
|
128
|
+
export async function invoke<T = unknown>(
|
|
129
|
+
request: KookRequest,
|
|
130
|
+
method: string,
|
|
131
|
+
path: string,
|
|
132
|
+
payload?: object,
|
|
133
|
+
signal?: AbortSignal,
|
|
134
|
+
): Promise<T> {
|
|
135
|
+
const config: {
|
|
136
|
+
method: string
|
|
137
|
+
headers: Record<string, string>
|
|
138
|
+
params?: object
|
|
139
|
+
data?: object
|
|
140
|
+
signal?: AbortSignal
|
|
141
|
+
} = {
|
|
142
|
+
method,
|
|
143
|
+
headers: { authorization: `Bot ${request.token}` },
|
|
144
|
+
}
|
|
145
|
+
if (method === 'GET' || method === 'DELETE') config.params = payload ?? {}
|
|
146
|
+
else config.data = payload ?? {}
|
|
147
|
+
if (signal !== undefined) config.signal = signal
|
|
148
|
+
|
|
149
|
+
let response: Response
|
|
150
|
+
try {
|
|
151
|
+
response = await request.ctx.http(urlOf(request, path), config)
|
|
152
|
+
} catch (cause) {
|
|
153
|
+
if (isCancellation(cause, signal)) throw cancellation(cause, signal)
|
|
154
|
+
throw new KookApiError(`kook ${method} ${path} request failed`, { cause })
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const status = response.status
|
|
158
|
+
const retryAfterMs = retryAfterOf(response)
|
|
159
|
+
const body = await jsonOf(response)
|
|
160
|
+
if (body === undefined) {
|
|
161
|
+
// Reading the body fails when the answer is cut short by cancellation; that is still a cancellation.
|
|
162
|
+
if (signal?.aborted === true) throw signal.reason
|
|
163
|
+
throw new KookApiError(
|
|
164
|
+
`kook ${method} ${path} answered ${status} without a JSON envelope`,
|
|
165
|
+
{ status, ...(retryAfterMs === undefined ? {} : { retryAfterMs }) },
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
if (status === 429) {
|
|
169
|
+
throw new KookApiError(`kook ${method} ${path} was rate limited`, {
|
|
170
|
+
status,
|
|
171
|
+
...(typeof body.code === 'number' ? { code: body.code } : {}),
|
|
172
|
+
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
if (body.code !== 0) {
|
|
176
|
+
throw new KookApiError(
|
|
177
|
+
`kook ${method} ${path} failed: ${body.message ?? 'the answer carries no success code'}`,
|
|
178
|
+
{
|
|
179
|
+
status,
|
|
180
|
+
...(typeof body.code === 'number' ? { code: body.code } : {}),
|
|
181
|
+
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
|
182
|
+
},
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
if (status >= 400) {
|
|
186
|
+
throw new KookApiError(
|
|
187
|
+
`kook ${method} ${path} answered ${status} with a success envelope`,
|
|
188
|
+
{
|
|
189
|
+
status,
|
|
190
|
+
code: body.code,
|
|
191
|
+
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
|
192
|
+
},
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
return body.data as T
|
|
196
|
+
}
|
package/src/send.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound hooks: the send pipeline's payload, deletion, member reads and message reads.
|
|
3
|
+
*
|
|
4
|
+
* A channel message goes to `/message/create` (or `/message/view`, `/message/delete`); a direct one goes to the
|
|
5
|
+
* upstream adapter's `/user-chat/*` routes. The two are told apart exactly as upstream tells them apart: a channel id
|
|
6
|
+
* is 16–19 digits and a direct chat code is 32 hexadecimal characters, so the longer id is the direct chat.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
10
|
+
import type {
|
|
11
|
+
ChatRef,
|
|
12
|
+
InboundMessage,
|
|
13
|
+
MembersPage,
|
|
14
|
+
MessageHandle,
|
|
15
|
+
SendContext,
|
|
16
|
+
SentPart,
|
|
17
|
+
Target,
|
|
18
|
+
UserRef,
|
|
19
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
20
|
+
import type { KookBot } from './bot.ts'
|
|
21
|
+
import { decodeMessage, userRefOf } from './decode.ts'
|
|
22
|
+
import type { KookReferrer } from './decode.ts'
|
|
23
|
+
import { KOOK_LIMITS } from './modes/shared.ts'
|
|
24
|
+
import type { KookPayload } from './modes/shared.ts'
|
|
25
|
+
import { invoke } from './request.ts'
|
|
26
|
+
import type { KookRequest } from './request.ts'
|
|
27
|
+
import type {
|
|
28
|
+
Channel,
|
|
29
|
+
GuildUserList,
|
|
30
|
+
Message as KookMessage,
|
|
31
|
+
MessageReturn,
|
|
32
|
+
PrivateChat,
|
|
33
|
+
} from './types.ts'
|
|
34
|
+
|
|
35
|
+
/** Whether a chat id names a direct chat: a `chat_code` (32 hex characters) is longer than a channel id. */
|
|
36
|
+
export function isDirectChannel(chatId: string): boolean {
|
|
37
|
+
return chatId.length > 30
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The platform path of one message operation, by chat kind. */
|
|
41
|
+
function pathOf(chatId: string, channel: string, direct: string): string {
|
|
42
|
+
return isDirectChannel(chatId) ? direct : channel
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The published part of one sent message. */
|
|
46
|
+
function partOf(
|
|
47
|
+
receipt: MessageReturn | undefined,
|
|
48
|
+
kind: string,
|
|
49
|
+
target: Target,
|
|
50
|
+
): SentPart {
|
|
51
|
+
if (receipt?.msg_id === undefined || receipt.msg_id === '') {
|
|
52
|
+
// The request reached the platform, so an answer without a receipt is a published outcome, not a refusal.
|
|
53
|
+
throw new PlatformError(
|
|
54
|
+
'invalid',
|
|
55
|
+
'kook: the platform answered without a msg_id',
|
|
56
|
+
{ published: 'unknown' },
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
const referrer: KookReferrer = {
|
|
60
|
+
chatId: target.chat.id,
|
|
61
|
+
channelType: target.chat.type === 'group' ? 'GROUP' : 'PERSON',
|
|
62
|
+
messageId: receipt.msg_id,
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
id: receipt.msg_id,
|
|
66
|
+
kind,
|
|
67
|
+
time:
|
|
68
|
+
receipt.msg_timestamp === undefined ? Date.now() : receipt.msg_timestamp,
|
|
69
|
+
referrer,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Send one serialized payload.
|
|
75
|
+
*
|
|
76
|
+
* @param request - endpoints for this bot.
|
|
77
|
+
* @param payload - the serialized draft (`type: 9` text or `type: 10` card).
|
|
78
|
+
* @param target - the chat the payload goes to.
|
|
79
|
+
* @param _ctx - part index and quote, already applied by `serialize`.
|
|
80
|
+
* @returns the published part.
|
|
81
|
+
*/
|
|
82
|
+
export async function sendPayload(
|
|
83
|
+
request: KookRequest,
|
|
84
|
+
payload: KookPayload,
|
|
85
|
+
target: Target,
|
|
86
|
+
_ctx: SendContext,
|
|
87
|
+
): Promise<SentPart> {
|
|
88
|
+
// KOOK chat messages have no threads: `thread: true` has nothing to follow (ignored), an explicit id is refused.
|
|
89
|
+
if (typeof target.thread === 'string') {
|
|
90
|
+
throw new PlatformError('invalid', 'kook has no message threads', {
|
|
91
|
+
published: 'no',
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
const direct = isDirectChannel(target.chat.id)
|
|
95
|
+
const body: Record<string, unknown> = {
|
|
96
|
+
type: payload.type,
|
|
97
|
+
content: payload.content,
|
|
98
|
+
}
|
|
99
|
+
if (direct) body.chat_code = target.chat.id
|
|
100
|
+
else body.target_id = target.chat.id
|
|
101
|
+
// `serialize` only puts the quote on the first part of a split message.
|
|
102
|
+
if (payload.quote !== undefined) body.quote = payload.quote
|
|
103
|
+
const receipt = await invoke<MessageReturn>(
|
|
104
|
+
request,
|
|
105
|
+
'POST',
|
|
106
|
+
direct ? '/user-chat/create-msg' : '/message/create',
|
|
107
|
+
body,
|
|
108
|
+
)
|
|
109
|
+
return partOf(receipt, payload.kind, target)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Recall one published part.
|
|
114
|
+
* @param request - endpoints for this bot.
|
|
115
|
+
* @param part - the part to recall.
|
|
116
|
+
* @param handle - the message the part belongs to, which names the chat.
|
|
117
|
+
*/
|
|
118
|
+
export async function deletePart(
|
|
119
|
+
request: KookRequest,
|
|
120
|
+
part: SentPart,
|
|
121
|
+
handle: MessageHandle,
|
|
122
|
+
): Promise<void> {
|
|
123
|
+
await invoke(
|
|
124
|
+
request,
|
|
125
|
+
'POST',
|
|
126
|
+
pathOf(handle.target.chat.id, '/message/delete', '/user-chat/delete-msg'),
|
|
127
|
+
{ msg_id: part.id },
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The page number a cursor names; anything unreadable starts at the first page. */
|
|
132
|
+
function pageOf(cursor: string | undefined): number {
|
|
133
|
+
const page = Number(cursor ?? '')
|
|
134
|
+
return Number.isFinite(page) && page >= 1 ? Math.floor(page) : 1
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* List the members of a chat.
|
|
139
|
+
*
|
|
140
|
+
* A group chat pages `/guild/user-list`; the guild id comes from `/channel/view` because the chat only knows its
|
|
141
|
+
* channel. A direct chat has exactly one member, which `/user-chat/view` reports.
|
|
142
|
+
*
|
|
143
|
+
* @param request - endpoints for this bot.
|
|
144
|
+
* @param chat - the chat the caller asked about.
|
|
145
|
+
* @param cursor - page number of the previous page's `next`.
|
|
146
|
+
* @param limit - page size the platform should use (at most 50).
|
|
147
|
+
* @returns one page of members.
|
|
148
|
+
*/
|
|
149
|
+
export async function members(
|
|
150
|
+
request: KookRequest,
|
|
151
|
+
chat: ChatRef,
|
|
152
|
+
cursor?: string,
|
|
153
|
+
limit?: number,
|
|
154
|
+
): Promise<MembersPage> {
|
|
155
|
+
if (isDirectChannel(chat.id)) {
|
|
156
|
+
const view = await invoke<PrivateChat>(request, 'GET', '/user-chat/view', {
|
|
157
|
+
chat_code: chat.id,
|
|
158
|
+
})
|
|
159
|
+
const target = view?.target_info
|
|
160
|
+
return { data: target === undefined ? [] : [userRefOf(target)] }
|
|
161
|
+
}
|
|
162
|
+
const page = pageOf(cursor)
|
|
163
|
+
const pageSize = Math.max(
|
|
164
|
+
1,
|
|
165
|
+
Math.min(limit ?? KOOK_LIMITS.membersPageSize, KOOK_LIMITS.membersPageSize),
|
|
166
|
+
)
|
|
167
|
+
const channel = await invoke<Channel>(request, 'GET', '/channel/view', {
|
|
168
|
+
target_id: chat.id,
|
|
169
|
+
})
|
|
170
|
+
if (channel?.guild_id === undefined || channel.guild_id === '') {
|
|
171
|
+
throw new PlatformError(
|
|
172
|
+
'not-found',
|
|
173
|
+
`kook: channel ${chat.id} names no guild`,
|
|
174
|
+
{ published: 'no' },
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
const list = await invoke<GuildUserList>(request, 'GET', '/guild/user-list', {
|
|
178
|
+
guild_id: channel.guild_id,
|
|
179
|
+
page,
|
|
180
|
+
page_size: pageSize,
|
|
181
|
+
})
|
|
182
|
+
const data: UserRef[] = list.items.map((member) => userRefOf(member))
|
|
183
|
+
const total = list.meta?.page_total ?? page
|
|
184
|
+
return { data, ...(page < total ? { next: String(page + 1) } : {}) }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Read one message by id.
|
|
189
|
+
*
|
|
190
|
+
* The read answer names the channel but not its type, so the requested chat is what the decoded message reports; the
|
|
191
|
+
* host verifies that its id matches the chat it asked about.
|
|
192
|
+
*
|
|
193
|
+
* @param request - endpoints for this bot.
|
|
194
|
+
* @param bot - the adapter, needed to decode the answer.
|
|
195
|
+
* @param chat - the chat the caller asked about.
|
|
196
|
+
* @param id - platform message id.
|
|
197
|
+
* @returns the decoded message.
|
|
198
|
+
*/
|
|
199
|
+
export async function getMessage(
|
|
200
|
+
request: KookRequest,
|
|
201
|
+
bot: KookBot,
|
|
202
|
+
chat: ChatRef,
|
|
203
|
+
id: string,
|
|
204
|
+
): Promise<InboundMessage> {
|
|
205
|
+
const path = isDirectChannel(chat.id) ? '/user-chat/view' : '/message/view'
|
|
206
|
+
// Both routes take `msg_id` for a message read; `chat_code`/`target_id` are the conversation reads.
|
|
207
|
+
const raw = await invoke<KookMessage>(request, 'GET', path, { msg_id: id })
|
|
208
|
+
if (raw === undefined || raw === null || typeof raw !== 'object') {
|
|
209
|
+
throw new PlatformError(
|
|
210
|
+
'not-found',
|
|
211
|
+
`kook: message ${id} is not in ${chat.id}`,
|
|
212
|
+
{ published: 'no' },
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
return decodeMessage(bot, raw, chat)
|
|
216
|
+
}
|