@gakr-gakr/line 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/api.ts +11 -0
- package/autobot.plugin.json +15 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +5 -0
- package/index.ts +54 -0
- package/package.json +60 -0
- package/runtime-api.ts +182 -0
- package/secret-contract-api.ts +4 -0
- package/setup-api.ts +2 -0
- package/setup-entry.ts +9 -0
- package/src/account-helpers.ts +16 -0
- package/src/accounts.ts +187 -0
- package/src/actions.ts +61 -0
- package/src/auto-reply-delivery.ts +200 -0
- package/src/bindings.ts +65 -0
- package/src/bot-access.ts +30 -0
- package/src/bot-handlers.ts +620 -0
- package/src/bot-message-context.ts +586 -0
- package/src/bot.ts +70 -0
- package/src/card-command.ts +347 -0
- package/src/channel-access-token.ts +14 -0
- package/src/channel-api.ts +17 -0
- package/src/channel-shared.ts +48 -0
- package/src/channel.runtime.ts +3 -0
- package/src/channel.setup.ts +11 -0
- package/src/channel.ts +155 -0
- package/src/config-adapter.ts +29 -0
- package/src/config-schema.ts +81 -0
- package/src/download.ts +34 -0
- package/src/flex-templates/basic-cards.ts +395 -0
- package/src/flex-templates/common.ts +20 -0
- package/src/flex-templates/media-control-cards.ts +555 -0
- package/src/flex-templates/message.ts +13 -0
- package/src/flex-templates/schedule-cards.ts +467 -0
- package/src/flex-templates/types.ts +22 -0
- package/src/flex-templates.ts +32 -0
- package/src/gateway.ts +129 -0
- package/src/group-keys.ts +65 -0
- package/src/group-policy.ts +22 -0
- package/src/markdown-to-line.ts +416 -0
- package/src/monitor-durable.ts +37 -0
- package/src/monitor.runtime.ts +1 -0
- package/src/monitor.ts +507 -0
- package/src/outbound-media.ts +120 -0
- package/src/outbound.runtime.ts +12 -0
- package/src/outbound.ts +427 -0
- package/src/probe.runtime.ts +1 -0
- package/src/probe.ts +34 -0
- package/src/quick-reply-fallback.ts +10 -0
- package/src/reply-chunks.ts +110 -0
- package/src/reply-payload-transform.ts +317 -0
- package/src/rich-menu.ts +326 -0
- package/src/runtime.ts +32 -0
- package/src/send-receipt.ts +32 -0
- package/src/send.ts +531 -0
- package/src/setup-core.ts +149 -0
- package/src/setup-runtime-api.ts +9 -0
- package/src/setup-surface.ts +229 -0
- package/src/signature.ts +24 -0
- package/src/status.ts +37 -0
- package/src/template-messages.ts +333 -0
- package/src/types.ts +130 -0
- package/src/webhook-node.ts +155 -0
- package/src/webhook-utils.ts +10 -0
- package/src/webhook.ts +135 -0
- package/tsconfig.json +16 -0
package/src/outbound.ts
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineChannelMessageAdapter,
|
|
3
|
+
type ChannelMessageSendResult,
|
|
4
|
+
type MessageReceiptPartKind,
|
|
5
|
+
} from "autobot/plugin-sdk/channel-message";
|
|
6
|
+
import {
|
|
7
|
+
createAttachedChannelResultAdapter,
|
|
8
|
+
createEmptyChannelResult,
|
|
9
|
+
} from "autobot/plugin-sdk/channel-send-result";
|
|
10
|
+
import { createLazyRuntimeModule } from "autobot/plugin-sdk/lazy-runtime";
|
|
11
|
+
import { resolveOutboundMediaUrls } from "autobot/plugin-sdk/reply-payload";
|
|
12
|
+
import { type ChannelPlugin, type ResolvedLineAccount } from "./channel-api.js";
|
|
13
|
+
import { resolveLineOutboundMedia, type LineOutboundMediaResolved } from "./outbound-media.js";
|
|
14
|
+
import { buildLineQuickReplyFallbackText } from "./quick-reply-fallback.js";
|
|
15
|
+
import { getLineRuntime } from "./runtime.js";
|
|
16
|
+
import { createLineSendReceipt } from "./send-receipt.js";
|
|
17
|
+
import type { LineChannelData, LineSendResult } from "./types.js";
|
|
18
|
+
|
|
19
|
+
const loadLineOutboundRuntime = createLazyRuntimeModule(() => import("./outbound.runtime.js"));
|
|
20
|
+
|
|
21
|
+
type LineChannelDataWithMedia = LineChannelData & {
|
|
22
|
+
mediaKind?: "image" | "video" | "audio";
|
|
23
|
+
previewImageUrl?: string;
|
|
24
|
+
durationMs?: number;
|
|
25
|
+
trackingId?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function isLineUserTarget(target: string): boolean {
|
|
29
|
+
const normalized = target
|
|
30
|
+
.trim()
|
|
31
|
+
.replace(/^line:(group|room|user):/i, "")
|
|
32
|
+
.replace(/^line:/i, "");
|
|
33
|
+
return /^U/i.test(normalized);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hasLineSpecificMediaOptions(lineData: LineChannelDataWithMedia): boolean {
|
|
37
|
+
return Boolean(
|
|
38
|
+
lineData.mediaKind ??
|
|
39
|
+
lineData.previewImageUrl?.trim() ??
|
|
40
|
+
(typeof lineData.durationMs === "number" ? lineData.durationMs : undefined) ??
|
|
41
|
+
lineData.trackingId?.trim(),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function buildLineMediaMessageObject(
|
|
46
|
+
resolved: LineOutboundMediaResolved,
|
|
47
|
+
opts?: { allowTrackingId?: boolean },
|
|
48
|
+
): Record<string, unknown> {
|
|
49
|
+
switch (resolved.mediaKind) {
|
|
50
|
+
case "video": {
|
|
51
|
+
const previewImageUrl = resolved.previewImageUrl?.trim();
|
|
52
|
+
if (!previewImageUrl) {
|
|
53
|
+
throw new Error("LINE video messages require previewImageUrl to reference an image URL");
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
type: "video",
|
|
57
|
+
originalContentUrl: resolved.mediaUrl,
|
|
58
|
+
previewImageUrl,
|
|
59
|
+
...(opts?.allowTrackingId && resolved.trackingId
|
|
60
|
+
? { trackingId: resolved.trackingId }
|
|
61
|
+
: {}),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
case "audio":
|
|
65
|
+
return {
|
|
66
|
+
type: "audio",
|
|
67
|
+
originalContentUrl: resolved.mediaUrl,
|
|
68
|
+
duration: resolved.durationMs ?? 60000,
|
|
69
|
+
};
|
|
70
|
+
default:
|
|
71
|
+
return {
|
|
72
|
+
type: "image",
|
|
73
|
+
originalContentUrl: resolved.mediaUrl,
|
|
74
|
+
previewImageUrl: resolved.previewImageUrl ?? resolved.mediaUrl,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>["outbound"]> = {
|
|
80
|
+
deliveryMode: "direct",
|
|
81
|
+
chunker: (text, limit) => getLineRuntime().channel.text.chunkMarkdownText(text, limit),
|
|
82
|
+
textChunkLimit: 5000,
|
|
83
|
+
sendPayload: async ({ to, payload, accountId, cfg }) => {
|
|
84
|
+
const runtime = getLineRuntime();
|
|
85
|
+
const outboundRuntime = await loadLineOutboundRuntime();
|
|
86
|
+
const lineData = (payload.channelData?.line as LineChannelDataWithMedia | undefined) ?? {};
|
|
87
|
+
const lineRuntime = runtime.channel.line;
|
|
88
|
+
const sendText = lineRuntime?.pushMessageLine ?? outboundRuntime.pushMessageLine;
|
|
89
|
+
const sendBatch = lineRuntime?.pushMessagesLine ?? outboundRuntime.pushMessagesLine;
|
|
90
|
+
const sendFlex = lineRuntime?.pushFlexMessage ?? outboundRuntime.pushFlexMessage;
|
|
91
|
+
const sendTemplate = lineRuntime?.pushTemplateMessage ?? outboundRuntime.pushTemplateMessage;
|
|
92
|
+
const sendLocation = lineRuntime?.pushLocationMessage ?? outboundRuntime.pushLocationMessage;
|
|
93
|
+
const sendQuickReplies =
|
|
94
|
+
lineRuntime?.pushTextMessageWithQuickReplies ??
|
|
95
|
+
outboundRuntime.pushTextMessageWithQuickReplies;
|
|
96
|
+
const buildTemplate =
|
|
97
|
+
lineRuntime?.buildTemplateMessageFromPayload ??
|
|
98
|
+
outboundRuntime.buildTemplateMessageFromPayload;
|
|
99
|
+
|
|
100
|
+
let lastResult: LineSendResult | null = null;
|
|
101
|
+
const quickReplies = lineData.quickReplies ?? [];
|
|
102
|
+
const hasQuickReplies = quickReplies.length > 0;
|
|
103
|
+
const quickReply = hasQuickReplies
|
|
104
|
+
? (lineRuntime?.createQuickReplyItems ?? outboundRuntime.createQuickReplyItems)(quickReplies)
|
|
105
|
+
: undefined;
|
|
106
|
+
|
|
107
|
+
// LINE SDK expects Message[] but we build dynamically.
|
|
108
|
+
const sendMessageBatch = async (messages: Array<Record<string, unknown>>) => {
|
|
109
|
+
if (messages.length === 0) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
for (let i = 0; i < messages.length; i += 5) {
|
|
113
|
+
const batch = messages.slice(i, i + 5) as unknown as Parameters<typeof sendBatch>[1];
|
|
114
|
+
const result = await sendBatch(to, batch, {
|
|
115
|
+
verbose: false,
|
|
116
|
+
cfg,
|
|
117
|
+
accountId: accountId ?? undefined,
|
|
118
|
+
});
|
|
119
|
+
lastResult = result;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const processed = payload.text
|
|
124
|
+
? outboundRuntime.processLineMessage(payload.text)
|
|
125
|
+
: { text: "", flexMessages: [] };
|
|
126
|
+
|
|
127
|
+
const chunkLimit =
|
|
128
|
+
runtime.channel.text.resolveTextChunkLimit?.(cfg, "line", accountId ?? undefined, {
|
|
129
|
+
fallbackLimit: 5000,
|
|
130
|
+
}) ?? 5000;
|
|
131
|
+
|
|
132
|
+
const chunks = processed.text
|
|
133
|
+
? runtime.channel.text.chunkMarkdownText(processed.text, chunkLimit)
|
|
134
|
+
: [];
|
|
135
|
+
const mediaUrls = resolveOutboundMediaUrls(payload);
|
|
136
|
+
const useLineSpecificMedia = hasLineSpecificMediaOptions(lineData);
|
|
137
|
+
const shouldSendQuickRepliesInline = chunks.length === 0 && hasQuickReplies;
|
|
138
|
+
const sendMediaMessages = async () => {
|
|
139
|
+
for (const url of mediaUrls) {
|
|
140
|
+
const trimmed = url?.trim();
|
|
141
|
+
if (!trimmed) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (!useLineSpecificMedia) {
|
|
145
|
+
lastResult = await (lineRuntime?.sendMessageLine ?? outboundRuntime.sendMessageLine)(
|
|
146
|
+
to,
|
|
147
|
+
"",
|
|
148
|
+
{
|
|
149
|
+
verbose: false,
|
|
150
|
+
mediaUrl: trimmed,
|
|
151
|
+
cfg,
|
|
152
|
+
accountId: accountId ?? undefined,
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const resolved = await resolveLineOutboundMedia(trimmed, {
|
|
158
|
+
mediaKind: lineData.mediaKind,
|
|
159
|
+
previewImageUrl: lineData.previewImageUrl,
|
|
160
|
+
durationMs: lineData.durationMs,
|
|
161
|
+
trackingId: lineData.trackingId,
|
|
162
|
+
});
|
|
163
|
+
lastResult = await (lineRuntime?.sendMessageLine ?? outboundRuntime.sendMessageLine)(
|
|
164
|
+
to,
|
|
165
|
+
"",
|
|
166
|
+
{
|
|
167
|
+
verbose: false,
|
|
168
|
+
mediaUrl: resolved.mediaUrl,
|
|
169
|
+
mediaKind: resolved.mediaKind,
|
|
170
|
+
previewImageUrl: resolved.previewImageUrl,
|
|
171
|
+
durationMs: resolved.durationMs,
|
|
172
|
+
trackingId: resolved.trackingId,
|
|
173
|
+
cfg,
|
|
174
|
+
accountId: accountId ?? undefined,
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
if (!shouldSendQuickRepliesInline) {
|
|
181
|
+
if (lineData.flexMessage) {
|
|
182
|
+
const flexContents = lineData.flexMessage.contents as Parameters<typeof sendFlex>[2];
|
|
183
|
+
lastResult = await sendFlex(to, lineData.flexMessage.altText, flexContents, {
|
|
184
|
+
verbose: false,
|
|
185
|
+
cfg,
|
|
186
|
+
accountId: accountId ?? undefined,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (lineData.templateMessage) {
|
|
191
|
+
const template = buildTemplate(lineData.templateMessage);
|
|
192
|
+
if (template) {
|
|
193
|
+
lastResult = await sendTemplate(to, template, {
|
|
194
|
+
verbose: false,
|
|
195
|
+
cfg,
|
|
196
|
+
accountId: accountId ?? undefined,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (lineData.location) {
|
|
202
|
+
lastResult = await sendLocation(to, lineData.location, {
|
|
203
|
+
verbose: false,
|
|
204
|
+
cfg,
|
|
205
|
+
accountId: accountId ?? undefined,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (const flexMsg of processed.flexMessages) {
|
|
210
|
+
const flexContents = flexMsg.contents;
|
|
211
|
+
lastResult = await sendFlex(to, flexMsg.altText, flexContents, {
|
|
212
|
+
verbose: false,
|
|
213
|
+
cfg,
|
|
214
|
+
accountId: accountId ?? undefined,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const sendMediaAfterText = !(hasQuickReplies && chunks.length > 0);
|
|
220
|
+
if (mediaUrls.length > 0 && !shouldSendQuickRepliesInline && !sendMediaAfterText) {
|
|
221
|
+
await sendMediaMessages();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (chunks.length > 0) {
|
|
225
|
+
for (let i = 0; i < chunks.length; i += 1) {
|
|
226
|
+
const isLast = i === chunks.length - 1;
|
|
227
|
+
if (isLast && hasQuickReplies) {
|
|
228
|
+
lastResult = await sendQuickReplies(to, chunks[i], quickReplies, {
|
|
229
|
+
verbose: false,
|
|
230
|
+
cfg,
|
|
231
|
+
accountId: accountId ?? undefined,
|
|
232
|
+
});
|
|
233
|
+
} else {
|
|
234
|
+
lastResult = await sendText(to, chunks[i], {
|
|
235
|
+
verbose: false,
|
|
236
|
+
cfg,
|
|
237
|
+
accountId: accountId ?? undefined,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
} else if (shouldSendQuickRepliesInline) {
|
|
242
|
+
const quickReplyMessages: Array<Record<string, unknown>> = [];
|
|
243
|
+
if (lineData.flexMessage) {
|
|
244
|
+
quickReplyMessages.push({
|
|
245
|
+
type: "flex",
|
|
246
|
+
altText: lineData.flexMessage.altText.slice(0, 400),
|
|
247
|
+
contents: lineData.flexMessage.contents,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
if (lineData.templateMessage) {
|
|
251
|
+
const template = buildTemplate(lineData.templateMessage);
|
|
252
|
+
if (template) {
|
|
253
|
+
quickReplyMessages.push(template);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (lineData.location) {
|
|
257
|
+
quickReplyMessages.push({
|
|
258
|
+
type: "location",
|
|
259
|
+
title: lineData.location.title.slice(0, 100),
|
|
260
|
+
address: lineData.location.address.slice(0, 100),
|
|
261
|
+
latitude: lineData.location.latitude,
|
|
262
|
+
longitude: lineData.location.longitude,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
for (const flexMsg of processed.flexMessages) {
|
|
266
|
+
quickReplyMessages.push({
|
|
267
|
+
type: "flex",
|
|
268
|
+
altText: flexMsg.altText.slice(0, 400),
|
|
269
|
+
contents: flexMsg.contents,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
for (const url of mediaUrls) {
|
|
273
|
+
const trimmed = url?.trim();
|
|
274
|
+
if (!trimmed) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (!useLineSpecificMedia) {
|
|
278
|
+
quickReplyMessages.push({
|
|
279
|
+
type: "image",
|
|
280
|
+
originalContentUrl: trimmed,
|
|
281
|
+
previewImageUrl: trimmed,
|
|
282
|
+
});
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const resolved = await resolveLineOutboundMedia(trimmed, {
|
|
286
|
+
mediaKind: lineData.mediaKind,
|
|
287
|
+
previewImageUrl: lineData.previewImageUrl,
|
|
288
|
+
durationMs: lineData.durationMs,
|
|
289
|
+
trackingId: lineData.trackingId,
|
|
290
|
+
});
|
|
291
|
+
quickReplyMessages.push(
|
|
292
|
+
buildLineMediaMessageObject(resolved, { allowTrackingId: isLineUserTarget(to) }),
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
if (quickReplyMessages.length > 0 && quickReply) {
|
|
296
|
+
const lastIndex = quickReplyMessages.length - 1;
|
|
297
|
+
quickReplyMessages[lastIndex] = {
|
|
298
|
+
...quickReplyMessages[lastIndex],
|
|
299
|
+
quickReply,
|
|
300
|
+
};
|
|
301
|
+
await sendMessageBatch(quickReplyMessages);
|
|
302
|
+
} else if (quickReply) {
|
|
303
|
+
lastResult = await sendQuickReplies(
|
|
304
|
+
to,
|
|
305
|
+
buildLineQuickReplyFallbackText(quickReplies),
|
|
306
|
+
quickReplies,
|
|
307
|
+
{
|
|
308
|
+
verbose: false,
|
|
309
|
+
cfg,
|
|
310
|
+
accountId: accountId ?? undefined,
|
|
311
|
+
},
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (mediaUrls.length > 0 && !shouldSendQuickRepliesInline && sendMediaAfterText) {
|
|
317
|
+
await sendMediaMessages();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (lastResult) {
|
|
321
|
+
return createEmptyChannelResult("line", { ...lastResult });
|
|
322
|
+
}
|
|
323
|
+
return createEmptyChannelResult("line", { messageId: "empty", chatId: to });
|
|
324
|
+
},
|
|
325
|
+
...createAttachedChannelResultAdapter({
|
|
326
|
+
channel: "line",
|
|
327
|
+
sendText: async ({ cfg, to, text, accountId }) => {
|
|
328
|
+
const outboundRuntime = await loadLineOutboundRuntime();
|
|
329
|
+
const sendText = outboundRuntime.pushMessageLine;
|
|
330
|
+
const sendFlex = outboundRuntime.pushFlexMessage;
|
|
331
|
+
const processed = outboundRuntime.processLineMessage(text);
|
|
332
|
+
let result: LineSendResult;
|
|
333
|
+
if (processed.text.trim()) {
|
|
334
|
+
result = await sendText(to, processed.text, {
|
|
335
|
+
verbose: false,
|
|
336
|
+
cfg,
|
|
337
|
+
accountId: accountId ?? undefined,
|
|
338
|
+
});
|
|
339
|
+
} else {
|
|
340
|
+
result = {
|
|
341
|
+
messageId: "processed",
|
|
342
|
+
chatId: to,
|
|
343
|
+
receipt: createLineSendReceipt({ messageId: "processed", chatId: to, kind: "card" }),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
for (const flexMsg of processed.flexMessages) {
|
|
347
|
+
const flexContents = flexMsg.contents;
|
|
348
|
+
await sendFlex(to, flexMsg.altText, flexContents, {
|
|
349
|
+
verbose: false,
|
|
350
|
+
cfg,
|
|
351
|
+
accountId: accountId ?? undefined,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
return result;
|
|
355
|
+
},
|
|
356
|
+
sendMedia: async ({ cfg, to, text, mediaUrl, accountId }) =>
|
|
357
|
+
await (
|
|
358
|
+
await loadLineOutboundRuntime()
|
|
359
|
+
).sendMessageLine(to, text, {
|
|
360
|
+
verbose: false,
|
|
361
|
+
mediaUrl,
|
|
362
|
+
cfg,
|
|
363
|
+
accountId: accountId ?? undefined,
|
|
364
|
+
}),
|
|
365
|
+
}),
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
function toLineMessageSendResult(
|
|
369
|
+
result: Awaited<ReturnType<NonNullable<typeof lineOutboundAdapter.sendPayload>>>,
|
|
370
|
+
kind: MessageReceiptPartKind,
|
|
371
|
+
): ChannelMessageSendResult {
|
|
372
|
+
const source = result as typeof result & { chatId?: string };
|
|
373
|
+
const receipt =
|
|
374
|
+
result.receipt ??
|
|
375
|
+
(result.messageId
|
|
376
|
+
? createLineSendReceipt({
|
|
377
|
+
messageId: result.messageId,
|
|
378
|
+
chatId: source.chatId ?? "",
|
|
379
|
+
kind,
|
|
380
|
+
})
|
|
381
|
+
: undefined);
|
|
382
|
+
if (!receipt) {
|
|
383
|
+
throw new Error("LINE message adapter send did not return a receipt");
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
messageId: result.messageId || receipt.primaryPlatformMessageId,
|
|
387
|
+
receipt,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export const lineMessageAdapter = defineChannelMessageAdapter({
|
|
392
|
+
id: "line",
|
|
393
|
+
durableFinal: {
|
|
394
|
+
capabilities: {
|
|
395
|
+
text: true,
|
|
396
|
+
media: true,
|
|
397
|
+
messageSendingHooks: true,
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
send: {
|
|
401
|
+
text: async ({ cfg, to, text, accountId }) => {
|
|
402
|
+
const result = await lineOutboundAdapter.sendPayload!({
|
|
403
|
+
cfg,
|
|
404
|
+
to,
|
|
405
|
+
text,
|
|
406
|
+
accountId,
|
|
407
|
+
payload: { text },
|
|
408
|
+
});
|
|
409
|
+
return toLineMessageSendResult(result, "text");
|
|
410
|
+
},
|
|
411
|
+
media: async ({ cfg, to, text, mediaUrl, accountId }) => {
|
|
412
|
+
const result = await lineOutboundAdapter.sendPayload!({
|
|
413
|
+
cfg,
|
|
414
|
+
to,
|
|
415
|
+
text,
|
|
416
|
+
mediaUrl,
|
|
417
|
+
accountId,
|
|
418
|
+
payload: { text, mediaUrl },
|
|
419
|
+
});
|
|
420
|
+
return toLineMessageSendResult(result, "media");
|
|
421
|
+
},
|
|
422
|
+
},
|
|
423
|
+
receive: {
|
|
424
|
+
defaultAckPolicy: "after_receive_record",
|
|
425
|
+
supportedAckPolicies: ["after_receive_record"],
|
|
426
|
+
},
|
|
427
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { probeLineBot } from "./probe.js";
|
package/src/probe.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { messagingApi } from "@line/bot-sdk";
|
|
2
|
+
import { formatErrorMessage } from "autobot/plugin-sdk/error-runtime";
|
|
3
|
+
import { withTimeout } from "autobot/plugin-sdk/text-utility-runtime";
|
|
4
|
+
import type { LineProbeResult } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export async function probeLineBot(
|
|
7
|
+
channelAccessToken: string,
|
|
8
|
+
timeoutMs = 5000,
|
|
9
|
+
): Promise<LineProbeResult> {
|
|
10
|
+
if (!channelAccessToken?.trim()) {
|
|
11
|
+
return { ok: false, error: "Channel access token not configured" };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const client = new messagingApi.MessagingApiClient({
|
|
15
|
+
channelAccessToken: channelAccessToken.trim(),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const profile = await withTimeout(client.getBotInfo(), timeoutMs);
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
ok: true,
|
|
23
|
+
bot: {
|
|
24
|
+
displayName: profile.displayName,
|
|
25
|
+
userId: profile.userId,
|
|
26
|
+
basicId: profile.basicId,
|
|
27
|
+
pictureUrl: profile.pictureUrl,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
} catch (err) {
|
|
31
|
+
const message = formatErrorMessage(err);
|
|
32
|
+
return { ok: false, error: message };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function buildLineQuickReplyFallbackText(labels: readonly string[] | undefined): string {
|
|
2
|
+
const normalized = (labels ?? [])
|
|
3
|
+
.map((label) => label.trim())
|
|
4
|
+
.filter(Boolean)
|
|
5
|
+
.slice(0, 13);
|
|
6
|
+
if (normalized.length === 0) {
|
|
7
|
+
return "Choose an option.";
|
|
8
|
+
}
|
|
9
|
+
return `Options:\n${normalized.map((label) => `- ${label}`).join("\n")}`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { messagingApi } from "@line/bot-sdk";
|
|
2
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
3
|
+
|
|
4
|
+
type LineReplyMessage = messagingApi.TextMessage;
|
|
5
|
+
|
|
6
|
+
export type SendLineReplyChunksParams = {
|
|
7
|
+
to: string;
|
|
8
|
+
chunks: string[];
|
|
9
|
+
quickReplies?: string[];
|
|
10
|
+
replyToken?: string | null;
|
|
11
|
+
replyTokenUsed?: boolean;
|
|
12
|
+
cfg: AutoBotConfig;
|
|
13
|
+
accountId?: string;
|
|
14
|
+
replyMessageLine: (
|
|
15
|
+
replyToken: string,
|
|
16
|
+
messages: messagingApi.Message[],
|
|
17
|
+
opts: { cfg: AutoBotConfig; accountId?: string },
|
|
18
|
+
) => Promise<unknown>;
|
|
19
|
+
pushMessageLine: (
|
|
20
|
+
to: string,
|
|
21
|
+
text: string,
|
|
22
|
+
opts: { cfg: AutoBotConfig; accountId?: string },
|
|
23
|
+
) => Promise<unknown>;
|
|
24
|
+
pushTextMessageWithQuickReplies: (
|
|
25
|
+
to: string,
|
|
26
|
+
text: string,
|
|
27
|
+
quickReplies: string[],
|
|
28
|
+
opts: { cfg: AutoBotConfig; accountId?: string },
|
|
29
|
+
) => Promise<unknown>;
|
|
30
|
+
createTextMessageWithQuickReplies: (text: string, quickReplies: string[]) => LineReplyMessage;
|
|
31
|
+
onReplyError?: (err: unknown) => void;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export async function sendLineReplyChunks(
|
|
35
|
+
params: SendLineReplyChunksParams,
|
|
36
|
+
): Promise<{ replyTokenUsed: boolean }> {
|
|
37
|
+
const hasQuickReplies = Boolean(params.quickReplies?.length);
|
|
38
|
+
let replyTokenUsed = Boolean(params.replyTokenUsed);
|
|
39
|
+
|
|
40
|
+
if (params.chunks.length === 0) {
|
|
41
|
+
return { replyTokenUsed };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (params.replyToken && !replyTokenUsed) {
|
|
45
|
+
try {
|
|
46
|
+
const replyBatch = params.chunks.slice(0, 5);
|
|
47
|
+
const remaining = params.chunks.slice(replyBatch.length);
|
|
48
|
+
|
|
49
|
+
const replyMessages: LineReplyMessage[] = replyBatch.map((chunk) => ({
|
|
50
|
+
type: "text",
|
|
51
|
+
text: chunk,
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
if (hasQuickReplies && remaining.length === 0 && replyMessages.length > 0) {
|
|
55
|
+
const lastIndex = replyMessages.length - 1;
|
|
56
|
+
replyMessages[lastIndex] = params.createTextMessageWithQuickReplies(
|
|
57
|
+
replyBatch[lastIndex],
|
|
58
|
+
params.quickReplies!,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
await params.replyMessageLine(params.replyToken, replyMessages, {
|
|
63
|
+
cfg: params.cfg,
|
|
64
|
+
accountId: params.accountId,
|
|
65
|
+
});
|
|
66
|
+
replyTokenUsed = true;
|
|
67
|
+
|
|
68
|
+
for (let i = 0; i < remaining.length; i += 1) {
|
|
69
|
+
const isLastChunk = i === remaining.length - 1;
|
|
70
|
+
if (isLastChunk && hasQuickReplies) {
|
|
71
|
+
await params.pushTextMessageWithQuickReplies(
|
|
72
|
+
params.to,
|
|
73
|
+
remaining[i],
|
|
74
|
+
params.quickReplies!,
|
|
75
|
+
{ cfg: params.cfg, accountId: params.accountId },
|
|
76
|
+
);
|
|
77
|
+
} else {
|
|
78
|
+
await params.pushMessageLine(params.to, remaining[i], {
|
|
79
|
+
cfg: params.cfg,
|
|
80
|
+
accountId: params.accountId,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { replyTokenUsed };
|
|
86
|
+
} catch (err) {
|
|
87
|
+
params.onReplyError?.(err);
|
|
88
|
+
replyTokenUsed = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < params.chunks.length; i += 1) {
|
|
93
|
+
const isLastChunk = i === params.chunks.length - 1;
|
|
94
|
+
if (isLastChunk && hasQuickReplies) {
|
|
95
|
+
await params.pushTextMessageWithQuickReplies(
|
|
96
|
+
params.to,
|
|
97
|
+
params.chunks[i],
|
|
98
|
+
params.quickReplies!,
|
|
99
|
+
{ cfg: params.cfg, accountId: params.accountId },
|
|
100
|
+
);
|
|
101
|
+
} else {
|
|
102
|
+
await params.pushMessageLine(params.to, params.chunks[i], {
|
|
103
|
+
cfg: params.cfg,
|
|
104
|
+
accountId: params.accountId,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { replyTokenUsed };
|
|
110
|
+
}
|