@gakr-gakr/line 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.ts +11 -0
- package/autobot.plugin.json +15 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +5 -0
- package/index.ts +54 -0
- package/package.json +60 -0
- package/runtime-api.ts +182 -0
- package/secret-contract-api.ts +4 -0
- package/setup-api.ts +2 -0
- package/setup-entry.ts +9 -0
- package/src/account-helpers.ts +16 -0
- package/src/accounts.ts +187 -0
- package/src/actions.ts +61 -0
- package/src/auto-reply-delivery.ts +200 -0
- package/src/bindings.ts +65 -0
- package/src/bot-access.ts +30 -0
- package/src/bot-handlers.ts +620 -0
- package/src/bot-message-context.ts +586 -0
- package/src/bot.ts +70 -0
- package/src/card-command.ts +347 -0
- package/src/channel-access-token.ts +14 -0
- package/src/channel-api.ts +17 -0
- package/src/channel-shared.ts +48 -0
- package/src/channel.runtime.ts +3 -0
- package/src/channel.setup.ts +11 -0
- package/src/channel.ts +155 -0
- package/src/config-adapter.ts +29 -0
- package/src/config-schema.ts +81 -0
- package/src/download.ts +34 -0
- package/src/flex-templates/basic-cards.ts +395 -0
- package/src/flex-templates/common.ts +20 -0
- package/src/flex-templates/media-control-cards.ts +555 -0
- package/src/flex-templates/message.ts +13 -0
- package/src/flex-templates/schedule-cards.ts +467 -0
- package/src/flex-templates/types.ts +22 -0
- package/src/flex-templates.ts +32 -0
- package/src/gateway.ts +129 -0
- package/src/group-keys.ts +65 -0
- package/src/group-policy.ts +22 -0
- package/src/markdown-to-line.ts +416 -0
- package/src/monitor-durable.ts +37 -0
- package/src/monitor.runtime.ts +1 -0
- package/src/monitor.ts +507 -0
- package/src/outbound-media.ts +120 -0
- package/src/outbound.runtime.ts +12 -0
- package/src/outbound.ts +427 -0
- package/src/probe.runtime.ts +1 -0
- package/src/probe.ts +34 -0
- package/src/quick-reply-fallback.ts +10 -0
- package/src/reply-chunks.ts +110 -0
- package/src/reply-payload-transform.ts +317 -0
- package/src/rich-menu.ts +326 -0
- package/src/runtime.ts +32 -0
- package/src/send-receipt.ts +32 -0
- package/src/send.ts +531 -0
- package/src/setup-core.ts +149 -0
- package/src/setup-runtime-api.ts +9 -0
- package/src/setup-surface.ts +229 -0
- package/src/signature.ts +24 -0
- package/src/status.ts +37 -0
- package/src/template-messages.ts +333 -0
- package/src/types.ts +130 -0
- package/src/webhook-node.ts +155 -0
- package/src/webhook-utils.ts +10 -0
- package/src/webhook.ts +135 -0
- package/tsconfig.json +16 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { BaseProbeResult } from "autobot/plugin-sdk/channel-contract";
|
|
2
|
+
import type { MessageReceipt } from "autobot/plugin-sdk/channel-message";
|
|
3
|
+
|
|
4
|
+
export type LineTokenSource = "config" | "env" | "file" | "none";
|
|
5
|
+
|
|
6
|
+
interface LineThreadBindingsConfig {
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
idleHours?: number;
|
|
9
|
+
maxAgeHours?: number;
|
|
10
|
+
spawnSessions?: boolean;
|
|
11
|
+
defaultSpawnContext?: "isolated" | "fork";
|
|
12
|
+
/** @deprecated Use spawnSessions instead. */
|
|
13
|
+
spawnSubagentSessions?: boolean;
|
|
14
|
+
/** @deprecated Use spawnSessions instead. */
|
|
15
|
+
spawnAcpSessions?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface LineAccountBaseConfig {
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
channelAccessToken?: string;
|
|
21
|
+
channelSecret?: string;
|
|
22
|
+
tokenFile?: string;
|
|
23
|
+
secretFile?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
allowFrom?: Array<string | number>;
|
|
26
|
+
groupAllowFrom?: Array<string | number>;
|
|
27
|
+
dmPolicy?: "open" | "allowlist" | "pairing" | "disabled";
|
|
28
|
+
groupPolicy?: "open" | "allowlist" | "disabled";
|
|
29
|
+
responsePrefix?: string;
|
|
30
|
+
mediaMaxMb?: number;
|
|
31
|
+
webhookPath?: string;
|
|
32
|
+
threadBindings?: LineThreadBindingsConfig;
|
|
33
|
+
groups?: Record<string, LineGroupConfig>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface LineConfig extends LineAccountBaseConfig {
|
|
37
|
+
accounts?: Record<string, LineAccountConfig>;
|
|
38
|
+
defaultAccount?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LineAccountConfig extends LineAccountBaseConfig {}
|
|
42
|
+
|
|
43
|
+
export interface LineGroupConfig {
|
|
44
|
+
enabled?: boolean;
|
|
45
|
+
allowFrom?: Array<string | number>;
|
|
46
|
+
requireMention?: boolean;
|
|
47
|
+
systemPrompt?: string;
|
|
48
|
+
skills?: string[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ResolvedLineAccount {
|
|
52
|
+
accountId: string;
|
|
53
|
+
name?: string;
|
|
54
|
+
enabled: boolean;
|
|
55
|
+
channelAccessToken: string;
|
|
56
|
+
channelSecret: string;
|
|
57
|
+
tokenSource: LineTokenSource;
|
|
58
|
+
config: LineConfig & LineAccountConfig;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface LineSendResult {
|
|
62
|
+
messageId: string;
|
|
63
|
+
chatId: string;
|
|
64
|
+
receipt: MessageReceipt;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type LineProbeResult = BaseProbeResult<string> & {
|
|
68
|
+
bot?: {
|
|
69
|
+
displayName?: string;
|
|
70
|
+
userId?: string;
|
|
71
|
+
basicId?: string;
|
|
72
|
+
pictureUrl?: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
type LineFlexMessagePayload = {
|
|
77
|
+
altText: string;
|
|
78
|
+
contents: unknown;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type LineTemplateMessagePayload =
|
|
82
|
+
| {
|
|
83
|
+
type: "confirm";
|
|
84
|
+
text: string;
|
|
85
|
+
confirmLabel: string;
|
|
86
|
+
confirmData: string;
|
|
87
|
+
cancelLabel: string;
|
|
88
|
+
cancelData: string;
|
|
89
|
+
altText?: string;
|
|
90
|
+
}
|
|
91
|
+
| {
|
|
92
|
+
type: "buttons";
|
|
93
|
+
title: string;
|
|
94
|
+
text: string;
|
|
95
|
+
actions: Array<{
|
|
96
|
+
type: "message" | "uri" | "postback";
|
|
97
|
+
label: string;
|
|
98
|
+
data?: string;
|
|
99
|
+
uri?: string;
|
|
100
|
+
}>;
|
|
101
|
+
thumbnailImageUrl?: string;
|
|
102
|
+
altText?: string;
|
|
103
|
+
}
|
|
104
|
+
| {
|
|
105
|
+
type: "carousel";
|
|
106
|
+
columns: Array<{
|
|
107
|
+
title?: string;
|
|
108
|
+
text: string;
|
|
109
|
+
thumbnailImageUrl?: string;
|
|
110
|
+
actions: Array<{
|
|
111
|
+
type: "message" | "uri" | "postback";
|
|
112
|
+
label: string;
|
|
113
|
+
data?: string;
|
|
114
|
+
uri?: string;
|
|
115
|
+
}>;
|
|
116
|
+
}>;
|
|
117
|
+
altText?: string;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export type LineChannelData = {
|
|
121
|
+
quickReplies?: string[];
|
|
122
|
+
location?: {
|
|
123
|
+
title: string;
|
|
124
|
+
address: string;
|
|
125
|
+
latitude: number;
|
|
126
|
+
longitude: number;
|
|
127
|
+
};
|
|
128
|
+
flexMessage?: LineFlexMessagePayload;
|
|
129
|
+
templateMessage?: LineTemplateMessagePayload;
|
|
130
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { webhook } from "@line/bot-sdk";
|
|
3
|
+
import {
|
|
4
|
+
createMessageReceiveContext,
|
|
5
|
+
type MessageReceiveContext,
|
|
6
|
+
} from "autobot/plugin-sdk/channel-message";
|
|
7
|
+
import { danger, logVerbose, type RuntimeEnv } from "autobot/plugin-sdk/runtime-env";
|
|
8
|
+
import {
|
|
9
|
+
isRequestBodyLimitError,
|
|
10
|
+
readRequestBodyWithLimit,
|
|
11
|
+
requestBodyErrorToText,
|
|
12
|
+
} from "autobot/plugin-sdk/webhook-request-guards";
|
|
13
|
+
import { parseLineWebhookBody, validateLineSignature } from "./webhook-utils.js";
|
|
14
|
+
|
|
15
|
+
const LINE_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
|
|
16
|
+
const LINE_WEBHOOK_PREAUTH_MAX_BODY_BYTES = 64 * 1024;
|
|
17
|
+
const LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS = 5_000;
|
|
18
|
+
|
|
19
|
+
export async function readLineWebhookRequestBody(
|
|
20
|
+
req: IncomingMessage,
|
|
21
|
+
maxBytes = LINE_WEBHOOK_MAX_BODY_BYTES,
|
|
22
|
+
timeoutMs = LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS,
|
|
23
|
+
): Promise<string> {
|
|
24
|
+
return await readRequestBodyWithLimit(req, {
|
|
25
|
+
maxBytes,
|
|
26
|
+
timeoutMs,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type ReadBodyFn = (req: IncomingMessage, maxBytes: number, timeoutMs?: number) => Promise<string>;
|
|
31
|
+
|
|
32
|
+
function logLineWebhookDispatchError(runtime: RuntimeEnv | undefined, err: unknown): void {
|
|
33
|
+
runtime?.error?.(danger(`line webhook dispatch failed: ${String(err)}`));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createLineNodeWebhookHandler(params: {
|
|
37
|
+
channelSecret: string;
|
|
38
|
+
bot: { handleWebhook: (body: webhook.CallbackRequest) => Promise<void> };
|
|
39
|
+
runtime: RuntimeEnv;
|
|
40
|
+
readBody?: ReadBodyFn;
|
|
41
|
+
maxBodyBytes?: number;
|
|
42
|
+
onRequestAuthenticated?: () => void;
|
|
43
|
+
}): (req: IncomingMessage, res: ServerResponse) => Promise<void> {
|
|
44
|
+
const maxBodyBytes = params.maxBodyBytes ?? LINE_WEBHOOK_MAX_BODY_BYTES;
|
|
45
|
+
const readBody = params.readBody ?? readLineWebhookRequestBody;
|
|
46
|
+
|
|
47
|
+
return async (req: IncomingMessage, res: ServerResponse) => {
|
|
48
|
+
if (req.method === "GET" || req.method === "HEAD") {
|
|
49
|
+
if (req.method === "HEAD") {
|
|
50
|
+
res.statusCode = 204;
|
|
51
|
+
res.end();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
res.statusCode = 200;
|
|
55
|
+
res.setHeader("Content-Type", "text/plain");
|
|
56
|
+
res.end("OK");
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (req.method !== "POST") {
|
|
61
|
+
res.statusCode = 405;
|
|
62
|
+
res.setHeader("Allow", "GET, HEAD, POST");
|
|
63
|
+
res.setHeader("Content-Type", "application/json");
|
|
64
|
+
res.end(JSON.stringify({ error: "Method Not Allowed" }));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let receiveContext: MessageReceiveContext<webhook.CallbackRequest> | undefined;
|
|
69
|
+
try {
|
|
70
|
+
const signatureHeader = req.headers["x-line-signature"];
|
|
71
|
+
const signature =
|
|
72
|
+
typeof signatureHeader === "string"
|
|
73
|
+
? signatureHeader.trim()
|
|
74
|
+
: Array.isArray(signatureHeader)
|
|
75
|
+
? (signatureHeader[0] ?? "").trim()
|
|
76
|
+
: "";
|
|
77
|
+
|
|
78
|
+
if (!signature) {
|
|
79
|
+
logVerbose("line: webhook missing X-Line-Signature header");
|
|
80
|
+
res.statusCode = 400;
|
|
81
|
+
res.setHeader("Content-Type", "application/json");
|
|
82
|
+
res.end(JSON.stringify({ error: "Missing X-Line-Signature header" }));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const rawBody = await readBody(
|
|
87
|
+
req,
|
|
88
|
+
Math.min(maxBodyBytes, LINE_WEBHOOK_PREAUTH_MAX_BODY_BYTES),
|
|
89
|
+
LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
if (!validateLineSignature(rawBody, signature, params.channelSecret)) {
|
|
93
|
+
logVerbose("line: webhook signature validation failed");
|
|
94
|
+
res.statusCode = 401;
|
|
95
|
+
res.setHeader("Content-Type", "application/json");
|
|
96
|
+
res.end(JSON.stringify({ error: "Invalid signature" }));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const body = parseLineWebhookBody(rawBody);
|
|
101
|
+
|
|
102
|
+
if (!body) {
|
|
103
|
+
res.statusCode = 400;
|
|
104
|
+
res.setHeader("Content-Type", "application/json");
|
|
105
|
+
res.end(JSON.stringify({ error: "Invalid webhook payload" }));
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
params.onRequestAuthenticated?.();
|
|
110
|
+
|
|
111
|
+
receiveContext = createMessageReceiveContext({
|
|
112
|
+
id: `${Date.now()}:line:webhook`,
|
|
113
|
+
channel: "line",
|
|
114
|
+
message: body,
|
|
115
|
+
ackPolicy: "after_receive_record",
|
|
116
|
+
onAck: () => {
|
|
117
|
+
res.statusCode = 200;
|
|
118
|
+
res.setHeader("Content-Type", "application/json");
|
|
119
|
+
res.end(JSON.stringify({ status: "ok" }));
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (receiveContext.shouldAckAfter("receive_record")) {
|
|
124
|
+
await receiveContext.ack();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (body.events && body.events.length > 0) {
|
|
128
|
+
logVerbose(`line: received ${body.events.length} webhook events`);
|
|
129
|
+
void Promise.resolve()
|
|
130
|
+
.then(() => params.bot.handleWebhook(body))
|
|
131
|
+
.catch((err) => logLineWebhookDispatchError(params.runtime, err));
|
|
132
|
+
}
|
|
133
|
+
} catch (err) {
|
|
134
|
+
await receiveContext?.nack(err);
|
|
135
|
+
if (isRequestBodyLimitError(err, "PAYLOAD_TOO_LARGE")) {
|
|
136
|
+
res.statusCode = 413;
|
|
137
|
+
res.setHeader("Content-Type", "application/json");
|
|
138
|
+
res.end(JSON.stringify({ error: "Payload too large" }));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (isRequestBodyLimitError(err, "REQUEST_BODY_TIMEOUT")) {
|
|
142
|
+
res.statusCode = 408;
|
|
143
|
+
res.setHeader("Content-Type", "application/json");
|
|
144
|
+
res.end(JSON.stringify({ error: requestBodyErrorToText("REQUEST_BODY_TIMEOUT") }));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
params.runtime.error?.(danger(`line webhook error: ${String(err)}`));
|
|
148
|
+
if (!res.headersSent) {
|
|
149
|
+
res.statusCode = 500;
|
|
150
|
+
res.setHeader("Content-Type", "application/json");
|
|
151
|
+
res.end(JSON.stringify({ error: "Internal server error" }));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { webhook } from "@line/bot-sdk";
|
|
2
|
+
export { validateLineSignature } from "./signature.js";
|
|
3
|
+
|
|
4
|
+
export function parseLineWebhookBody(rawBody: string): webhook.CallbackRequest | null {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(rawBody) as webhook.CallbackRequest;
|
|
7
|
+
} catch {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
}
|
package/src/webhook.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { webhook } from "@line/bot-sdk";
|
|
2
|
+
import type { NextFunction, Request, Response } from "express";
|
|
3
|
+
import {
|
|
4
|
+
createMessageReceiveContext,
|
|
5
|
+
type MessageReceiveContext,
|
|
6
|
+
} from "autobot/plugin-sdk/channel-message";
|
|
7
|
+
import { danger, logVerbose, type RuntimeEnv } from "autobot/plugin-sdk/runtime-env";
|
|
8
|
+
import { parseLineWebhookBody, validateLineSignature } from "./webhook-utils.js";
|
|
9
|
+
|
|
10
|
+
const LINE_WEBHOOK_MAX_RAW_BODY_BYTES = 64 * 1024;
|
|
11
|
+
|
|
12
|
+
export interface LineWebhookOptions {
|
|
13
|
+
channelSecret: string;
|
|
14
|
+
onEvents: (body: webhook.CallbackRequest) => Promise<void>;
|
|
15
|
+
runtime?: RuntimeEnv;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readRawBody(req: Request): string | null {
|
|
19
|
+
const rawBody =
|
|
20
|
+
(req as { rawBody?: string | Buffer }).rawBody ??
|
|
21
|
+
(typeof req.body === "string" || Buffer.isBuffer(req.body) ? req.body : null);
|
|
22
|
+
if (!rawBody) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return Buffer.isBuffer(rawBody) ? rawBody.toString("utf-8") : rawBody;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseWebhookBody(rawBody?: string | null): webhook.CallbackRequest | null {
|
|
29
|
+
if (!rawBody) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return parseLineWebhookBody(rawBody);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function logLineWebhookDispatchError(runtime: RuntimeEnv | undefined, err: unknown): void {
|
|
36
|
+
runtime?.error?.(danger(`line webhook dispatch failed: ${String(err)}`));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createLineWebhookMiddleware(
|
|
40
|
+
options: LineWebhookOptions,
|
|
41
|
+
): (req: Request, res: Response, _next: NextFunction) => Promise<void> {
|
|
42
|
+
const { channelSecret, onEvents, runtime } = options;
|
|
43
|
+
|
|
44
|
+
return async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
|
45
|
+
let receiveContext: MessageReceiveContext<webhook.CallbackRequest> | undefined;
|
|
46
|
+
try {
|
|
47
|
+
const signature = req.headers["x-line-signature"];
|
|
48
|
+
|
|
49
|
+
if (!signature || typeof signature !== "string") {
|
|
50
|
+
res.status(400).json({ error: "Missing X-Line-Signature header" });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const rawBody = readRawBody(req);
|
|
55
|
+
|
|
56
|
+
if (!rawBody) {
|
|
57
|
+
res.status(400).json({ error: "Missing raw request body for signature verification" });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (Buffer.byteLength(rawBody, "utf-8") > LINE_WEBHOOK_MAX_RAW_BODY_BYTES) {
|
|
61
|
+
res.status(413).json({ error: "Payload too large" });
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!validateLineSignature(rawBody, signature, channelSecret)) {
|
|
66
|
+
logVerbose("line: webhook signature validation failed");
|
|
67
|
+
res.status(401).json({ error: "Invalid signature" });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const body = parseWebhookBody(rawBody);
|
|
72
|
+
|
|
73
|
+
if (!body) {
|
|
74
|
+
res.status(400).json({ error: "Invalid webhook payload" });
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
receiveContext = createMessageReceiveContext({
|
|
79
|
+
id: `${Date.now()}:line:webhook`,
|
|
80
|
+
channel: "line",
|
|
81
|
+
message: body,
|
|
82
|
+
ackPolicy: "after_receive_record",
|
|
83
|
+
onAck: () => {
|
|
84
|
+
res.status(200).json({ status: "ok" });
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (receiveContext.shouldAckAfter("receive_record")) {
|
|
89
|
+
await receiveContext.ack();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (body.events && body.events.length > 0) {
|
|
93
|
+
logVerbose(`line: received ${body.events.length} webhook events`);
|
|
94
|
+
void Promise.resolve()
|
|
95
|
+
.then(() => onEvents(body))
|
|
96
|
+
.catch((err) => logLineWebhookDispatchError(runtime, err));
|
|
97
|
+
}
|
|
98
|
+
} catch (err) {
|
|
99
|
+
await receiveContext?.nack(err);
|
|
100
|
+
runtime?.error?.(danger(`line webhook error: ${String(err)}`));
|
|
101
|
+
if (!res.headersSent) {
|
|
102
|
+
res.status(500).json({ error: "Internal server error" });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface StartLineWebhookOptions {
|
|
109
|
+
channelSecret: string;
|
|
110
|
+
onEvents: (body: webhook.CallbackRequest) => Promise<void>;
|
|
111
|
+
runtime?: RuntimeEnv;
|
|
112
|
+
path?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function startLineWebhook(options: StartLineWebhookOptions): {
|
|
116
|
+
path: string;
|
|
117
|
+
handler: (req: Request, res: Response, _next: NextFunction) => Promise<void>;
|
|
118
|
+
} {
|
|
119
|
+
const channelSecret =
|
|
120
|
+
typeof options.channelSecret === "string" ? options.channelSecret.trim() : "";
|
|
121
|
+
if (!channelSecret) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
"LINE webhook mode requires a non-empty channel secret. " +
|
|
124
|
+
"Set channels.line.channelSecret in your config.",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const path = options.path ?? "/line/webhook";
|
|
128
|
+
const middleware = createLineWebhookMiddleware({
|
|
129
|
+
channelSecret,
|
|
130
|
+
onEvents: options.onEvents,
|
|
131
|
+
runtime: options.runtime,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return { path, handler: middleware };
|
|
135
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.package-boundary.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "."
|
|
5
|
+
},
|
|
6
|
+
"include": ["./*.ts", "./src/**/*.ts"],
|
|
7
|
+
"exclude": [
|
|
8
|
+
"./**/*.test.ts",
|
|
9
|
+
"./dist/**",
|
|
10
|
+
"./node_modules/**",
|
|
11
|
+
"./src/test-support/**",
|
|
12
|
+
"./src/**/*test-helpers.ts",
|
|
13
|
+
"./src/**/*test-harness.ts",
|
|
14
|
+
"./src/**/*test-support.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|