@openclaw/line 2026.5.2-beta.1
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/index.ts +54 -0
- package/openclaw.plugin.json +342 -0
- package/package.json +58 -0
- package/runtime-api.ts +187 -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 +290 -0
- package/src/accounts.ts +187 -0
- package/src/actions.ts +61 -0
- package/src/auto-reply-delivery.test.ts +248 -0
- package/src/auto-reply-delivery.ts +200 -0
- package/src/bindings.ts +65 -0
- package/src/bot-access.ts +48 -0
- package/src/bot-handlers.test.ts +1089 -0
- package/src/bot-handlers.ts +642 -0
- package/src/bot-message-context.test.ts +405 -0
- package/src/bot-message-context.ts +581 -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-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 +514 -0
- package/src/channel.setup.ts +11 -0
- package/src/channel.status.test.ts +63 -0
- package/src/channel.ts +154 -0
- package/src/config-adapter.ts +29 -0
- package/src/config-schema.ts +57 -0
- package/src/download.test.ts +133 -0
- package/src/download.ts +87 -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.lifecycle.test.ts +421 -0
- package/src/monitor.runtime.ts +1 -0
- package/src/monitor.ts +506 -0
- package/src/outbound-media.test.ts +189 -0
- package/src/outbound-media.ts +120 -0
- package/src/outbound.runtime.ts +12 -0
- package/src/outbound.ts +356 -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 +179 -0
- package/src/reply-chunks.ts +110 -0
- package/src/reply-payload-transform.test.ts +387 -0
- package/src/reply-payload-transform.ts +317 -0
- package/src/rich-menu.test.ts +310 -0
- package/src/rich-menu.ts +326 -0
- package/src/runtime.ts +32 -0
- package/src/send.test.ts +346 -0
- package/src/send.ts +489 -0
- package/src/setup-core.ts +149 -0
- package/src/setup-runtime-api.ts +9 -0
- package/src/setup-surface.test.ts +474 -0
- package/src/setup-surface.ts +227 -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 +128 -0
- package/src/webhook-node.test.ts +513 -0
- package/src/webhook-node.ts +131 -0
- package/src/webhook-utils.ts +10 -0
- package/src/webhook.ts +111 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createStartAccountContext } from "openclaw/plugin-sdk/channel-test-helpers";
|
|
4
|
+
import {
|
|
5
|
+
createPluginSetupWizardConfigure,
|
|
6
|
+
createTestWizardPrompter,
|
|
7
|
+
runSetupWizardConfigure,
|
|
8
|
+
} from "openclaw/plugin-sdk/plugin-test-runtime";
|
|
9
|
+
import type { WizardPrompter } from "openclaw/plugin-sdk/plugin-test-runtime";
|
|
10
|
+
import { bundledPluginRoot } from "openclaw/plugin-sdk/test-fixtures";
|
|
11
|
+
import ts from "typescript";
|
|
12
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
13
|
+
import type { OpenClawConfig, PluginRuntime, ResolvedLineAccount } from "../api.js";
|
|
14
|
+
import { linePlugin } from "./channel.js";
|
|
15
|
+
import { lineGatewayAdapter } from "./gateway.js";
|
|
16
|
+
import { probeLineBot } from "./probe.js";
|
|
17
|
+
import { clearLineRuntime, setLineRuntime } from "./runtime.js";
|
|
18
|
+
import { lineSetupWizard } from "./setup-surface.js";
|
|
19
|
+
import { lineStatusAdapter } from "./status.js";
|
|
20
|
+
|
|
21
|
+
const { getBotInfoMock, MessagingApiClientMock } = vi.hoisted(() => {
|
|
22
|
+
const getBotInfoMock = vi.fn();
|
|
23
|
+
const MessagingApiClientMock = vi.fn(function () {
|
|
24
|
+
return { getBotInfo: getBotInfoMock };
|
|
25
|
+
});
|
|
26
|
+
return { getBotInfoMock, MessagingApiClientMock };
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
vi.mock("@line/bot-sdk", () => ({
|
|
30
|
+
messagingApi: { MessagingApiClient: MessagingApiClientMock },
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
const lineConfigure = createPluginSetupWizardConfigure(linePlugin);
|
|
34
|
+
const LINE_SRC_PREFIX = `../../${bundledPluginRoot("line")}/src/`;
|
|
35
|
+
|
|
36
|
+
function normalizeModuleSpecifier(specifier: string): string | null {
|
|
37
|
+
if (specifier.startsWith("./src/")) {
|
|
38
|
+
return specifier;
|
|
39
|
+
}
|
|
40
|
+
if (specifier.startsWith(LINE_SRC_PREFIX)) {
|
|
41
|
+
return `./src/${specifier.slice(LINE_SRC_PREFIX.length)}`;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function collectModuleExportNames(filePath: string): string[] {
|
|
47
|
+
const sourcePath = filePath.replace(/\.js$/, ".ts");
|
|
48
|
+
const sourceText = readFileSync(sourcePath, "utf8");
|
|
49
|
+
const sourceFile = ts.createSourceFile(sourcePath, sourceText, ts.ScriptTarget.Latest, true);
|
|
50
|
+
const names = new Set<string>();
|
|
51
|
+
|
|
52
|
+
for (const statement of sourceFile.statements) {
|
|
53
|
+
if (
|
|
54
|
+
ts.isExportDeclaration(statement) &&
|
|
55
|
+
statement.exportClause &&
|
|
56
|
+
ts.isNamedExports(statement.exportClause)
|
|
57
|
+
) {
|
|
58
|
+
for (const element of statement.exportClause.elements) {
|
|
59
|
+
if (!element.isTypeOnly) {
|
|
60
|
+
names.add(element.name.text);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : undefined;
|
|
67
|
+
const isExported = modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
68
|
+
if (!isExported) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (ts.isVariableStatement(statement)) {
|
|
73
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
74
|
+
if (ts.isIdentifier(declaration.name)) {
|
|
75
|
+
names.add(declaration.name.text);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (
|
|
82
|
+
ts.isFunctionDeclaration(statement) ||
|
|
83
|
+
ts.isClassDeclaration(statement) ||
|
|
84
|
+
ts.isEnumDeclaration(statement)
|
|
85
|
+
) {
|
|
86
|
+
if (statement.name) {
|
|
87
|
+
names.add(statement.name.text);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return Array.from(names).toSorted();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function collectRuntimeApiPreExports(runtimeApiPath: string): string[] {
|
|
96
|
+
const runtimeApiSource = readFileSync(runtimeApiPath, "utf8");
|
|
97
|
+
const runtimeApiFile = ts.createSourceFile(
|
|
98
|
+
runtimeApiPath,
|
|
99
|
+
runtimeApiSource,
|
|
100
|
+
ts.ScriptTarget.Latest,
|
|
101
|
+
true,
|
|
102
|
+
);
|
|
103
|
+
const preExports = new Set<string>();
|
|
104
|
+
let pluginSdkLineRuntimeSeen = false;
|
|
105
|
+
const removedLineRuntimeSpecifier = ["openclaw", "plugin-sdk", "line-runtime"].join("/");
|
|
106
|
+
|
|
107
|
+
for (const statement of runtimeApiFile.statements) {
|
|
108
|
+
if (!ts.isExportDeclaration(statement)) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const moduleSpecifier =
|
|
112
|
+
statement.moduleSpecifier && ts.isStringLiteral(statement.moduleSpecifier)
|
|
113
|
+
? statement.moduleSpecifier.text
|
|
114
|
+
: undefined;
|
|
115
|
+
if (!moduleSpecifier) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (moduleSpecifier === removedLineRuntimeSpecifier) {
|
|
119
|
+
pluginSdkLineRuntimeSeen = true;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
const normalized = normalizeModuleSpecifier(moduleSpecifier);
|
|
123
|
+
if (!normalized) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!statement.exportClause) {
|
|
128
|
+
for (const name of collectModuleExportNames(
|
|
129
|
+
path.join(process.cwd(), "extensions", "line", normalized),
|
|
130
|
+
)) {
|
|
131
|
+
preExports.add(name);
|
|
132
|
+
}
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!ts.isNamedExports(statement.exportClause)) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (const element of statement.exportClause.elements) {
|
|
141
|
+
if (!element.isTypeOnly) {
|
|
142
|
+
preExports.add(element.name.text);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!pluginSdkLineRuntimeSeen) {
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return Array.from(preExports).toSorted();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
describe("line setup wizard", () => {
|
|
155
|
+
it("configures token and secret for the default account", async () => {
|
|
156
|
+
const prompter = createTestWizardPrompter({
|
|
157
|
+
text: vi.fn(async ({ message }: { message: string }) => {
|
|
158
|
+
if (message === "Enter LINE channel access token") {
|
|
159
|
+
return "line-token";
|
|
160
|
+
}
|
|
161
|
+
if (message === "Enter LINE channel secret") {
|
|
162
|
+
return "line-secret";
|
|
163
|
+
}
|
|
164
|
+
throw new Error(`Unexpected prompt: ${message}`);
|
|
165
|
+
}) as WizardPrompter["text"],
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const result = await runSetupWizardConfigure({
|
|
169
|
+
configure: lineConfigure,
|
|
170
|
+
cfg: {} as OpenClawConfig,
|
|
171
|
+
prompter,
|
|
172
|
+
options: {},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
expect(result.accountId).toBe("default");
|
|
176
|
+
expect(result.cfg.channels?.line?.enabled).toBe(true);
|
|
177
|
+
expect(result.cfg.channels?.line?.channelAccessToken).toBe("line-token");
|
|
178
|
+
expect(result.cfg.channels?.line?.channelSecret).toBe("line-secret");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("reads the named-account DM policy instead of the channel root", async () => {
|
|
182
|
+
expect(
|
|
183
|
+
lineSetupWizard.dmPolicy?.getCurrent(
|
|
184
|
+
{
|
|
185
|
+
channels: {
|
|
186
|
+
line: {
|
|
187
|
+
dmPolicy: "disabled",
|
|
188
|
+
accounts: {
|
|
189
|
+
work: {
|
|
190
|
+
channelAccessToken: "token",
|
|
191
|
+
channelSecret: "secret",
|
|
192
|
+
dmPolicy: "allowlist",
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
} as OpenClawConfig,
|
|
198
|
+
"work",
|
|
199
|
+
),
|
|
200
|
+
).toBe("allowlist");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("reports account-scoped config keys for named accounts", async () => {
|
|
204
|
+
expect(lineSetupWizard.dmPolicy?.resolveConfigKeys?.({} as OpenClawConfig, "work")).toEqual({
|
|
205
|
+
policyKey: "channels.line.accounts.work.dmPolicy",
|
|
206
|
+
allowFromKey: "channels.line.accounts.work.allowFrom",
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("uses configured defaultAccount for omitted DM policy account context", async () => {
|
|
211
|
+
const cfg = {
|
|
212
|
+
channels: {
|
|
213
|
+
line: {
|
|
214
|
+
defaultAccount: "work",
|
|
215
|
+
dmPolicy: "disabled",
|
|
216
|
+
allowFrom: ["Uroot"],
|
|
217
|
+
accounts: {
|
|
218
|
+
work: {
|
|
219
|
+
channelAccessToken: "token",
|
|
220
|
+
channelSecret: "secret",
|
|
221
|
+
dmPolicy: "allowlist",
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
} as OpenClawConfig;
|
|
227
|
+
|
|
228
|
+
expect(lineSetupWizard.dmPolicy?.getCurrent(cfg)).toBe("allowlist");
|
|
229
|
+
expect(lineSetupWizard.dmPolicy?.resolveConfigKeys?.(cfg)).toEqual({
|
|
230
|
+
policyKey: "channels.line.accounts.work.dmPolicy",
|
|
231
|
+
allowFromKey: "channels.line.accounts.work.allowFrom",
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const next = lineSetupWizard.dmPolicy?.setPolicy(cfg, "open");
|
|
235
|
+
const workAccount = next?.channels?.line?.accounts?.work as
|
|
236
|
+
| {
|
|
237
|
+
dmPolicy?: string;
|
|
238
|
+
}
|
|
239
|
+
| undefined;
|
|
240
|
+
expect(next?.channels?.line?.dmPolicy).toBe("disabled");
|
|
241
|
+
expect(workAccount?.dmPolicy).toBe("open");
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('writes open policy state to the named account and preserves inherited allowFrom with "*"', async () => {
|
|
245
|
+
const next = lineSetupWizard.dmPolicy?.setPolicy(
|
|
246
|
+
{
|
|
247
|
+
channels: {
|
|
248
|
+
line: {
|
|
249
|
+
allowFrom: ["Uroot"],
|
|
250
|
+
accounts: {
|
|
251
|
+
work: {
|
|
252
|
+
channelAccessToken: "token",
|
|
253
|
+
channelSecret: "secret",
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
} as OpenClawConfig,
|
|
259
|
+
"open",
|
|
260
|
+
"work",
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const workAccount = next?.channels?.line?.accounts?.work as
|
|
264
|
+
| {
|
|
265
|
+
dmPolicy?: string;
|
|
266
|
+
allowFrom?: string[];
|
|
267
|
+
}
|
|
268
|
+
| undefined;
|
|
269
|
+
expect(next?.channels?.line?.dmPolicy).toBeUndefined();
|
|
270
|
+
expect(next?.channels?.line?.allowFrom).toEqual(["Uroot"]);
|
|
271
|
+
expect(workAccount?.dmPolicy).toBe("open");
|
|
272
|
+
expect(workAccount?.allowFrom).toEqual(["Uroot", "*"]);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("uses configured defaultAccount for omitted setup configured state", async () => {
|
|
276
|
+
const configured = await lineSetupWizard.status.resolveConfigured({
|
|
277
|
+
cfg: {
|
|
278
|
+
channels: {
|
|
279
|
+
line: {
|
|
280
|
+
defaultAccount: "work",
|
|
281
|
+
channelAccessToken: "root-token",
|
|
282
|
+
channelSecret: "root-secret",
|
|
283
|
+
accounts: {
|
|
284
|
+
alerts: {
|
|
285
|
+
channelAccessToken: "alerts-token",
|
|
286
|
+
channelSecret: "alerts-secret",
|
|
287
|
+
},
|
|
288
|
+
work: {
|
|
289
|
+
channelAccessToken: "",
|
|
290
|
+
channelSecret: "",
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
} as OpenClawConfig,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
expect(configured).toBe(false);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe("probeLineBot", () => {
|
|
303
|
+
beforeEach(() => {
|
|
304
|
+
getBotInfoMock.mockReset();
|
|
305
|
+
MessagingApiClientMock.mockReset();
|
|
306
|
+
MessagingApiClientMock.mockImplementation(function () {
|
|
307
|
+
return { getBotInfo: getBotInfoMock };
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
afterEach(() => {
|
|
312
|
+
clearLineRuntime();
|
|
313
|
+
vi.useRealTimers();
|
|
314
|
+
getBotInfoMock.mockClear();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("returns timeout when bot info stalls", async () => {
|
|
318
|
+
vi.useFakeTimers();
|
|
319
|
+
getBotInfoMock.mockImplementation(() => new Promise(() => {}));
|
|
320
|
+
|
|
321
|
+
const probePromise = probeLineBot("token", 10);
|
|
322
|
+
await vi.advanceTimersByTimeAsync(20);
|
|
323
|
+
const result = await probePromise;
|
|
324
|
+
|
|
325
|
+
expect(result.ok).toBe(false);
|
|
326
|
+
expect(result.error).toBe("timeout");
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("returns bot info when available", async () => {
|
|
330
|
+
getBotInfoMock.mockResolvedValue({
|
|
331
|
+
displayName: "OpenClaw",
|
|
332
|
+
userId: "U123",
|
|
333
|
+
basicId: "@openclaw",
|
|
334
|
+
pictureUrl: "https://example.com/bot.png",
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const result = await probeLineBot("token", 50);
|
|
338
|
+
|
|
339
|
+
expect(result.ok).toBe(true);
|
|
340
|
+
expect(result.bot?.userId).toBe("U123");
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe("linePlugin status.probeAccount", () => {
|
|
345
|
+
it("falls back to the direct probe helper when runtime is not initialized", async () => {
|
|
346
|
+
MessagingApiClientMock.mockReset();
|
|
347
|
+
MessagingApiClientMock.mockImplementation(function () {
|
|
348
|
+
return { getBotInfo: getBotInfoMock };
|
|
349
|
+
});
|
|
350
|
+
getBotInfoMock.mockResolvedValue({
|
|
351
|
+
displayName: "OpenClaw",
|
|
352
|
+
userId: "U123",
|
|
353
|
+
basicId: "@openclaw",
|
|
354
|
+
pictureUrl: "https://example.com/bot.png",
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const params = {
|
|
358
|
+
cfg: {} as OpenClawConfig,
|
|
359
|
+
account: {
|
|
360
|
+
accountId: "default",
|
|
361
|
+
enabled: true,
|
|
362
|
+
channelAccessToken: "token",
|
|
363
|
+
channelSecret: "secret",
|
|
364
|
+
tokenSource: "config",
|
|
365
|
+
} as ResolvedLineAccount,
|
|
366
|
+
timeoutMs: 50,
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
clearLineRuntime();
|
|
370
|
+
|
|
371
|
+
await expect(lineStatusAdapter.probeAccount!(params)).resolves.toEqual(
|
|
372
|
+
await probeLineBot("token", 50),
|
|
373
|
+
);
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
describe("line runtime api", () => {
|
|
378
|
+
it("keeps the LINE runtime barrel self-contained", () => {
|
|
379
|
+
const runtimeApiPath = path.join(process.cwd(), "extensions", "line", "runtime-api.ts");
|
|
380
|
+
expect(collectRuntimeApiPreExports(runtimeApiPath)).toEqual([]);
|
|
381
|
+
expect(collectRuntimeApiPreExports(runtimeApiPath)).toEqual([]);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
function createRuntime() {
|
|
386
|
+
const monitorLineProvider = vi.fn(async () => ({
|
|
387
|
+
account: { accountId: "default" },
|
|
388
|
+
handleWebhook: async () => {},
|
|
389
|
+
stop: () => {},
|
|
390
|
+
}));
|
|
391
|
+
|
|
392
|
+
const runtime = {
|
|
393
|
+
channel: {
|
|
394
|
+
line: {
|
|
395
|
+
monitorLineProvider,
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
logging: {
|
|
399
|
+
shouldLogVerbose: () => false,
|
|
400
|
+
},
|
|
401
|
+
} as unknown as PluginRuntime;
|
|
402
|
+
|
|
403
|
+
return { runtime, monitorLineProvider };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function createAccount(params: { token: string; secret: string }): ResolvedLineAccount {
|
|
407
|
+
return {
|
|
408
|
+
accountId: "default",
|
|
409
|
+
enabled: true,
|
|
410
|
+
channelAccessToken: params.token,
|
|
411
|
+
channelSecret: params.secret,
|
|
412
|
+
tokenSource: "config",
|
|
413
|
+
config: {} as ResolvedLineAccount["config"],
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function startLineAccount(params: { account: ResolvedLineAccount; abortSignal?: AbortSignal }) {
|
|
418
|
+
const { runtime, monitorLineProvider } = createRuntime();
|
|
419
|
+
setLineRuntime(runtime);
|
|
420
|
+
return {
|
|
421
|
+
monitorLineProvider,
|
|
422
|
+
task: lineGatewayAdapter.startAccount!(
|
|
423
|
+
createStartAccountContext({
|
|
424
|
+
account: params.account,
|
|
425
|
+
abortSignal: params.abortSignal,
|
|
426
|
+
}),
|
|
427
|
+
),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
describe("linePlugin gateway.startAccount", () => {
|
|
432
|
+
it("fails startup when channel secret is missing", async () => {
|
|
433
|
+
const { monitorLineProvider, task } = startLineAccount({
|
|
434
|
+
account: createAccount({ token: "token", secret: " " }),
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
await expect(task).rejects.toThrow(
|
|
438
|
+
'LINE webhook mode requires a non-empty channel secret for account "default".',
|
|
439
|
+
);
|
|
440
|
+
expect(monitorLineProvider).not.toHaveBeenCalled();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("fails startup when channel access token is missing", async () => {
|
|
444
|
+
const { monitorLineProvider, task } = startLineAccount({
|
|
445
|
+
account: createAccount({ token: " ", secret: "secret" }),
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
await expect(task).rejects.toThrow(
|
|
449
|
+
'LINE webhook mode requires a non-empty channel access token for account "default".',
|
|
450
|
+
);
|
|
451
|
+
expect(monitorLineProvider).not.toHaveBeenCalled();
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
it("starts provider when token and secret are present", async () => {
|
|
455
|
+
const abort = new AbortController();
|
|
456
|
+
const { monitorLineProvider, task } = startLineAccount({
|
|
457
|
+
account: createAccount({ token: "token", secret: "secret" }),
|
|
458
|
+
abortSignal: abort.signal,
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
await vi.waitFor(() => {
|
|
462
|
+
expect(monitorLineProvider).toHaveBeenCalledWith(
|
|
463
|
+
expect.objectContaining({
|
|
464
|
+
channelAccessToken: "token",
|
|
465
|
+
channelSecret: "secret",
|
|
466
|
+
accountId: "default",
|
|
467
|
+
}),
|
|
468
|
+
);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
abort.abort();
|
|
472
|
+
await task;
|
|
473
|
+
});
|
|
474
|
+
});
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAllowFromSection,
|
|
3
|
+
createStandardChannelSetupStatus,
|
|
4
|
+
mergeAllowFromEntries,
|
|
5
|
+
} from "openclaw/plugin-sdk/setup";
|
|
6
|
+
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
|
|
7
|
+
import { resolveDefaultLineAccountId } from "./accounts.js";
|
|
8
|
+
import {
|
|
9
|
+
isLineConfigured,
|
|
10
|
+
listLineAccountIds,
|
|
11
|
+
parseLineAllowFromId,
|
|
12
|
+
patchLineAccountConfig,
|
|
13
|
+
} from "./setup-core.js";
|
|
14
|
+
import {
|
|
15
|
+
DEFAULT_ACCOUNT_ID,
|
|
16
|
+
formatDocsLink,
|
|
17
|
+
resolveLineAccount,
|
|
18
|
+
setSetupChannelEnabled,
|
|
19
|
+
splitSetupEntries,
|
|
20
|
+
type ChannelSetupDmPolicy,
|
|
21
|
+
type ChannelSetupWizard,
|
|
22
|
+
} from "./setup-runtime-api.js";
|
|
23
|
+
|
|
24
|
+
const channel = "line" as const;
|
|
25
|
+
|
|
26
|
+
const LINE_SETUP_HELP_LINES = [
|
|
27
|
+
"1) Open the LINE Developers Console and create or pick a Messaging API channel",
|
|
28
|
+
"2) Copy the channel access token and channel secret",
|
|
29
|
+
"3) Enable Use webhook in the Messaging API settings",
|
|
30
|
+
"4) Point the webhook at https://<gateway-host>/line/webhook",
|
|
31
|
+
`Docs: ${formatDocsLink("/channels/line", "channels/line")}`,
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const LINE_ALLOW_FROM_HELP_LINES = [
|
|
35
|
+
"Allowlist LINE DMs by user id.",
|
|
36
|
+
"LINE ids are case-sensitive.",
|
|
37
|
+
"Examples:",
|
|
38
|
+
"- U1234567890abcdef1234567890abcdef",
|
|
39
|
+
"- line:user:U1234567890abcdef1234567890abcdef",
|
|
40
|
+
"Multiple entries: comma-separated.",
|
|
41
|
+
`Docs: ${formatDocsLink("/channels/line", "channels/line")}`,
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const lineDmPolicy: ChannelSetupDmPolicy = {
|
|
45
|
+
label: "LINE",
|
|
46
|
+
channel,
|
|
47
|
+
policyKey: "channels.line.dmPolicy",
|
|
48
|
+
allowFromKey: "channels.line.allowFrom",
|
|
49
|
+
resolveConfigKeys: (cfg, accountId) =>
|
|
50
|
+
(accountId ?? resolveDefaultLineAccountId(cfg)) !== DEFAULT_ACCOUNT_ID
|
|
51
|
+
? {
|
|
52
|
+
policyKey: `channels.line.accounts.${accountId ?? resolveDefaultLineAccountId(cfg)}.dmPolicy`,
|
|
53
|
+
allowFromKey: `channels.line.accounts.${accountId ?? resolveDefaultLineAccountId(cfg)}.allowFrom`,
|
|
54
|
+
}
|
|
55
|
+
: {
|
|
56
|
+
policyKey: "channels.line.dmPolicy",
|
|
57
|
+
allowFromKey: "channels.line.allowFrom",
|
|
58
|
+
},
|
|
59
|
+
getCurrent: (cfg, accountId) =>
|
|
60
|
+
resolveLineAccount({ cfg, accountId: accountId ?? resolveDefaultLineAccountId(cfg) }).config
|
|
61
|
+
.dmPolicy ?? "pairing",
|
|
62
|
+
setPolicy: (cfg, policy, accountId) =>
|
|
63
|
+
patchLineAccountConfig({
|
|
64
|
+
cfg,
|
|
65
|
+
accountId: accountId ?? resolveDefaultLineAccountId(cfg),
|
|
66
|
+
enabled: true,
|
|
67
|
+
patch:
|
|
68
|
+
policy === "open"
|
|
69
|
+
? {
|
|
70
|
+
dmPolicy: "open",
|
|
71
|
+
allowFrom: mergeAllowFromEntries(
|
|
72
|
+
resolveLineAccount({
|
|
73
|
+
cfg,
|
|
74
|
+
accountId: accountId ?? resolveDefaultLineAccountId(cfg),
|
|
75
|
+
}).config.allowFrom,
|
|
76
|
+
["*"],
|
|
77
|
+
),
|
|
78
|
+
}
|
|
79
|
+
: { dmPolicy: policy },
|
|
80
|
+
clearFields: policy === "pairing" || policy === "disabled" ? ["allowFrom"] : undefined,
|
|
81
|
+
}),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const lineSetupWizard: ChannelSetupWizard = {
|
|
85
|
+
channel,
|
|
86
|
+
status: createStandardChannelSetupStatus({
|
|
87
|
+
channelLabel: "LINE",
|
|
88
|
+
configuredLabel: "configured",
|
|
89
|
+
unconfiguredLabel: "needs token + secret",
|
|
90
|
+
configuredHint: "configured",
|
|
91
|
+
unconfiguredHint: "needs token + secret",
|
|
92
|
+
configuredScore: 1,
|
|
93
|
+
unconfiguredScore: 0,
|
|
94
|
+
includeStatusLine: true,
|
|
95
|
+
resolveConfigured: ({ cfg, accountId }) =>
|
|
96
|
+
isLineConfigured(cfg, accountId ?? resolveDefaultLineAccountId(cfg)),
|
|
97
|
+
resolveExtraStatusLines: ({ cfg }) => [`Accounts: ${listLineAccountIds(cfg).length || 0}`],
|
|
98
|
+
}),
|
|
99
|
+
introNote: {
|
|
100
|
+
title: "LINE Messaging API",
|
|
101
|
+
lines: LINE_SETUP_HELP_LINES,
|
|
102
|
+
shouldShow: ({ cfg, accountId }) =>
|
|
103
|
+
!isLineConfigured(cfg, accountId ?? resolveDefaultLineAccountId(cfg)),
|
|
104
|
+
},
|
|
105
|
+
credentials: [
|
|
106
|
+
{
|
|
107
|
+
inputKey: "token",
|
|
108
|
+
providerHint: channel,
|
|
109
|
+
credentialLabel: "channel access token",
|
|
110
|
+
preferredEnvVar: "LINE_CHANNEL_ACCESS_TOKEN",
|
|
111
|
+
helpTitle: "LINE Messaging API",
|
|
112
|
+
helpLines: LINE_SETUP_HELP_LINES,
|
|
113
|
+
envPrompt: "LINE_CHANNEL_ACCESS_TOKEN detected. Use env var?",
|
|
114
|
+
keepPrompt: "LINE channel access token already configured. Keep it?",
|
|
115
|
+
inputPrompt: "Enter LINE channel access token",
|
|
116
|
+
allowEnv: ({ accountId }) => accountId === DEFAULT_ACCOUNT_ID,
|
|
117
|
+
inspect: ({ cfg, accountId }) => {
|
|
118
|
+
const resolved = resolveLineAccount({ cfg, accountId });
|
|
119
|
+
return {
|
|
120
|
+
accountConfigured: Boolean(
|
|
121
|
+
normalizeOptionalString(resolved.channelAccessToken) &&
|
|
122
|
+
normalizeOptionalString(resolved.channelSecret),
|
|
123
|
+
),
|
|
124
|
+
hasConfiguredValue: Boolean(
|
|
125
|
+
normalizeOptionalString(resolved.config.channelAccessToken) ??
|
|
126
|
+
normalizeOptionalString(resolved.config.tokenFile),
|
|
127
|
+
),
|
|
128
|
+
resolvedValue: normalizeOptionalString(resolved.channelAccessToken),
|
|
129
|
+
envValue:
|
|
130
|
+
accountId === DEFAULT_ACCOUNT_ID
|
|
131
|
+
? normalizeOptionalString(process.env.LINE_CHANNEL_ACCESS_TOKEN)
|
|
132
|
+
: undefined,
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
applyUseEnv: ({ cfg, accountId }) =>
|
|
136
|
+
patchLineAccountConfig({
|
|
137
|
+
cfg,
|
|
138
|
+
accountId,
|
|
139
|
+
enabled: true,
|
|
140
|
+
clearFields: ["channelAccessToken", "tokenFile"],
|
|
141
|
+
patch: {},
|
|
142
|
+
}),
|
|
143
|
+
applySet: ({ cfg, accountId, resolvedValue }) =>
|
|
144
|
+
patchLineAccountConfig({
|
|
145
|
+
cfg,
|
|
146
|
+
accountId,
|
|
147
|
+
enabled: true,
|
|
148
|
+
clearFields: ["tokenFile"],
|
|
149
|
+
patch: { channelAccessToken: resolvedValue },
|
|
150
|
+
}),
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
inputKey: "password",
|
|
154
|
+
providerHint: "line-secret",
|
|
155
|
+
credentialLabel: "channel secret",
|
|
156
|
+
preferredEnvVar: "LINE_CHANNEL_SECRET",
|
|
157
|
+
helpTitle: "LINE Messaging API",
|
|
158
|
+
helpLines: LINE_SETUP_HELP_LINES,
|
|
159
|
+
envPrompt: "LINE_CHANNEL_SECRET detected. Use env var?",
|
|
160
|
+
keepPrompt: "LINE channel secret already configured. Keep it?",
|
|
161
|
+
inputPrompt: "Enter LINE channel secret",
|
|
162
|
+
allowEnv: ({ accountId }) => accountId === DEFAULT_ACCOUNT_ID,
|
|
163
|
+
inspect: ({ cfg, accountId }) => {
|
|
164
|
+
const resolved = resolveLineAccount({ cfg, accountId });
|
|
165
|
+
return {
|
|
166
|
+
accountConfigured: Boolean(
|
|
167
|
+
normalizeOptionalString(resolved.channelAccessToken) &&
|
|
168
|
+
normalizeOptionalString(resolved.channelSecret),
|
|
169
|
+
),
|
|
170
|
+
hasConfiguredValue: Boolean(
|
|
171
|
+
normalizeOptionalString(resolved.config.channelSecret) ??
|
|
172
|
+
normalizeOptionalString(resolved.config.secretFile),
|
|
173
|
+
),
|
|
174
|
+
resolvedValue: normalizeOptionalString(resolved.channelSecret),
|
|
175
|
+
envValue:
|
|
176
|
+
accountId === DEFAULT_ACCOUNT_ID
|
|
177
|
+
? normalizeOptionalString(process.env.LINE_CHANNEL_SECRET)
|
|
178
|
+
: undefined,
|
|
179
|
+
};
|
|
180
|
+
},
|
|
181
|
+
applyUseEnv: ({ cfg, accountId }) =>
|
|
182
|
+
patchLineAccountConfig({
|
|
183
|
+
cfg,
|
|
184
|
+
accountId,
|
|
185
|
+
enabled: true,
|
|
186
|
+
clearFields: ["channelSecret", "secretFile"],
|
|
187
|
+
patch: {},
|
|
188
|
+
}),
|
|
189
|
+
applySet: ({ cfg, accountId, resolvedValue }) =>
|
|
190
|
+
patchLineAccountConfig({
|
|
191
|
+
cfg,
|
|
192
|
+
accountId,
|
|
193
|
+
enabled: true,
|
|
194
|
+
clearFields: ["secretFile"],
|
|
195
|
+
patch: { channelSecret: resolvedValue },
|
|
196
|
+
}),
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
allowFrom: createAllowFromSection({
|
|
200
|
+
helpTitle: "LINE allowlist",
|
|
201
|
+
helpLines: LINE_ALLOW_FROM_HELP_LINES,
|
|
202
|
+
message: "LINE allowFrom (user id)",
|
|
203
|
+
placeholder: "U1234567890abcdef1234567890abcdef",
|
|
204
|
+
invalidWithoutCredentialNote:
|
|
205
|
+
"LINE allowFrom requires raw user ids like U1234567890abcdef1234567890abcdef.",
|
|
206
|
+
parseInputs: splitSetupEntries,
|
|
207
|
+
parseId: parseLineAllowFromId,
|
|
208
|
+
apply: ({ cfg, accountId, allowFrom }) =>
|
|
209
|
+
patchLineAccountConfig({
|
|
210
|
+
cfg,
|
|
211
|
+
accountId,
|
|
212
|
+
enabled: true,
|
|
213
|
+
patch: { dmPolicy: "allowlist", allowFrom },
|
|
214
|
+
}),
|
|
215
|
+
}),
|
|
216
|
+
dmPolicy: lineDmPolicy,
|
|
217
|
+
completionNote: {
|
|
218
|
+
title: "LINE webhook",
|
|
219
|
+
lines: [
|
|
220
|
+
"Enable Use webhook in the LINE console after saving credentials.",
|
|
221
|
+
"Default webhook URL: https://<gateway-host>/line/webhook",
|
|
222
|
+
"If you set channels.line.webhookPath, update the URL to match.",
|
|
223
|
+
`Docs: ${formatDocsLink("/channels/line", "channels/line")}`,
|
|
224
|
+
],
|
|
225
|
+
},
|
|
226
|
+
disable: (cfg) => setSetupChannelEnabled(cfg, channel, false),
|
|
227
|
+
};
|