@im-more/dsh-line 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 +1701 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +55 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +169 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +47 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +77 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +60 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +321 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +47 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +112 -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 +17 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +16 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/media.d.ts +19 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +55 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/shared.d.ts +126 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +316 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/modes/text.d.ts +13 -0
- package/lib/types/modes/text.d.ts.map +1 -0
- package/lib/types/modes/text.js +143 -0
- package/lib/types/modes/text.js.map +1 -0
- package/lib/types/request.d.ts +49 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +162 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +43 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +74 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/signature.d.ts +19 -0
- package/lib/types/signature.d.ts.map +1 -0
- package/lib/types/signature.js +30 -0
- package/lib/types/signature.js.map +1 -0
- package/lib/types/transport/media.d.ts +62 -0
- package/lib/types/transport/media.d.ts.map +1 -0
- package/lib/types/transport/media.js +104 -0
- package/lib/types/transport/media.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +62 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +208 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types/definition.d.ts +1006 -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/index.d.ts +542 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +4 -0
- package/lib/types/types/index.js.map +1 -0
- package/lib/types/types/internal.d.ts +8 -0
- package/lib/types/types/internal.d.ts.map +1 -0
- package/lib/types/types/internal.js +234 -0
- package/lib/types/types/internal.js.map +1 -0
- package/lib/types/types/request.d.ts +341 -0
- package/lib/types/types/request.d.ts.map +1 -0
- package/lib/types/types/request.js +2 -0
- package/lib/types/types/request.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +229 -0
- package/src/config.ts +113 -0
- package/src/decode.ts +418 -0
- package/src/errors.ts +138 -0
- package/src/identity.ts +28 -0
- package/src/index.ts +60 -0
- package/src/media.ts +66 -0
- package/src/modes/shared.ts +408 -0
- package/src/modes/text.ts +177 -0
- package/src/request.ts +215 -0
- package/src/send.ts +104 -0
- package/src/signature.ts +32 -0
- package/src/transport/media.ts +117 -0
- package/src/transport/webhook.ts +266 -0
- package/src/types/.eslintrc.yml +2 -0
- package/src/types/definition.ts +1008 -0
- package/src/types/index.ts +593 -0
- package/src/types/internal.ts +233 -0
- package/src/types/request.ts +341 -0
- package/README.md +0 -3
package/src/decode.ts
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound decoding: one LINE webhook event becomes an intake `Session` (`message`), a button interaction, or an
|
|
3
|
+
* unsent-message notification.
|
|
4
|
+
*
|
|
5
|
+
* LINE gives the content away exactly once — there is no API that reads a message back — so the webhook payload, its
|
|
6
|
+
* UTF-16 indices included, is the only source of text, mentions, quotes and media.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
10
|
+
import type {
|
|
11
|
+
ButtonEvent,
|
|
12
|
+
ChatRef,
|
|
13
|
+
InboundMessage,
|
|
14
|
+
Session,
|
|
15
|
+
UserRef,
|
|
16
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
17
|
+
import { h, normalize } from '@deepseek-ai/satori-element'
|
|
18
|
+
import type { Child } from '@deepseek-ai/satori-element'
|
|
19
|
+
import type { LineBot } from './bot.ts'
|
|
20
|
+
import type {
|
|
21
|
+
ContentProvider,
|
|
22
|
+
EventMessage,
|
|
23
|
+
EventSource,
|
|
24
|
+
MessageEvent,
|
|
25
|
+
PostbackEvent,
|
|
26
|
+
TextEventMessage,
|
|
27
|
+
WebhookEvent,
|
|
28
|
+
} from './types/index.ts'
|
|
29
|
+
|
|
30
|
+
/** Adapter-private context carried through `Target.referrer`. */
|
|
31
|
+
export interface LineReferrer {
|
|
32
|
+
/** Platform id of the message this context belongs to; absent on a postback. */
|
|
33
|
+
messageId?: string
|
|
34
|
+
/** Token that quotes this inbound message, when the platform offers one (text, sticker, image, video). */
|
|
35
|
+
quoteToken?: string
|
|
36
|
+
/** One-use, one-minute reply token of the event; the adapter itself always pushes. */
|
|
37
|
+
replyToken?: string
|
|
38
|
+
timestamp: number
|
|
39
|
+
mode: 'active' | 'standby'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** How a chat id names its platform session: the `chat.id` prefix. */
|
|
43
|
+
export type LineChatKind = 'user' | 'group' | 'room'
|
|
44
|
+
|
|
45
|
+
/** A parsed `chat.id`: the platform conversation kind and its id. */
|
|
46
|
+
export interface LineChatAddress {
|
|
47
|
+
kind: LineChatKind
|
|
48
|
+
id: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The `chat.id` of a source: `user:`, `group:` or `room:` plus the platform id. */
|
|
52
|
+
export function chatIdOf(source: EventSource): string {
|
|
53
|
+
switch (source.type) {
|
|
54
|
+
case 'user':
|
|
55
|
+
return `user:${source.userId}`
|
|
56
|
+
case 'group':
|
|
57
|
+
return `group:${source.groupId}`
|
|
58
|
+
case 'room':
|
|
59
|
+
return `room:${source.roomId}`
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The `ChatRef` of a source; a room is a group in the DSH vocabulary. */
|
|
64
|
+
export function chatRefOf(source: EventSource): ChatRef {
|
|
65
|
+
const id = chatIdOf(source)
|
|
66
|
+
return { id, type: source.type === 'user' ? 'direct' : 'group' }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Split a `chat.id` back into the platform conversation.
|
|
71
|
+
*
|
|
72
|
+
* @param chatId - a `chat.id` this adapter wrote, or one a profile configured by hand.
|
|
73
|
+
* @returns the conversation kind and its platform id.
|
|
74
|
+
* @throws PlatformError `invalid` when the id carries no known prefix.
|
|
75
|
+
*/
|
|
76
|
+
export function parseChatId(chatId: string): LineChatAddress {
|
|
77
|
+
const index = chatId.indexOf(':')
|
|
78
|
+
const kind = index === -1 ? '' : chatId.slice(0, index)
|
|
79
|
+
if (kind !== 'user' && kind !== 'group' && kind !== 'room') {
|
|
80
|
+
throw new PlatformError(
|
|
81
|
+
'invalid',
|
|
82
|
+
`line: chat id "${chatId}" carries no user:/group:/room: prefix`,
|
|
83
|
+
{ published: 'no' },
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
return { kind, id: chatId.slice(index + 1) }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The `pushMessage` recipient of a `chat.id`.
|
|
91
|
+
*
|
|
92
|
+
* @param chatId - a `chat.id` this adapter wrote.
|
|
93
|
+
* @returns the `to` value of a push request.
|
|
94
|
+
* @throws PlatformError `invalid` when the id carries no known prefix.
|
|
95
|
+
*/
|
|
96
|
+
export function chatTargetOf(chatId: string): string {
|
|
97
|
+
return parseChatId(chatId).id
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The sender of a source; LINE omits the user id of an unconverted user, which collapses to `anonymous`. */
|
|
101
|
+
function senderOf(source: EventSource): UserRef {
|
|
102
|
+
const id =
|
|
103
|
+
source.type === 'user' ? source.userId : (source.userId ?? 'anonymous')
|
|
104
|
+
return { id }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A mention's visible name, without the leading `@` the payload carries. */
|
|
108
|
+
function mentionName(slice: string): string {
|
|
109
|
+
return slice.startsWith('@') ? slice.slice(1) : slice
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** One UTF-16 slice of a text message; LINE indices count UTF-16 code units. */
|
|
113
|
+
function sliceOf(text: string, index: number, length: number): string {
|
|
114
|
+
return text.slice(index, index + length)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** One span of a text message the payload names separately from the plain text. */
|
|
118
|
+
type TextSpan =
|
|
119
|
+
| {
|
|
120
|
+
kind: 'emoji'
|
|
121
|
+
index: number
|
|
122
|
+
length: number
|
|
123
|
+
emoji: NonNullable<TextEventMessage['emojis']>[number]
|
|
124
|
+
}
|
|
125
|
+
| {
|
|
126
|
+
kind: 'mention'
|
|
127
|
+
index: number
|
|
128
|
+
length: number
|
|
129
|
+
mention: NonNullable<TextEventMessage['mention']>['mentionees'][number]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The element nodes of one text message, slicing the body at its emoji and mention spans. */
|
|
133
|
+
function textNodes(message: TextEventMessage): Child[] {
|
|
134
|
+
const text = message.text
|
|
135
|
+
const spans: TextSpan[] = [
|
|
136
|
+
...(message.emojis ?? []).map((emoji): TextSpan => ({
|
|
137
|
+
kind: 'emoji',
|
|
138
|
+
index: emoji.index,
|
|
139
|
+
length: emoji.length,
|
|
140
|
+
emoji,
|
|
141
|
+
})),
|
|
142
|
+
...(message.mention?.mentionees ?? []).map((mention): TextSpan => ({
|
|
143
|
+
kind: 'mention',
|
|
144
|
+
index: mention.index,
|
|
145
|
+
length: mention.length,
|
|
146
|
+
mention,
|
|
147
|
+
})),
|
|
148
|
+
]
|
|
149
|
+
.filter(
|
|
150
|
+
(span) =>
|
|
151
|
+
span.index >= 0 &&
|
|
152
|
+
span.length > 0 &&
|
|
153
|
+
span.index + span.length <= text.length,
|
|
154
|
+
)
|
|
155
|
+
.sort((a, b) => a.index - b.index)
|
|
156
|
+
|
|
157
|
+
const nodes: Child[] = []
|
|
158
|
+
let cursor = 0
|
|
159
|
+
for (const span of spans) {
|
|
160
|
+
// A span the payload reports inside another one cannot be rendered without repeating text.
|
|
161
|
+
if (span.index < cursor) continue
|
|
162
|
+
if (span.index > cursor) nodes.push(text.slice(cursor, span.index))
|
|
163
|
+
if (span.kind === 'emoji') {
|
|
164
|
+
nodes.push(
|
|
165
|
+
h('emoji', { id: `e:${span.emoji.productId}:${span.emoji.emojiId}` }),
|
|
166
|
+
)
|
|
167
|
+
} else if (span.mention.type === 'all') {
|
|
168
|
+
nodes.push(h('at', { type: 'all' }))
|
|
169
|
+
} else if (span.mention.userId === undefined) {
|
|
170
|
+
// An `at[type=user]` needs the id it addresses; a mention of a user who did not agree to the terms of use names
|
|
171
|
+
// only the visible text, which stays readable as plain text.
|
|
172
|
+
nodes.push(sliceOf(text, span.index, span.length))
|
|
173
|
+
} else {
|
|
174
|
+
nodes.push(
|
|
175
|
+
h('at', {
|
|
176
|
+
type: 'user',
|
|
177
|
+
id: span.mention.userId,
|
|
178
|
+
name: mentionName(sliceOf(text, span.index, span.length)),
|
|
179
|
+
}),
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
cursor = span.index + span.length
|
|
183
|
+
}
|
|
184
|
+
if (cursor < text.length) nodes.push(text.slice(cursor))
|
|
185
|
+
return nodes
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The source of an inbound media element: LINE's own content API, or the URL a third party provided. */
|
|
189
|
+
function contentSource(
|
|
190
|
+
bot: LineBot,
|
|
191
|
+
messageId: string,
|
|
192
|
+
provider: ContentProvider | ContentProvider<false> | undefined,
|
|
193
|
+
): string {
|
|
194
|
+
if (provider?.type === 'external') return provider.originalContentUrl
|
|
195
|
+
return bot.getInternalUrl(`/assets/${encodeURIComponent(messageId)}`)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The elements of one message of the seven platform types. */
|
|
199
|
+
function messageNodes(bot: LineBot, message: EventMessage): Child[] {
|
|
200
|
+
switch (message.type) {
|
|
201
|
+
case 'text':
|
|
202
|
+
return textNodes(message)
|
|
203
|
+
case 'image':
|
|
204
|
+
return [
|
|
205
|
+
h('img', {
|
|
206
|
+
src: contentSource(bot, message.id, message.contentProvider),
|
|
207
|
+
}),
|
|
208
|
+
]
|
|
209
|
+
case 'video':
|
|
210
|
+
return [
|
|
211
|
+
h('video', {
|
|
212
|
+
src: contentSource(bot, message.id, message.contentProvider),
|
|
213
|
+
...(message.duration === undefined
|
|
214
|
+
? {}
|
|
215
|
+
: { duration: message.duration }),
|
|
216
|
+
}),
|
|
217
|
+
]
|
|
218
|
+
case 'audio':
|
|
219
|
+
return [
|
|
220
|
+
h('audio', {
|
|
221
|
+
src: contentSource(bot, message.id, message.contentProvider),
|
|
222
|
+
duration: message.duration,
|
|
223
|
+
}),
|
|
224
|
+
]
|
|
225
|
+
case 'file': {
|
|
226
|
+
const size = Number(message.fileSize)
|
|
227
|
+
return [
|
|
228
|
+
h('file', {
|
|
229
|
+
src: bot.getInternalUrl(`/assets/${encodeURIComponent(message.id)}`),
|
|
230
|
+
name: message.fileName,
|
|
231
|
+
...(Number.isFinite(size) ? { size } : {}),
|
|
232
|
+
}),
|
|
233
|
+
]
|
|
234
|
+
}
|
|
235
|
+
case 'location': {
|
|
236
|
+
const lines = [
|
|
237
|
+
message.title,
|
|
238
|
+
message.address,
|
|
239
|
+
`${message.latitude}, ${message.longitude}`,
|
|
240
|
+
].filter((line) => line !== '')
|
|
241
|
+
return [h('p', lines.join('\n'))]
|
|
242
|
+
}
|
|
243
|
+
case 'sticker':
|
|
244
|
+
return [
|
|
245
|
+
h('emoji', {
|
|
246
|
+
id: `s:${message.packageId}:${message.stickerId}`,
|
|
247
|
+
}),
|
|
248
|
+
]
|
|
249
|
+
default:
|
|
250
|
+
// A message type this snapshot does not know: the caller keeps the payload in a `<raw>` element.
|
|
251
|
+
return []
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Mention flags and mentioned users of one message. */
|
|
256
|
+
function mentionsOf(
|
|
257
|
+
bot: LineBot,
|
|
258
|
+
message: EventMessage,
|
|
259
|
+
): InboundMessage['mentions'] {
|
|
260
|
+
if (message.type !== 'text') return { self: false, all: false, users: [] }
|
|
261
|
+
const text = message.text
|
|
262
|
+
const mentionees = message.mention?.mentionees ?? []
|
|
263
|
+
const users: UserRef[] = []
|
|
264
|
+
for (const mention of mentionees) {
|
|
265
|
+
if (mention.type !== 'user' || mention.userId === undefined) continue
|
|
266
|
+
users.push({
|
|
267
|
+
id: mention.userId,
|
|
268
|
+
name: mentionName(sliceOf(text, mention.index, mention.length)),
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
const all = mentionees.some((mention) => mention.type === 'all')
|
|
272
|
+
// `isSelf` is the platform's own answer to "was the bot mentioned"; an older payload without it falls back to the id.
|
|
273
|
+
const declaresSelf = mentionees.some(
|
|
274
|
+
(mention) => mention.isSelf !== undefined,
|
|
275
|
+
)
|
|
276
|
+
const self =
|
|
277
|
+
mentionees.some((mention) => mention.isSelf === true) ||
|
|
278
|
+
(!declaresSelf &&
|
|
279
|
+
mentionees.some(
|
|
280
|
+
(mention) => mention.type === 'user' && mention.userId === bot.selfId,
|
|
281
|
+
))
|
|
282
|
+
return { self, all, users }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** The quote token of a message, which only the four quotable types carry. */
|
|
286
|
+
function quoteTokenOf(message: EventMessage): string | undefined {
|
|
287
|
+
switch (message.type) {
|
|
288
|
+
case 'text':
|
|
289
|
+
case 'image':
|
|
290
|
+
case 'video':
|
|
291
|
+
case 'sticker':
|
|
292
|
+
return message.quoteToken
|
|
293
|
+
default:
|
|
294
|
+
return undefined
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The quote an inbound message answers, when the platform names it. */
|
|
299
|
+
function quoteOf(message: EventMessage): { id: string } | undefined {
|
|
300
|
+
return message.type === 'text' && message.quotedMessageId !== undefined
|
|
301
|
+
? { id: message.quotedMessageId }
|
|
302
|
+
: undefined
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Decode one message event into the normalized inbound message. */
|
|
306
|
+
function decodeMessage(bot: LineBot, event: MessageEvent): InboundMessage {
|
|
307
|
+
const source = event.source
|
|
308
|
+
const message = event.message
|
|
309
|
+
const referrer: LineReferrer = {
|
|
310
|
+
messageId: message.id,
|
|
311
|
+
timestamp: event.timestamp,
|
|
312
|
+
mode: event.mode,
|
|
313
|
+
...(quoteTokenOf(message) === undefined
|
|
314
|
+
? {}
|
|
315
|
+
: { quoteToken: quoteTokenOf(message)! }),
|
|
316
|
+
...(event.replyToken === undefined ? {} : { replyToken: event.replyToken }),
|
|
317
|
+
}
|
|
318
|
+
const nodes = messageNodes(bot, message)
|
|
319
|
+
const content = normalize(
|
|
320
|
+
nodes.length > 0
|
|
321
|
+
? nodes
|
|
322
|
+
: [h('raw', { platform: 'line', type: 'message', data: message })],
|
|
323
|
+
)
|
|
324
|
+
const quote = quoteOf(message)
|
|
325
|
+
return {
|
|
326
|
+
id: message.id,
|
|
327
|
+
chat: chatRefOf(source),
|
|
328
|
+
sender: senderOf(source),
|
|
329
|
+
content,
|
|
330
|
+
...(quote === undefined ? {} : { quote }),
|
|
331
|
+
mentions: mentionsOf(bot, message),
|
|
332
|
+
time: event.timestamp,
|
|
333
|
+
referrer,
|
|
334
|
+
raw: event,
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Decode the button context of a postback, which names no platform message of its own. */
|
|
339
|
+
function decodePostback(bot: LineBot, event: PostbackEvent): Session {
|
|
340
|
+
const chat = chatRefOf(event.source)
|
|
341
|
+
const referrer: LineReferrer = {
|
|
342
|
+
timestamp: event.timestamp,
|
|
343
|
+
mode: event.mode,
|
|
344
|
+
replyToken: event.replyToken,
|
|
345
|
+
}
|
|
346
|
+
const button: ButtonEvent = {
|
|
347
|
+
id: event.postback.data,
|
|
348
|
+
data: event.postback.data,
|
|
349
|
+
referrer,
|
|
350
|
+
time: event.timestamp,
|
|
351
|
+
bot,
|
|
352
|
+
user: senderOf(event.source),
|
|
353
|
+
chat,
|
|
354
|
+
}
|
|
355
|
+
return bot.session({
|
|
356
|
+
type: 'interaction/button',
|
|
357
|
+
id: event.webhookEventId,
|
|
358
|
+
timestamp: event.timestamp,
|
|
359
|
+
button,
|
|
360
|
+
raw: event,
|
|
361
|
+
})
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Decode one webhook event.
|
|
366
|
+
*
|
|
367
|
+
* Events the DSH contract has no consumer for (follows, joins, member changes, beacons, edits, deliveries …) are
|
|
368
|
+
* dropped with a debug line: the model has no `friend-*`/`guild-*` event to receive them.
|
|
369
|
+
*
|
|
370
|
+
* @param bot - the receiving adapter.
|
|
371
|
+
* @param event - one event of the webhook body.
|
|
372
|
+
* @returns the session to publish, or undefined for an ignored event.
|
|
373
|
+
*/
|
|
374
|
+
export function decodeEvent(
|
|
375
|
+
bot: LineBot,
|
|
376
|
+
event: WebhookEvent,
|
|
377
|
+
): Session | undefined {
|
|
378
|
+
if (event.mode === 'standby') {
|
|
379
|
+
// The channel is handled by a human agent; the platform explicitly says not to send from a standby event.
|
|
380
|
+
bot.ctx.logger.debug(
|
|
381
|
+
'line: skipping standby event %s',
|
|
382
|
+
event.webhookEventId,
|
|
383
|
+
)
|
|
384
|
+
return undefined
|
|
385
|
+
}
|
|
386
|
+
if (event.deliveryContext?.isRedelivery === true) {
|
|
387
|
+
// A redelivery is handled like any other event: skipping it would lose the event whose first delivery failed.
|
|
388
|
+
bot.ctx.logger.debug(
|
|
389
|
+
'line: handling redelivered event %s',
|
|
390
|
+
event.webhookEventId,
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
switch (event.type) {
|
|
394
|
+
case 'message': {
|
|
395
|
+
const message = decodeMessage(bot, event)
|
|
396
|
+
return bot.session({
|
|
397
|
+
type: 'message',
|
|
398
|
+
id: message.id,
|
|
399
|
+
timestamp: message.time,
|
|
400
|
+
message,
|
|
401
|
+
raw: event,
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
case 'unsend':
|
|
405
|
+
return bot.session({
|
|
406
|
+
type: 'message-deleted',
|
|
407
|
+
id: event.webhookEventId,
|
|
408
|
+
timestamp: event.timestamp,
|
|
409
|
+
deleted: { id: event.unsend.messageId, chat: chatRefOf(event.source) },
|
|
410
|
+
raw: event,
|
|
411
|
+
})
|
|
412
|
+
case 'postback':
|
|
413
|
+
return decodePostback(bot, event)
|
|
414
|
+
default:
|
|
415
|
+
bot.ctx.logger.debug('line: ignoring a %s event', event.type)
|
|
416
|
+
return undefined
|
|
417
|
+
}
|
|
418
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
2
|
+
|
|
3
|
+
/** One field-level detail of a LINE business error (`details[].property`). */
|
|
4
|
+
export interface LineErrorDetail {
|
|
5
|
+
property?: string
|
|
6
|
+
message?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Fields a failed LINE API call carries for classification and logs. */
|
|
10
|
+
export interface LineApiErrorInit {
|
|
11
|
+
/** HTTP status, absent when the request never got an answer. */
|
|
12
|
+
status?: number
|
|
13
|
+
/** Text of the `message` / `error_description` field, whichever the answer carries. */
|
|
14
|
+
platformMessage?: string
|
|
15
|
+
/** The `error` field of a token or request-format refusal. */
|
|
16
|
+
error?: string
|
|
17
|
+
details?: readonly LineErrorDetail[]
|
|
18
|
+
/** Start of the answer body, for a refusal that carried no JSON. */
|
|
19
|
+
bodyText?: string
|
|
20
|
+
/** The answer body was not JSON, so the status alone does not explain the outcome. */
|
|
21
|
+
nonJson?: boolean
|
|
22
|
+
cause?: unknown
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One failed LINE Messaging API call.
|
|
27
|
+
*
|
|
28
|
+
* LINE answers a refusal in one of two JSON envelopes — `{ message, details[] }` for a business refusal and
|
|
29
|
+
* `{ error, error_description }` for a token or request-format refusal — or with a page no proxy should produce.
|
|
30
|
+
*/
|
|
31
|
+
export class LineApiError extends Error {
|
|
32
|
+
readonly status: number | undefined
|
|
33
|
+
readonly platformMessage: string | undefined
|
|
34
|
+
readonly error: string | undefined
|
|
35
|
+
readonly details: readonly LineErrorDetail[]
|
|
36
|
+
readonly bodyText: string | undefined
|
|
37
|
+
readonly nonJson: boolean
|
|
38
|
+
|
|
39
|
+
constructor(message: string, init: LineApiErrorInit = {}) {
|
|
40
|
+
super(message, init.cause === undefined ? undefined : { cause: init.cause })
|
|
41
|
+
this.name = 'LineApiError'
|
|
42
|
+
this.status = init.status
|
|
43
|
+
this.platformMessage = init.platformMessage
|
|
44
|
+
this.error = init.error
|
|
45
|
+
this.details = init.details ?? []
|
|
46
|
+
this.bodyText = init.bodyText
|
|
47
|
+
this.nonJson = init.nonJson ?? false
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether a caught value is an abort error, including the `DOMException` a signal produces. */
|
|
52
|
+
function isAbortError(error: unknown): error is Error | DOMException {
|
|
53
|
+
return (
|
|
54
|
+
(error instanceof Error || error instanceof DOMException) &&
|
|
55
|
+
error.name === 'AbortError'
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Map any failure onto the core platform taxonomy.
|
|
61
|
+
*
|
|
62
|
+
* A LINE refusal is a request-level answer: the platform rejects the whole request or sends all of it, so every 4xx
|
|
63
|
+
* means nothing was published. Transport failures, answers whose body is not JSON and 5xx leave the outcome unknown.
|
|
64
|
+
*
|
|
65
|
+
* @param error - the caught failure.
|
|
66
|
+
* @returns the classified platform error.
|
|
67
|
+
*/
|
|
68
|
+
export function classifyLineError(error: unknown): PlatformError {
|
|
69
|
+
if (error instanceof PlatformError) return error
|
|
70
|
+
if (error instanceof LineApiError) {
|
|
71
|
+
const status = error.status
|
|
72
|
+
const init = {
|
|
73
|
+
...(status === undefined ? {} : { code: status }),
|
|
74
|
+
cause: error,
|
|
75
|
+
}
|
|
76
|
+
// The answer body was not the platform's JSON envelope (a proxy page, a cut-off answer): the status alone cannot
|
|
77
|
+
// say whether the request was rejected or half-processed.
|
|
78
|
+
if (status === undefined || error.nonJson) {
|
|
79
|
+
return new PlatformError('retryable', error.message, {
|
|
80
|
+
...init,
|
|
81
|
+
published: 'unknown',
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
if (status === 429) {
|
|
85
|
+
return new PlatformError('rate-limit', error.message, {
|
|
86
|
+
...init,
|
|
87
|
+
published: 'no',
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
if (status === 401 || status === 403) {
|
|
91
|
+
return new PlatformError('permission', error.message, {
|
|
92
|
+
...init,
|
|
93
|
+
published: 'no',
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
if (status === 404 || status === 410) {
|
|
97
|
+
return new PlatformError('not-found', error.message, {
|
|
98
|
+
...init,
|
|
99
|
+
published: 'no',
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
if (status === 409) {
|
|
103
|
+
// Only a request carrying `X-Line-Retry-Key` answers 409, and it means the key was already accepted: the
|
|
104
|
+
// message may have been sent.
|
|
105
|
+
return new PlatformError('fatal', error.message, {
|
|
106
|
+
...init,
|
|
107
|
+
published: 'unknown',
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
if (status === 413) {
|
|
111
|
+
return new PlatformError('limit', error.message, {
|
|
112
|
+
...init,
|
|
113
|
+
published: 'no',
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
if (status >= 500) {
|
|
117
|
+
return new PlatformError('retryable', error.message, {
|
|
118
|
+
...init,
|
|
119
|
+
published: 'unknown',
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
return new PlatformError('invalid', error.message, {
|
|
123
|
+
...init,
|
|
124
|
+
published: 'no',
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
if (isAbortError(error)) {
|
|
128
|
+
return new PlatformError('fatal', error.message, {
|
|
129
|
+
published: 'unknown',
|
|
130
|
+
cause: error,
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
134
|
+
return new PlatformError('retryable', `line: request failed: ${message}`, {
|
|
135
|
+
published: 'unknown',
|
|
136
|
+
cause: error,
|
|
137
|
+
})
|
|
138
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { BotInfoResponse } from './types/index.ts'
|
|
2
|
+
|
|
3
|
+
/** Failure to confirm the configured bot identity. */
|
|
4
|
+
export class LineIdentityError extends Error {
|
|
5
|
+
constructor(message: string) {
|
|
6
|
+
super(message)
|
|
7
|
+
this.name = 'LineIdentityError'
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Verify a `getBotInfo` answer against the configured `selfId`.
|
|
13
|
+
*
|
|
14
|
+
* `bot.id` is fixed at construction, so the channel must be identified from configuration; this call proves the
|
|
15
|
+
* token belongs to that channel instead of discovering the id from it.
|
|
16
|
+
*
|
|
17
|
+
* @param selfId - the configured bot user id.
|
|
18
|
+
* @param info - the `GET /v2/bot/info` answer.
|
|
19
|
+
* @returns the platform user id, which is `selfId`.
|
|
20
|
+
*/
|
|
21
|
+
export function verifyIdentity(selfId: string, info: BotInfoResponse): string {
|
|
22
|
+
if (info.userId !== selfId) {
|
|
23
|
+
throw new LineIdentityError(
|
|
24
|
+
`line: selfId is ${selfId} but getBotInfo returned ${info.userId}`,
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
return info.userId
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as Line from './types/index.ts'
|
|
2
|
+
|
|
3
|
+
export { LineBot, LineBot as default, type LineModes } from './bot.ts'
|
|
4
|
+
export {
|
|
5
|
+
Config,
|
|
6
|
+
configError,
|
|
7
|
+
publicBase,
|
|
8
|
+
routePath,
|
|
9
|
+
type Config as LineConfig,
|
|
10
|
+
} from './config.ts'
|
|
11
|
+
export {
|
|
12
|
+
chatIdOf,
|
|
13
|
+
chatRefOf,
|
|
14
|
+
chatTargetOf,
|
|
15
|
+
decodeEvent,
|
|
16
|
+
parseChatId,
|
|
17
|
+
type LineChatAddress,
|
|
18
|
+
type LineChatKind,
|
|
19
|
+
type LineReferrer,
|
|
20
|
+
} from './decode.ts'
|
|
21
|
+
export {
|
|
22
|
+
classifyLineError,
|
|
23
|
+
LineApiError,
|
|
24
|
+
type LineApiErrorInit,
|
|
25
|
+
type LineErrorDetail,
|
|
26
|
+
} from './errors.ts'
|
|
27
|
+
export { LineIdentityError, verifyIdentity } from './identity.ts'
|
|
28
|
+
export { serveMessageContent } from './media.ts'
|
|
29
|
+
export type {
|
|
30
|
+
LineAction,
|
|
31
|
+
LineBlock,
|
|
32
|
+
LineDraft,
|
|
33
|
+
LineModeTypes,
|
|
34
|
+
LineNode,
|
|
35
|
+
LinePayload,
|
|
36
|
+
} from './modes/shared.ts'
|
|
37
|
+
export { LINE_LIMITS } from './modes/shared.ts'
|
|
38
|
+
export {
|
|
39
|
+
downloadContent,
|
|
40
|
+
invoke,
|
|
41
|
+
LINE_API_ORIGIN,
|
|
42
|
+
LINE_DATA_ORIGIN,
|
|
43
|
+
type LineRequest,
|
|
44
|
+
type LineRequestConfig,
|
|
45
|
+
} from './request.ts'
|
|
46
|
+
export { members, sendPayload, type LineSendDeps } from './send.ts'
|
|
47
|
+
export { matchesSignature, signatureOf } from './signature.ts'
|
|
48
|
+
export {
|
|
49
|
+
LineMediaRoute,
|
|
50
|
+
MediaStaging,
|
|
51
|
+
type StagedMedia,
|
|
52
|
+
} from './transport/media.ts'
|
|
53
|
+
export {
|
|
54
|
+
LineWebhook,
|
|
55
|
+
type WebHandlerRoute,
|
|
56
|
+
type WebResourceRoute,
|
|
57
|
+
type WebServerLike,
|
|
58
|
+
} from './transport/webhook.ts'
|
|
59
|
+
export { Internal } from './types/internal.ts'
|
|
60
|
+
export { Line }
|