@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
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { messagingApi } from "@line/bot-sdk";
|
|
2
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
3
|
+
import { resolveSendableOutboundReplyParts } from "autobot/plugin-sdk/reply-payload";
|
|
4
|
+
import type { ReplyPayload } from "autobot/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: AutoBotConfig; 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: AutoBotConfig;
|
|
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 "autobot/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 };
|