@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
package/src/actions.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { messagingApi } from "@line/bot-sdk";
|
|
2
|
+
|
|
3
|
+
export type Action = messagingApi.Action;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create a message action (sends text when tapped)
|
|
7
|
+
*/
|
|
8
|
+
export function messageAction(label: string, text?: string): Action {
|
|
9
|
+
return {
|
|
10
|
+
type: "message",
|
|
11
|
+
label: label.slice(0, 20),
|
|
12
|
+
text: text ?? label,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Create a URI action (opens a URL when tapped)
|
|
18
|
+
*/
|
|
19
|
+
export function uriAction(label: string, uri: string): Action {
|
|
20
|
+
return {
|
|
21
|
+
type: "uri",
|
|
22
|
+
label: label.slice(0, 20),
|
|
23
|
+
uri,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a postback action (sends data to webhook when tapped)
|
|
29
|
+
*/
|
|
30
|
+
export function postbackAction(label: string, data: string, displayText?: string): Action {
|
|
31
|
+
return {
|
|
32
|
+
type: "postback",
|
|
33
|
+
label: label.slice(0, 20),
|
|
34
|
+
data: data.slice(0, 300),
|
|
35
|
+
displayText: displayText?.slice(0, 300),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a datetime picker action
|
|
41
|
+
*/
|
|
42
|
+
export function datetimePickerAction(
|
|
43
|
+
label: string,
|
|
44
|
+
data: string,
|
|
45
|
+
mode: "date" | "time" | "datetime",
|
|
46
|
+
options?: {
|
|
47
|
+
initial?: string;
|
|
48
|
+
max?: string;
|
|
49
|
+
min?: string;
|
|
50
|
+
},
|
|
51
|
+
): Action {
|
|
52
|
+
return {
|
|
53
|
+
type: "datetimepicker",
|
|
54
|
+
label: label.slice(0, 20),
|
|
55
|
+
data: data.slice(0, 300),
|
|
56
|
+
mode,
|
|
57
|
+
initial: options?.initial,
|
|
58
|
+
max: options?.max,
|
|
59
|
+
min: options?.min,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { LineAutoReplyDeps } from "./auto-reply-delivery.js";
|
|
3
|
+
import { deliverLineAutoReply } from "./auto-reply-delivery.js";
|
|
4
|
+
import { sendLineReplyChunks } from "./reply-chunks.js";
|
|
5
|
+
import { createLineSendReceipt } from "./send-receipt.js";
|
|
6
|
+
|
|
7
|
+
const createFlexMessage = (altText: string, contents: unknown) => ({
|
|
8
|
+
type: "flex" as const,
|
|
9
|
+
altText,
|
|
10
|
+
contents,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const createImageMessage = (url: string) => ({
|
|
14
|
+
type: "image" as const,
|
|
15
|
+
originalContentUrl: url,
|
|
16
|
+
previewImageUrl: url,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const createLocationMessage = (location: {
|
|
20
|
+
title: string;
|
|
21
|
+
address: string;
|
|
22
|
+
latitude: number;
|
|
23
|
+
longitude: number;
|
|
24
|
+
}) => ({
|
|
25
|
+
type: "location" as const,
|
|
26
|
+
...location,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("deliverLineAutoReply", () => {
|
|
30
|
+
const LINE_TEST_CFG = { channels: { line: { accounts: { acc: {} } } } };
|
|
31
|
+
const baseDeliveryParams = {
|
|
32
|
+
cfg: LINE_TEST_CFG,
|
|
33
|
+
to: "line:user:1",
|
|
34
|
+
replyToken: "token",
|
|
35
|
+
replyTokenUsed: false,
|
|
36
|
+
accountId: "acc",
|
|
37
|
+
textLimit: 5000,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function createDeps(overrides?: Partial<LineAutoReplyDeps>) {
|
|
41
|
+
const replyMessageLine = vi.fn(async () => ({}));
|
|
42
|
+
const pushMessageLine = vi.fn(async () => ({}));
|
|
43
|
+
const pushTextMessageWithQuickReplies = vi.fn(async () => ({}));
|
|
44
|
+
const createTextMessageWithQuickReplies = vi.fn((text: string) => ({
|
|
45
|
+
type: "text" as const,
|
|
46
|
+
text,
|
|
47
|
+
}));
|
|
48
|
+
const createQuickReplyItems = vi.fn((labels: string[]) => ({ items: labels }));
|
|
49
|
+
const pushMessagesLine = vi.fn(async () => ({
|
|
50
|
+
messageId: "push",
|
|
51
|
+
chatId: "u1",
|
|
52
|
+
receipt: createLineSendReceipt({ messageId: "push", chatId: "u1", kind: "text" }),
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
const deps: LineAutoReplyDeps = {
|
|
56
|
+
buildTemplateMessageFromPayload: () => null,
|
|
57
|
+
processLineMessage: (text) => ({ text, flexMessages: [] }),
|
|
58
|
+
chunkMarkdownText: (text) => [text],
|
|
59
|
+
sendLineReplyChunks,
|
|
60
|
+
replyMessageLine,
|
|
61
|
+
pushMessageLine,
|
|
62
|
+
pushTextMessageWithQuickReplies,
|
|
63
|
+
createTextMessageWithQuickReplies,
|
|
64
|
+
createQuickReplyItems: createQuickReplyItems as LineAutoReplyDeps["createQuickReplyItems"],
|
|
65
|
+
pushMessagesLine,
|
|
66
|
+
createFlexMessage: createFlexMessage as LineAutoReplyDeps["createFlexMessage"],
|
|
67
|
+
createImageMessage,
|
|
68
|
+
createLocationMessage,
|
|
69
|
+
...overrides,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
deps,
|
|
74
|
+
replyMessageLine,
|
|
75
|
+
pushMessageLine,
|
|
76
|
+
pushTextMessageWithQuickReplies,
|
|
77
|
+
createTextMessageWithQuickReplies,
|
|
78
|
+
createQuickReplyItems,
|
|
79
|
+
pushMessagesLine,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
it("uses reply token for text before sending rich messages", async () => {
|
|
84
|
+
const lineData = {
|
|
85
|
+
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
86
|
+
};
|
|
87
|
+
const { deps, replyMessageLine, pushMessagesLine, createQuickReplyItems } = createDeps();
|
|
88
|
+
|
|
89
|
+
const result = await deliverLineAutoReply({
|
|
90
|
+
...baseDeliveryParams,
|
|
91
|
+
payload: { text: "hello", channelData: { line: lineData } },
|
|
92
|
+
lineData,
|
|
93
|
+
deps,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(result.replyTokenUsed).toBe(true);
|
|
97
|
+
expect(replyMessageLine).toHaveBeenCalledTimes(1);
|
|
98
|
+
expect(replyMessageLine).toHaveBeenCalledWith("token", [{ type: "text", text: "hello" }], {
|
|
99
|
+
cfg: LINE_TEST_CFG,
|
|
100
|
+
accountId: "acc",
|
|
101
|
+
});
|
|
102
|
+
expect(pushMessagesLine).toHaveBeenCalledTimes(1);
|
|
103
|
+
expect(pushMessagesLine).toHaveBeenCalledWith(
|
|
104
|
+
"line:user:1",
|
|
105
|
+
[createFlexMessage("Card", { type: "bubble" })],
|
|
106
|
+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
107
|
+
);
|
|
108
|
+
expect(createQuickReplyItems).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("uses reply token for rich-only payloads", async () => {
|
|
112
|
+
const lineData = {
|
|
113
|
+
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
114
|
+
quickReplies: ["A"],
|
|
115
|
+
};
|
|
116
|
+
const { deps, replyMessageLine, pushMessagesLine, createQuickReplyItems } = createDeps({
|
|
117
|
+
processLineMessage: () => ({ text: "", flexMessages: [] }),
|
|
118
|
+
chunkMarkdownText: () => [],
|
|
119
|
+
sendLineReplyChunks: vi.fn(async () => ({ replyTokenUsed: false })),
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const result = await deliverLineAutoReply({
|
|
123
|
+
...baseDeliveryParams,
|
|
124
|
+
payload: { channelData: { line: lineData } },
|
|
125
|
+
lineData,
|
|
126
|
+
deps,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(result.replyTokenUsed).toBe(true);
|
|
130
|
+
expect(replyMessageLine).toHaveBeenCalledTimes(1);
|
|
131
|
+
expect(replyMessageLine).toHaveBeenCalledWith(
|
|
132
|
+
"token",
|
|
133
|
+
[
|
|
134
|
+
{
|
|
135
|
+
...createFlexMessage("Card", { type: "bubble" }),
|
|
136
|
+
quickReply: { items: ["A"] },
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
140
|
+
);
|
|
141
|
+
expect(pushMessagesLine).not.toHaveBeenCalled();
|
|
142
|
+
expect(createQuickReplyItems).toHaveBeenCalledWith(["A"]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("uses fallback text for quick-reply-only payloads", async () => {
|
|
146
|
+
const createTextMessageWithQuickReplies = vi.fn((text: string, _quickReplies: string[]) => ({
|
|
147
|
+
type: "text" as const,
|
|
148
|
+
text,
|
|
149
|
+
quickReply: { items: ["A", "B"] },
|
|
150
|
+
}));
|
|
151
|
+
const lineData = {
|
|
152
|
+
quickReplies: ["A", "B"],
|
|
153
|
+
};
|
|
154
|
+
const { deps, replyMessageLine, pushMessagesLine } = createDeps({
|
|
155
|
+
createTextMessageWithQuickReplies:
|
|
156
|
+
createTextMessageWithQuickReplies as LineAutoReplyDeps["createTextMessageWithQuickReplies"],
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const result = await deliverLineAutoReply({
|
|
160
|
+
...baseDeliveryParams,
|
|
161
|
+
payload: { text: "", channelData: { line: lineData } },
|
|
162
|
+
lineData,
|
|
163
|
+
deps,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
expect(result.replyTokenUsed).toBe(true);
|
|
167
|
+
expect(replyMessageLine).toHaveBeenCalledWith(
|
|
168
|
+
"token",
|
|
169
|
+
[
|
|
170
|
+
{
|
|
171
|
+
type: "text",
|
|
172
|
+
text: "Options:\n- A\n- B",
|
|
173
|
+
quickReply: { items: ["A", "B"] },
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
177
|
+
);
|
|
178
|
+
expect(pushMessagesLine).not.toHaveBeenCalled();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("sends rich messages before quick-reply text so quick replies remain visible", async () => {
|
|
182
|
+
const createTextMessageWithQuickReplies = vi.fn((text: string, _quickReplies: string[]) => ({
|
|
183
|
+
type: "text" as const,
|
|
184
|
+
text,
|
|
185
|
+
quickReply: { items: ["A"] },
|
|
186
|
+
}));
|
|
187
|
+
|
|
188
|
+
const lineData = {
|
|
189
|
+
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
190
|
+
quickReplies: ["A"],
|
|
191
|
+
};
|
|
192
|
+
const { deps, pushMessagesLine, replyMessageLine } = createDeps({
|
|
193
|
+
createTextMessageWithQuickReplies:
|
|
194
|
+
createTextMessageWithQuickReplies as LineAutoReplyDeps["createTextMessageWithQuickReplies"],
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
await deliverLineAutoReply({
|
|
198
|
+
...baseDeliveryParams,
|
|
199
|
+
payload: { text: "hello", channelData: { line: lineData } },
|
|
200
|
+
lineData,
|
|
201
|
+
deps,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
expect(pushMessagesLine).toHaveBeenCalledWith(
|
|
205
|
+
"line:user:1",
|
|
206
|
+
[createFlexMessage("Card", { type: "bubble" })],
|
|
207
|
+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
208
|
+
);
|
|
209
|
+
expect(replyMessageLine).toHaveBeenCalledWith(
|
|
210
|
+
"token",
|
|
211
|
+
[
|
|
212
|
+
{
|
|
213
|
+
type: "text",
|
|
214
|
+
text: "hello",
|
|
215
|
+
quickReply: { items: ["A"] },
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
219
|
+
);
|
|
220
|
+
const pushOrder = pushMessagesLine.mock.invocationCallOrder[0];
|
|
221
|
+
const replyOrder = replyMessageLine.mock.invocationCallOrder[0];
|
|
222
|
+
expect(pushOrder).toBeLessThan(replyOrder);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("falls back to push when reply token delivery fails", async () => {
|
|
226
|
+
const lineData = {
|
|
227
|
+
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
228
|
+
};
|
|
229
|
+
const failingReplyMessageLine = vi.fn(async () => {
|
|
230
|
+
throw new Error("reply failed");
|
|
231
|
+
});
|
|
232
|
+
const { deps, pushMessagesLine } = createDeps({
|
|
233
|
+
processLineMessage: () => ({ text: "", flexMessages: [] }),
|
|
234
|
+
chunkMarkdownText: () => [],
|
|
235
|
+
replyMessageLine: failingReplyMessageLine as LineAutoReplyDeps["replyMessageLine"],
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const result = await deliverLineAutoReply({
|
|
239
|
+
...baseDeliveryParams,
|
|
240
|
+
payload: { channelData: { line: lineData } },
|
|
241
|
+
lineData,
|
|
242
|
+
deps,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(result.replyTokenUsed).toBe(true);
|
|
246
|
+
expect(failingReplyMessageLine).toHaveBeenCalledTimes(1);
|
|
247
|
+
expect(pushMessagesLine).toHaveBeenCalledWith(
|
|
248
|
+
"line:user:1",
|
|
249
|
+
[createFlexMessage("Card", { type: "bubble" })],
|
|
250
|
+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
251
|
+
);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { messagingApi } from "@line/bot-sdk";
|
|
2
|
+
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
3
|
+
import { resolveSendableOutboundReplyParts } from "klaw/plugin-sdk/reply-payload";
|
|
4
|
+
import type { ReplyPayload } from "klaw/plugin-sdk/reply-runtime";
|
|
5
|
+
import type { FlexContainer } from "./flex-templates.js";
|
|
6
|
+
import type { ProcessedLineMessage } from "./markdown-to-line.js";
|
|
7
|
+
import { buildLineQuickReplyFallbackText } from "./quick-reply-fallback.js";
|
|
8
|
+
import type { SendLineReplyChunksParams } from "./reply-chunks.js";
|
|
9
|
+
import type { LineChannelData, LineTemplateMessagePayload } from "./types.js";
|
|
10
|
+
|
|
11
|
+
export type LineAutoReplyDeps = {
|
|
12
|
+
buildTemplateMessageFromPayload: (
|
|
13
|
+
payload: LineTemplateMessagePayload,
|
|
14
|
+
) => messagingApi.TemplateMessage | null;
|
|
15
|
+
processLineMessage: (text: string) => ProcessedLineMessage;
|
|
16
|
+
chunkMarkdownText: (text: string, limit: number) => string[];
|
|
17
|
+
sendLineReplyChunks: (params: SendLineReplyChunksParams) => Promise<{ replyTokenUsed: boolean }>;
|
|
18
|
+
createQuickReplyItems: (labels: string[]) => messagingApi.QuickReply;
|
|
19
|
+
pushMessagesLine: (
|
|
20
|
+
to: string,
|
|
21
|
+
messages: messagingApi.Message[],
|
|
22
|
+
opts: { cfg: KlawConfig; accountId?: string },
|
|
23
|
+
) => Promise<unknown>;
|
|
24
|
+
createFlexMessage: (altText: string, contents: FlexContainer) => messagingApi.FlexMessage;
|
|
25
|
+
createImageMessage: (
|
|
26
|
+
originalContentUrl: string,
|
|
27
|
+
previewImageUrl?: string,
|
|
28
|
+
) => messagingApi.ImageMessage;
|
|
29
|
+
createLocationMessage: (location: {
|
|
30
|
+
title: string;
|
|
31
|
+
address: string;
|
|
32
|
+
latitude: number;
|
|
33
|
+
longitude: number;
|
|
34
|
+
}) => messagingApi.LocationMessage;
|
|
35
|
+
} & Pick<
|
|
36
|
+
SendLineReplyChunksParams,
|
|
37
|
+
| "replyMessageLine"
|
|
38
|
+
| "pushMessageLine"
|
|
39
|
+
| "pushTextMessageWithQuickReplies"
|
|
40
|
+
| "createTextMessageWithQuickReplies"
|
|
41
|
+
| "onReplyError"
|
|
42
|
+
>;
|
|
43
|
+
|
|
44
|
+
export async function deliverLineAutoReply(params: {
|
|
45
|
+
payload: ReplyPayload;
|
|
46
|
+
lineData: LineChannelData;
|
|
47
|
+
to: string;
|
|
48
|
+
replyToken?: string | null;
|
|
49
|
+
replyTokenUsed: boolean;
|
|
50
|
+
accountId?: string;
|
|
51
|
+
cfg: KlawConfig;
|
|
52
|
+
textLimit: number;
|
|
53
|
+
deps: LineAutoReplyDeps;
|
|
54
|
+
}): Promise<{ replyTokenUsed: boolean }> {
|
|
55
|
+
const { payload, lineData, replyToken, accountId, to, textLimit, deps } = params;
|
|
56
|
+
let replyTokenUsed = params.replyTokenUsed;
|
|
57
|
+
|
|
58
|
+
const pushLineMessages = async (messages: messagingApi.Message[]): Promise<void> => {
|
|
59
|
+
if (messages.length === 0) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
for (let i = 0; i < messages.length; i += 5) {
|
|
63
|
+
await deps.pushMessagesLine(to, messages.slice(i, i + 5), {
|
|
64
|
+
cfg: params.cfg,
|
|
65
|
+
accountId,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const sendLineMessages = async (
|
|
71
|
+
messages: messagingApi.Message[],
|
|
72
|
+
allowReplyToken: boolean,
|
|
73
|
+
): Promise<void> => {
|
|
74
|
+
if (messages.length === 0) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let remaining = messages;
|
|
79
|
+
if (allowReplyToken && replyToken && !replyTokenUsed) {
|
|
80
|
+
const replyBatch = remaining.slice(0, 5);
|
|
81
|
+
try {
|
|
82
|
+
await deps.replyMessageLine(replyToken, replyBatch, {
|
|
83
|
+
cfg: params.cfg,
|
|
84
|
+
accountId,
|
|
85
|
+
});
|
|
86
|
+
} catch (err) {
|
|
87
|
+
deps.onReplyError?.(err);
|
|
88
|
+
await pushLineMessages(replyBatch);
|
|
89
|
+
}
|
|
90
|
+
replyTokenUsed = true;
|
|
91
|
+
remaining = remaining.slice(replyBatch.length);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (remaining.length > 0) {
|
|
95
|
+
await pushLineMessages(remaining);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const richMessages: messagingApi.Message[] = [];
|
|
100
|
+
const hasQuickReplies = Boolean(lineData.quickReplies?.length);
|
|
101
|
+
|
|
102
|
+
if (lineData.flexMessage) {
|
|
103
|
+
richMessages.push(
|
|
104
|
+
deps.createFlexMessage(
|
|
105
|
+
lineData.flexMessage.altText.slice(0, 400),
|
|
106
|
+
lineData.flexMessage.contents as FlexContainer,
|
|
107
|
+
),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (lineData.templateMessage) {
|
|
112
|
+
const templateMsg = deps.buildTemplateMessageFromPayload(lineData.templateMessage);
|
|
113
|
+
if (templateMsg) {
|
|
114
|
+
richMessages.push(templateMsg);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (lineData.location) {
|
|
119
|
+
richMessages.push(deps.createLocationMessage(lineData.location));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const processed = payload.text
|
|
123
|
+
? deps.processLineMessage(payload.text)
|
|
124
|
+
: { text: "", flexMessages: [] };
|
|
125
|
+
|
|
126
|
+
for (const flexMsg of processed.flexMessages) {
|
|
127
|
+
richMessages.push(deps.createFlexMessage(flexMsg.altText.slice(0, 400), flexMsg.contents));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const chunks = processed.text ? deps.chunkMarkdownText(processed.text, textLimit) : [];
|
|
131
|
+
|
|
132
|
+
const mediaUrls = resolveSendableOutboundReplyParts(payload).mediaUrls;
|
|
133
|
+
const mediaMessages = mediaUrls
|
|
134
|
+
.map((url) => url?.trim())
|
|
135
|
+
.filter((url): url is string => Boolean(url))
|
|
136
|
+
.map((url) => deps.createImageMessage(url));
|
|
137
|
+
|
|
138
|
+
if (chunks.length > 0) {
|
|
139
|
+
const hasRichOrMedia = richMessages.length > 0 || mediaMessages.length > 0;
|
|
140
|
+
if (hasQuickReplies && hasRichOrMedia) {
|
|
141
|
+
try {
|
|
142
|
+
await sendLineMessages([...richMessages, ...mediaMessages], false);
|
|
143
|
+
} catch (err) {
|
|
144
|
+
deps.onReplyError?.(err);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const { replyTokenUsed: nextReplyTokenUsed } = await deps.sendLineReplyChunks({
|
|
148
|
+
to,
|
|
149
|
+
chunks,
|
|
150
|
+
quickReplies: lineData.quickReplies,
|
|
151
|
+
replyToken,
|
|
152
|
+
replyTokenUsed,
|
|
153
|
+
cfg: params.cfg,
|
|
154
|
+
accountId,
|
|
155
|
+
replyMessageLine: deps.replyMessageLine,
|
|
156
|
+
pushMessageLine: deps.pushMessageLine,
|
|
157
|
+
pushTextMessageWithQuickReplies: deps.pushTextMessageWithQuickReplies,
|
|
158
|
+
createTextMessageWithQuickReplies: deps.createTextMessageWithQuickReplies,
|
|
159
|
+
});
|
|
160
|
+
replyTokenUsed = nextReplyTokenUsed;
|
|
161
|
+
if (!hasQuickReplies || !hasRichOrMedia) {
|
|
162
|
+
await sendLineMessages(richMessages, false);
|
|
163
|
+
if (mediaMessages.length > 0) {
|
|
164
|
+
await sendLineMessages(mediaMessages, false);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
const combined = [...richMessages, ...mediaMessages];
|
|
169
|
+
if (hasQuickReplies && combined.length === 0) {
|
|
170
|
+
const { replyTokenUsed: nextReplyTokenUsed } = await deps.sendLineReplyChunks({
|
|
171
|
+
to,
|
|
172
|
+
chunks: [buildLineQuickReplyFallbackText(lineData.quickReplies)],
|
|
173
|
+
quickReplies: lineData.quickReplies,
|
|
174
|
+
replyToken,
|
|
175
|
+
replyTokenUsed,
|
|
176
|
+
cfg: params.cfg,
|
|
177
|
+
accountId,
|
|
178
|
+
replyMessageLine: deps.replyMessageLine,
|
|
179
|
+
pushMessageLine: deps.pushMessageLine,
|
|
180
|
+
pushTextMessageWithQuickReplies: deps.pushTextMessageWithQuickReplies,
|
|
181
|
+
createTextMessageWithQuickReplies: deps.createTextMessageWithQuickReplies,
|
|
182
|
+
onReplyError: deps.onReplyError,
|
|
183
|
+
});
|
|
184
|
+
replyTokenUsed = nextReplyTokenUsed;
|
|
185
|
+
} else {
|
|
186
|
+
if (hasQuickReplies && combined.length > 0) {
|
|
187
|
+
const quickReply = deps.createQuickReplyItems(lineData.quickReplies!);
|
|
188
|
+
const targetIndex =
|
|
189
|
+
replyToken && !replyTokenUsed ? Math.min(4, combined.length - 1) : combined.length - 1;
|
|
190
|
+
const target = combined[targetIndex] as messagingApi.Message & {
|
|
191
|
+
quickReply?: messagingApi.QuickReply;
|
|
192
|
+
};
|
|
193
|
+
combined[targetIndex] = { ...target, quickReply };
|
|
194
|
+
}
|
|
195
|
+
await sendLineMessages(combined, true);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return { replyTokenUsed };
|
|
200
|
+
}
|
package/src/bindings.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
function normalizeLineConversationId(raw?: string | null): string | null {
|
|
2
|
+
const trimmed = raw?.trim() ?? "";
|
|
3
|
+
if (!trimmed) {
|
|
4
|
+
return null;
|
|
5
|
+
}
|
|
6
|
+
const prefixed = trimmed.match(/^line:(?:(?:user|group|room):)?(.+)$/i)?.[1];
|
|
7
|
+
return (prefixed ?? trimmed).trim() || null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function resolveLineCommandConversation(params: {
|
|
11
|
+
originatingTo?: string;
|
|
12
|
+
commandTo?: string;
|
|
13
|
+
fallbackTo?: string;
|
|
14
|
+
}) {
|
|
15
|
+
const conversationId =
|
|
16
|
+
normalizeLineConversationId(params.originatingTo) ??
|
|
17
|
+
normalizeLineConversationId(params.commandTo) ??
|
|
18
|
+
normalizeLineConversationId(params.fallbackTo);
|
|
19
|
+
return conversationId ? { conversationId } : null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function resolveLineInboundConversation(params: { to?: string; conversationId?: string }) {
|
|
23
|
+
const conversationId =
|
|
24
|
+
normalizeLineConversationId(params.conversationId) ?? normalizeLineConversationId(params.to);
|
|
25
|
+
return conversationId ? { conversationId } : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const lineBindingsAdapter = {
|
|
29
|
+
compileConfiguredBinding: ({ conversationId }: { conversationId?: string }) => {
|
|
30
|
+
const normalized = normalizeLineConversationId(conversationId);
|
|
31
|
+
return normalized ? { conversationId: normalized } : null;
|
|
32
|
+
},
|
|
33
|
+
matchInboundConversation: ({
|
|
34
|
+
compiledBinding,
|
|
35
|
+
conversationId,
|
|
36
|
+
}: {
|
|
37
|
+
compiledBinding: { conversationId: string };
|
|
38
|
+
conversationId?: string;
|
|
39
|
+
}) => {
|
|
40
|
+
const normalizedIncoming = normalizeLineConversationId(conversationId);
|
|
41
|
+
if (!normalizedIncoming || compiledBinding.conversationId !== normalizedIncoming) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
conversationId: normalizedIncoming,
|
|
46
|
+
matchPriority: 2,
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
resolveCommandConversation: ({
|
|
50
|
+
originatingTo,
|
|
51
|
+
commandTo,
|
|
52
|
+
fallbackTo,
|
|
53
|
+
}: {
|
|
54
|
+
originatingTo?: string;
|
|
55
|
+
commandTo?: string;
|
|
56
|
+
fallbackTo?: string;
|
|
57
|
+
}) =>
|
|
58
|
+
resolveLineCommandConversation({
|
|
59
|
+
originatingTo,
|
|
60
|
+
commandTo,
|
|
61
|
+
fallbackTo,
|
|
62
|
+
}),
|
|
63
|
+
resolveInboundConversation: ({ to, conversationId }: { to?: string; conversationId?: string }) =>
|
|
64
|
+
resolveLineInboundConversation({ to, conversationId }),
|
|
65
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { firstDefined } from "klaw/plugin-sdk/allow-from";
|
|
2
|
+
|
|
3
|
+
export type NormalizedAllowFrom = {
|
|
4
|
+
entries: string[];
|
|
5
|
+
hasWildcard: boolean;
|
|
6
|
+
hasEntries: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function normalizeLineAllowEntry(value: string | number): string {
|
|
10
|
+
const trimmed = String(value).trim();
|
|
11
|
+
if (!trimmed) {
|
|
12
|
+
return "";
|
|
13
|
+
}
|
|
14
|
+
if (trimmed === "*") {
|
|
15
|
+
return "*";
|
|
16
|
+
}
|
|
17
|
+
return trimmed.replace(/^line:(?:user:)?/i, "");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const normalizeAllowFrom = (list?: Array<string | number>): NormalizedAllowFrom => {
|
|
21
|
+
const entries = (list ?? []).map((value) => normalizeLineAllowEntry(value)).filter(Boolean);
|
|
22
|
+
const hasWildcard = entries.includes("*");
|
|
23
|
+
return {
|
|
24
|
+
entries,
|
|
25
|
+
hasWildcard,
|
|
26
|
+
hasEntries: entries.length > 0,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { firstDefined };
|