@kodelyth/line 2026.5.39 → 2026.5.42
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/channel-plugin-api.ts +1 -0
- package/contract-api.ts +5 -0
- package/dist/accounts-CD4A1FE7.js +105 -0
- package/dist/api.js +11 -0
- package/dist/basic-cards-BISytiSa.js +307 -0
- package/dist/card-command-dQBX3fVN.js +240 -0
- package/dist/channel-DV5h44-j.js +649 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-Cc-v3szZ.js +4 -0
- package/dist/contract-api.js +2 -0
- package/dist/index.js +45 -0
- package/dist/markdown-to-line-CC3BU6CC.js +810 -0
- package/dist/monitor-Ci8Hg8ay.js +1485 -0
- package/dist/monitor.runtime-t6-QvlDB.js +2 -0
- package/dist/outbound.runtime-D1CxEvcL.js +2 -0
- package/dist/probe-BPSs_A_8.js +30 -0
- package/dist/probe.runtime-7u2o9QN5.js +2 -0
- package/dist/reply-payload-transform-CDuBzoT4.js +855 -0
- package/dist/runtime-api.js +291 -0
- package/dist/schedule-cards-D-yZMHDE.js +359 -0
- package/dist/secret-contract-api.js +5 -0
- package/dist/setup-api.js +2 -0
- package/dist/setup-entry.js +11 -0
- package/dist/setup-surface-CHfQ6Z4i.js +282 -0
- package/index.ts +53 -0
- package/klaw.plugin.json +2 -329
- package/package.json +4 -4
- package/runtime-api.ts +179 -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.test.ts +288 -0
- package/src/accounts.ts +187 -0
- package/src/actions.ts +61 -0
- package/src/auto-reply-delivery.test.ts +253 -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.test.ts +1094 -0
- package/src/bot-handlers.ts +620 -0
- package/src/bot-message-context.test.ts +420 -0
- package/src/bot-message-context.ts +586 -0
- package/src/bot.ts +66 -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-setup-status.contract.test.ts +70 -0
- package/src/channel-shared.ts +48 -0
- package/src/channel.logout.test.ts +145 -0
- package/src/channel.runtime.ts +3 -0
- package/src/channel.sendPayload.test.ts +659 -0
- package/src/channel.setup.ts +11 -0
- package/src/channel.status.test.ts +63 -0
- package/src/channel.ts +155 -0
- package/src/config-adapter.ts +29 -0
- package/src/config-schema.test.ts +53 -0
- package/src/config-schema.ts +81 -0
- package/src/download.test.ts +164 -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.test.ts +123 -0
- package/src/group-keys.ts +65 -0
- package/src/group-policy.ts +22 -0
- package/src/markdown-to-line.test.ts +348 -0
- package/src/markdown-to-line.ts +416 -0
- package/src/message-cards.test.ts +204 -0
- package/src/monitor-durable.test.ts +57 -0
- package/src/monitor-durable.ts +37 -0
- package/src/monitor.lifecycle.test.ts +499 -0
- package/src/monitor.runtime.ts +1 -0
- package/src/monitor.ts +507 -0
- package/src/outbound-media.test.ts +194 -0
- package/src/outbound-media.ts +120 -0
- package/src/outbound.runtime.ts +12 -0
- package/src/outbound.ts +427 -0
- package/src/probe.contract.test.ts +9 -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.test.ts +180 -0
- package/src/reply-chunks.ts +110 -0
- package/src/reply-payload-transform.test.ts +392 -0
- package/src/reply-payload-transform.ts +317 -0
- package/src/rich-menu.test.ts +315 -0
- package/src/rich-menu.ts +326 -0
- package/src/runtime.ts +32 -0
- package/src/send-receipt.ts +32 -0
- package/src/send.test.ts +453 -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.test.ts +481 -0
- package/src/setup-surface.ts +229 -0
- package/src/signature.test.ts +34 -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.test.ts +598 -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/api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-api.js +0 -7
- package/setup-entry.js +0 -7
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { resolvePinnedHostnameWithPolicy, type SsrFPolicy } from "klaw/plugin-sdk/ssrf-runtime";
|
|
2
|
+
import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
3
|
+
|
|
4
|
+
type LineOutboundMediaKind = "image" | "video" | "audio";
|
|
5
|
+
|
|
6
|
+
export type LineOutboundMediaResolved = {
|
|
7
|
+
mediaUrl: string;
|
|
8
|
+
mediaKind: LineOutboundMediaKind;
|
|
9
|
+
previewImageUrl?: string;
|
|
10
|
+
durationMs?: number;
|
|
11
|
+
trackingId?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type ResolveLineOutboundMediaOpts = {
|
|
15
|
+
mediaKind?: LineOutboundMediaKind;
|
|
16
|
+
previewImageUrl?: string;
|
|
17
|
+
durationMs?: number;
|
|
18
|
+
trackingId?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const LINE_OUTBOUND_MEDIA_SSRF_POLICY: SsrFPolicy = {
|
|
22
|
+
allowPrivateNetwork: false,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export async function validateLineMediaUrl(url: string): Promise<void> {
|
|
26
|
+
let parsed: URL;
|
|
27
|
+
try {
|
|
28
|
+
parsed = new URL(url);
|
|
29
|
+
} catch {
|
|
30
|
+
throw new Error(`LINE outbound media URL must be a valid URL: ${url}`);
|
|
31
|
+
}
|
|
32
|
+
if (parsed.protocol !== "https:") {
|
|
33
|
+
throw new Error(`LINE outbound media URL must use HTTPS: ${url}`);
|
|
34
|
+
}
|
|
35
|
+
if (url.length > 2000) {
|
|
36
|
+
throw new Error(`LINE outbound media URL must be 2000 chars or less (got ${url.length})`);
|
|
37
|
+
}
|
|
38
|
+
await resolvePinnedHostnameWithPolicy(parsed.hostname, {
|
|
39
|
+
policy: LINE_OUTBOUND_MEDIA_SSRF_POLICY,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function detectLineMediaKind(mimeType: string): LineOutboundMediaKind {
|
|
44
|
+
const normalized = normalizeLowercaseStringOrEmpty(mimeType);
|
|
45
|
+
if (normalized.startsWith("image/")) {
|
|
46
|
+
return "image";
|
|
47
|
+
}
|
|
48
|
+
if (normalized.startsWith("video/")) {
|
|
49
|
+
return "video";
|
|
50
|
+
}
|
|
51
|
+
if (normalized.startsWith("audio/")) {
|
|
52
|
+
return "audio";
|
|
53
|
+
}
|
|
54
|
+
return "image";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isHttpsUrl(url: string): boolean {
|
|
58
|
+
try {
|
|
59
|
+
return new URL(url).protocol === "https:";
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function detectLineMediaKindFromUrl(url: string): LineOutboundMediaKind | undefined {
|
|
66
|
+
try {
|
|
67
|
+
const pathname = normalizeLowercaseStringOrEmpty(new URL(url).pathname);
|
|
68
|
+
if (/\.(png|jpe?g|gif|webp|bmp|heic|heif|avif)$/i.test(pathname)) {
|
|
69
|
+
return "image";
|
|
70
|
+
}
|
|
71
|
+
if (/\.(mp4|mov|m4v|webm)$/i.test(pathname)) {
|
|
72
|
+
return "video";
|
|
73
|
+
}
|
|
74
|
+
if (/\.(mp3|m4a|aac|wav|ogg|oga)$/i.test(pathname)) {
|
|
75
|
+
return "audio";
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function resolveLineOutboundMedia(
|
|
84
|
+
mediaUrl: string,
|
|
85
|
+
opts: ResolveLineOutboundMediaOpts = {},
|
|
86
|
+
): Promise<LineOutboundMediaResolved> {
|
|
87
|
+
const trimmedUrl = mediaUrl.trim();
|
|
88
|
+
if (isHttpsUrl(trimmedUrl)) {
|
|
89
|
+
await validateLineMediaUrl(trimmedUrl);
|
|
90
|
+
const previewImageUrl = opts.previewImageUrl?.trim();
|
|
91
|
+
if (previewImageUrl) {
|
|
92
|
+
await validateLineMediaUrl(previewImageUrl);
|
|
93
|
+
}
|
|
94
|
+
const mediaKind =
|
|
95
|
+
opts.mediaKind ??
|
|
96
|
+
(typeof opts.durationMs === "number" ? "audio" : undefined) ??
|
|
97
|
+
(opts.trackingId?.trim() ? "video" : undefined) ??
|
|
98
|
+
detectLineMediaKindFromUrl(trimmedUrl) ??
|
|
99
|
+
"image";
|
|
100
|
+
return {
|
|
101
|
+
mediaUrl: trimmedUrl,
|
|
102
|
+
mediaKind,
|
|
103
|
+
...(previewImageUrl ? { previewImageUrl } : {}),
|
|
104
|
+
...(typeof opts.durationMs === "number" ? { durationMs: opts.durationMs } : {}),
|
|
105
|
+
...(opts.trackingId ? { trackingId: opts.trackingId } : {}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const parsed = new URL(trimmedUrl);
|
|
111
|
+
if (parsed.protocol !== "https:") {
|
|
112
|
+
throw new Error(`LINE outbound media URL must use HTTPS: ${trimmedUrl}`);
|
|
113
|
+
}
|
|
114
|
+
} catch (e) {
|
|
115
|
+
if (e instanceof Error && e.message.startsWith("LINE outbound")) {
|
|
116
|
+
throw e;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
throw new Error("LINE outbound media currently requires a public HTTPS URL");
|
|
120
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { processLineMessage } from "./markdown-to-line.js";
|
|
2
|
+
export {
|
|
3
|
+
createQuickReplyItems,
|
|
4
|
+
pushFlexMessage,
|
|
5
|
+
pushLocationMessage,
|
|
6
|
+
pushMessageLine,
|
|
7
|
+
pushMessagesLine,
|
|
8
|
+
pushTemplateMessage,
|
|
9
|
+
pushTextMessageWithQuickReplies,
|
|
10
|
+
sendMessageLine,
|
|
11
|
+
} from "./send.js";
|
|
12
|
+
export { buildTemplateMessageFromPayload } from "./template-messages.js";
|
package/src/outbound.ts
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineChannelMessageAdapter,
|
|
3
|
+
type ChannelMessageSendResult,
|
|
4
|
+
type MessageReceiptPartKind,
|
|
5
|
+
} from "klaw/plugin-sdk/channel-message";
|
|
6
|
+
import {
|
|
7
|
+
createAttachedChannelResultAdapter,
|
|
8
|
+
createEmptyChannelResult,
|
|
9
|
+
} from "klaw/plugin-sdk/channel-send-result";
|
|
10
|
+
import { createLazyRuntimeModule } from "klaw/plugin-sdk/lazy-runtime";
|
|
11
|
+
import { resolveOutboundMediaUrls } from "klaw/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,9 @@
|
|
|
1
|
+
import type { BaseProbeResult } from "klaw/plugin-sdk/channel-contract";
|
|
2
|
+
import { describe, expectTypeOf, it } from "vitest";
|
|
3
|
+
import type { LineProbeResult } from "./types.js";
|
|
4
|
+
|
|
5
|
+
describe("LINE probe contract", () => {
|
|
6
|
+
it("keeps public probe aligned with base contract", () => {
|
|
7
|
+
expectTypeOf<LineProbeResult>().toMatchTypeOf<BaseProbeResult>();
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -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 "klaw/plugin-sdk/error-runtime";
|
|
3
|
+
import { withTimeout } from "klaw/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
|
+
}
|