@kodelyth/zalo 2026.5.42 → 2026.6.2
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/klaw.plugin.json +509 -2
- package/package.json +19 -6
- package/api.ts +0 -8
- package/channel-plugin-api.ts +0 -1
- package/contract-api.ts +0 -5
- package/index.test.ts +0 -15
- package/index.ts +0 -20
- package/runtime-api.test.ts +0 -10
- package/runtime-api.ts +0 -71
- package/secret-contract-api.ts +0 -5
- package/setup-api.ts +0 -34
- package/setup-entry.ts +0 -13
- package/src/accounts.test.ts +0 -95
- package/src/accounts.ts +0 -65
- package/src/actions.runtime.ts +0 -5
- package/src/actions.test.ts +0 -32
- package/src/actions.ts +0 -62
- package/src/api.test.ts +0 -166
- package/src/api.ts +0 -265
- package/src/approval-auth.test.ts +0 -17
- package/src/approval-auth.ts +0 -25
- package/src/channel.directory.test.ts +0 -56
- package/src/channel.runtime.ts +0 -89
- package/src/channel.startup.test.ts +0 -121
- package/src/channel.ts +0 -309
- package/src/config-schema.test.ts +0 -30
- package/src/config-schema.ts +0 -29
- package/src/group-access.ts +0 -23
- package/src/monitor-durable.test.ts +0 -49
- package/src/monitor-durable.ts +0 -38
- package/src/monitor.group-policy.test.ts +0 -213
- package/src/monitor.image.polling.test.ts +0 -113
- package/src/monitor.lifecycle.test.ts +0 -194
- package/src/monitor.pairing.lifecycle.test.ts +0 -139
- package/src/monitor.polling.media-reply.test.ts +0 -433
- package/src/monitor.reply-once.lifecycle.test.ts +0 -178
- package/src/monitor.ts +0 -1009
- package/src/monitor.types.ts +0 -4
- package/src/monitor.webhook.test.ts +0 -808
- package/src/monitor.webhook.ts +0 -278
- package/src/outbound-media.test.ts +0 -186
- package/src/outbound-media.ts +0 -236
- package/src/outbound-payload.contract.test.ts +0 -143
- package/src/probe.ts +0 -45
- package/src/proxy.ts +0 -18
- package/src/runtime-api.ts +0 -71
- package/src/runtime-support.ts +0 -82
- package/src/runtime.ts +0 -9
- package/src/secret-contract.ts +0 -109
- package/src/secret-input.ts +0 -5
- package/src/send.test.ts +0 -150
- package/src/send.ts +0 -207
- package/src/session-route.ts +0 -32
- package/src/setup-allow-from.ts +0 -97
- package/src/setup-core.ts +0 -152
- package/src/setup-status.test.ts +0 -33
- package/src/setup-surface.test.ts +0 -193
- package/src/setup-surface.ts +0 -294
- package/src/status-issues.test.ts +0 -17
- package/src/status-issues.ts +0 -34
- package/src/test-support/lifecycle-test-support.ts +0 -456
- package/src/test-support/monitor-mocks-test-support.ts +0 -209
- package/src/token.test.ts +0 -92
- package/src/token.ts +0 -79
- package/src/types.ts +0 -50
- package/test-api.ts +0 -1
- package/tsconfig.json +0 -16
package/src/api.ts
DELETED
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Zalo Bot API client
|
|
3
|
-
* @see https://bot.zaloplatforms.com/docs
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { resolvePinnedHostnameWithPolicy, type SsrFPolicy } from "klaw/plugin-sdk/ssrf-runtime";
|
|
7
|
-
|
|
8
|
-
const ZALO_API_BASE = "https://bot-api.zaloplatforms.com";
|
|
9
|
-
const ZALO_MEDIA_SSRF_POLICY: SsrFPolicy = {};
|
|
10
|
-
|
|
11
|
-
export type ZaloFetch = (input: string, init?: RequestInit) => Promise<Response>;
|
|
12
|
-
|
|
13
|
-
export type ZaloApiResponse<T = unknown> = {
|
|
14
|
-
ok: boolean;
|
|
15
|
-
result?: T;
|
|
16
|
-
error_code?: number;
|
|
17
|
-
description?: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type ZaloBotInfo = {
|
|
21
|
-
id: string;
|
|
22
|
-
name: string;
|
|
23
|
-
avatar?: string;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type ZaloMessage = {
|
|
27
|
-
message_id: string;
|
|
28
|
-
from: {
|
|
29
|
-
id: string;
|
|
30
|
-
name?: string;
|
|
31
|
-
display_name?: string;
|
|
32
|
-
avatar?: string;
|
|
33
|
-
is_bot?: boolean;
|
|
34
|
-
};
|
|
35
|
-
chat: {
|
|
36
|
-
id: string;
|
|
37
|
-
chat_type: "PRIVATE" | "GROUP";
|
|
38
|
-
};
|
|
39
|
-
date: number;
|
|
40
|
-
text?: string;
|
|
41
|
-
photo_url?: string;
|
|
42
|
-
caption?: string;
|
|
43
|
-
sticker?: string;
|
|
44
|
-
message_type?: string;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export type ZaloUpdate = {
|
|
48
|
-
event_name:
|
|
49
|
-
| "message.text.received"
|
|
50
|
-
| "message.image.received"
|
|
51
|
-
| "message.sticker.received"
|
|
52
|
-
| "message.unsupported.received";
|
|
53
|
-
message?: ZaloMessage;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export type ZaloSendMessageParams = {
|
|
57
|
-
chat_id: string;
|
|
58
|
-
text: string;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type ZaloSendPhotoParams = {
|
|
62
|
-
chat_id: string;
|
|
63
|
-
photo: string;
|
|
64
|
-
caption?: string;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export type ZaloSendChatActionParams = {
|
|
68
|
-
chat_id: string;
|
|
69
|
-
action: "typing" | "upload_photo";
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export type ZaloSetWebhookParams = {
|
|
73
|
-
url: string;
|
|
74
|
-
secret_token: string;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export type ZaloWebhookInfo = {
|
|
78
|
-
url?: string;
|
|
79
|
-
updated_at?: number;
|
|
80
|
-
has_custom_certificate?: boolean;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export type ZaloGetUpdatesParams = {
|
|
84
|
-
/** Timeout in seconds (passed as string to API) */
|
|
85
|
-
timeout?: number;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export class ZaloApiError extends Error {
|
|
89
|
-
constructor(
|
|
90
|
-
message: string,
|
|
91
|
-
public readonly errorCode?: number,
|
|
92
|
-
public readonly description?: string,
|
|
93
|
-
) {
|
|
94
|
-
super(message);
|
|
95
|
-
this.name = "ZaloApiError";
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** True if this is a long-polling timeout (no updates available) */
|
|
99
|
-
get isPollingTimeout(): boolean {
|
|
100
|
-
return this.errorCode === 408;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Call the Zalo Bot API
|
|
106
|
-
*/
|
|
107
|
-
export async function callZaloApi<T = unknown>(
|
|
108
|
-
method: string,
|
|
109
|
-
token: string,
|
|
110
|
-
body?: Record<string, unknown>,
|
|
111
|
-
options?: { timeoutMs?: number; fetch?: ZaloFetch },
|
|
112
|
-
): Promise<ZaloApiResponse<T>> {
|
|
113
|
-
const url = `${ZALO_API_BASE}/bot${token}/${method}`;
|
|
114
|
-
const controller = new AbortController();
|
|
115
|
-
const timeoutId = options?.timeoutMs
|
|
116
|
-
? setTimeout(() => controller.abort(), options.timeoutMs)
|
|
117
|
-
: undefined;
|
|
118
|
-
const fetcher = options?.fetch ?? fetch;
|
|
119
|
-
|
|
120
|
-
try {
|
|
121
|
-
const response = await fetcher(url, {
|
|
122
|
-
method: "POST",
|
|
123
|
-
headers: {
|
|
124
|
-
"Content-Type": "application/json",
|
|
125
|
-
},
|
|
126
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
127
|
-
signal: controller.signal,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const data = (await response.json()) as ZaloApiResponse<T>;
|
|
131
|
-
|
|
132
|
-
if (!data.ok) {
|
|
133
|
-
throw new ZaloApiError(
|
|
134
|
-
data.description ?? `Zalo API error: ${method}`,
|
|
135
|
-
data.error_code,
|
|
136
|
-
data.description,
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return data;
|
|
141
|
-
} finally {
|
|
142
|
-
if (timeoutId) {
|
|
143
|
-
clearTimeout(timeoutId);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Validate bot token and get bot info
|
|
150
|
-
*/
|
|
151
|
-
export async function getMe(
|
|
152
|
-
token: string,
|
|
153
|
-
timeoutMs?: number,
|
|
154
|
-
fetcher?: ZaloFetch,
|
|
155
|
-
): Promise<ZaloApiResponse<ZaloBotInfo>> {
|
|
156
|
-
return callZaloApi<ZaloBotInfo>("getMe", token, undefined, { timeoutMs, fetch: fetcher });
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Send a text message
|
|
161
|
-
*/
|
|
162
|
-
export async function sendMessage(
|
|
163
|
-
token: string,
|
|
164
|
-
params: ZaloSendMessageParams,
|
|
165
|
-
fetcher?: ZaloFetch,
|
|
166
|
-
): Promise<ZaloApiResponse<ZaloMessage>> {
|
|
167
|
-
return callZaloApi<ZaloMessage>("sendMessage", token, params, { fetch: fetcher });
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Send a photo message
|
|
172
|
-
*/
|
|
173
|
-
export async function sendPhoto(
|
|
174
|
-
token: string,
|
|
175
|
-
params: ZaloSendPhotoParams,
|
|
176
|
-
fetcher?: ZaloFetch,
|
|
177
|
-
): Promise<ZaloApiResponse<ZaloMessage>> {
|
|
178
|
-
const photoUrl = params.photo.trim();
|
|
179
|
-
let parsedPhotoUrl: URL;
|
|
180
|
-
try {
|
|
181
|
-
parsedPhotoUrl = new URL(photoUrl);
|
|
182
|
-
} catch {
|
|
183
|
-
throw new Error("Zalo photo URL must be an absolute HTTP or HTTPS URL");
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (parsedPhotoUrl.protocol !== "http:" && parsedPhotoUrl.protocol !== "https:") {
|
|
187
|
-
throw new Error("Zalo photo URL must use HTTP or HTTPS");
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
await resolvePinnedHostnameWithPolicy(parsedPhotoUrl.hostname, {
|
|
191
|
-
policy: ZALO_MEDIA_SSRF_POLICY,
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
return callZaloApi<ZaloMessage>(
|
|
195
|
-
"sendPhoto",
|
|
196
|
-
token,
|
|
197
|
-
{ ...params, photo: parsedPhotoUrl.href },
|
|
198
|
-
{ fetch: fetcher },
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Send a temporary chat action such as typing.
|
|
204
|
-
*/
|
|
205
|
-
export async function sendChatAction(
|
|
206
|
-
token: string,
|
|
207
|
-
params: ZaloSendChatActionParams,
|
|
208
|
-
fetcher?: ZaloFetch,
|
|
209
|
-
timeoutMs?: number,
|
|
210
|
-
): Promise<ZaloApiResponse<boolean>> {
|
|
211
|
-
return callZaloApi<boolean>("sendChatAction", token, params, {
|
|
212
|
-
timeoutMs,
|
|
213
|
-
fetch: fetcher,
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Get updates using long polling (dev/testing only)
|
|
219
|
-
* Note: Zalo returns a single update per call, not an array like Telegram
|
|
220
|
-
*/
|
|
221
|
-
export async function getUpdates(
|
|
222
|
-
token: string,
|
|
223
|
-
params?: ZaloGetUpdatesParams,
|
|
224
|
-
fetcher?: ZaloFetch,
|
|
225
|
-
): Promise<ZaloApiResponse<ZaloUpdate>> {
|
|
226
|
-
const pollTimeoutSec = params?.timeout ?? 30;
|
|
227
|
-
const timeoutMs = (pollTimeoutSec + 5) * 1000;
|
|
228
|
-
const body = { timeout: String(pollTimeoutSec) };
|
|
229
|
-
return callZaloApi<ZaloUpdate>("getUpdates", token, body, { timeoutMs, fetch: fetcher });
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Set webhook URL for receiving updates
|
|
234
|
-
*/
|
|
235
|
-
export async function setWebhook(
|
|
236
|
-
token: string,
|
|
237
|
-
params: ZaloSetWebhookParams,
|
|
238
|
-
fetcher?: ZaloFetch,
|
|
239
|
-
): Promise<ZaloApiResponse<ZaloWebhookInfo>> {
|
|
240
|
-
return callZaloApi<ZaloWebhookInfo>("setWebhook", token, params, { fetch: fetcher });
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Delete webhook configuration
|
|
245
|
-
*/
|
|
246
|
-
export async function deleteWebhook(
|
|
247
|
-
token: string,
|
|
248
|
-
fetcher?: ZaloFetch,
|
|
249
|
-
timeoutMs?: number,
|
|
250
|
-
): Promise<ZaloApiResponse<ZaloWebhookInfo>> {
|
|
251
|
-
return callZaloApi<ZaloWebhookInfo>("deleteWebhook", token, undefined, {
|
|
252
|
-
timeoutMs,
|
|
253
|
-
fetch: fetcher,
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Get current webhook info
|
|
259
|
-
*/
|
|
260
|
-
export async function getWebhookInfo(
|
|
261
|
-
token: string,
|
|
262
|
-
fetcher?: ZaloFetch,
|
|
263
|
-
): Promise<ZaloApiResponse<ZaloWebhookInfo>> {
|
|
264
|
-
return callZaloApi<ZaloWebhookInfo>("getWebhookInfo", token, undefined, { fetch: fetcher });
|
|
265
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { zaloApprovalAuth } from "./approval-auth.js";
|
|
3
|
-
|
|
4
|
-
describe("zaloApprovalAuth", () => {
|
|
5
|
-
it("authorizes numeric Zalo user ids", () => {
|
|
6
|
-
const cfg = { channels: { zalo: { allowFrom: ["zl:123"] } } };
|
|
7
|
-
|
|
8
|
-
expect(
|
|
9
|
-
zaloApprovalAuth.authorizeActorAction({
|
|
10
|
-
cfg,
|
|
11
|
-
senderId: "123",
|
|
12
|
-
action: "approve",
|
|
13
|
-
approvalKind: "exec",
|
|
14
|
-
}),
|
|
15
|
-
).toEqual({ authorized: true });
|
|
16
|
-
});
|
|
17
|
-
});
|
package/src/approval-auth.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createResolvedApproverActionAuthAdapter,
|
|
3
|
-
resolveApprovalApprovers,
|
|
4
|
-
} from "klaw/plugin-sdk/approval-auth-runtime";
|
|
5
|
-
import { resolveZaloAccount } from "./accounts.js";
|
|
6
|
-
|
|
7
|
-
function normalizeZaloApproverId(value: string | number): string | undefined {
|
|
8
|
-
const normalized = String(value)
|
|
9
|
-
.trim()
|
|
10
|
-
.replace(/^(zalo|zl):/i, "")
|
|
11
|
-
.trim();
|
|
12
|
-
return /^\d+$/.test(normalized) ? normalized : undefined;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const zaloApprovalAuth = createResolvedApproverActionAuthAdapter({
|
|
16
|
-
channelLabel: "Zalo",
|
|
17
|
-
resolveApprovers: ({ cfg, accountId }) => {
|
|
18
|
-
const account = resolveZaloAccount({ cfg, accountId }).config;
|
|
19
|
-
return resolveApprovalApprovers({
|
|
20
|
-
allowFrom: account.allowFrom,
|
|
21
|
-
normalizeApprover: normalizeZaloApproverId,
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
normalizeSenderId: (value) => normalizeZaloApproverId(value),
|
|
25
|
-
});
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createDirectoryTestRuntime,
|
|
3
|
-
expectDirectorySurface,
|
|
4
|
-
} from "klaw/plugin-sdk/channel-test-helpers";
|
|
5
|
-
import { describe, expect, it } from "vitest";
|
|
6
|
-
import type { KlawConfig, RuntimeEnv } from "../runtime-api.js";
|
|
7
|
-
import { zaloPlugin } from "./channel.js";
|
|
8
|
-
|
|
9
|
-
describe("zalo directory", () => {
|
|
10
|
-
const runtimeEnv = createDirectoryTestRuntime() as RuntimeEnv;
|
|
11
|
-
const directory = expectDirectorySurface(zaloPlugin.directory);
|
|
12
|
-
|
|
13
|
-
async function expectPeersFromAllowFrom(allowFrom: string[]) {
|
|
14
|
-
const cfg = {
|
|
15
|
-
channels: {
|
|
16
|
-
zalo: {
|
|
17
|
-
allowFrom,
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
} as unknown as KlawConfig;
|
|
21
|
-
|
|
22
|
-
const peers = await directory.listPeers({
|
|
23
|
-
cfg,
|
|
24
|
-
accountId: undefined,
|
|
25
|
-
query: undefined,
|
|
26
|
-
limit: undefined,
|
|
27
|
-
runtime: runtimeEnv,
|
|
28
|
-
});
|
|
29
|
-
expect(peers).toStrictEqual([
|
|
30
|
-
{ kind: "user", id: "123" },
|
|
31
|
-
{ kind: "user", id: "234" },
|
|
32
|
-
{ kind: "user", id: "345" },
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
|
-
await expect(
|
|
36
|
-
directory.listGroups({
|
|
37
|
-
cfg,
|
|
38
|
-
accountId: undefined,
|
|
39
|
-
query: undefined,
|
|
40
|
-
limit: undefined,
|
|
41
|
-
runtime: runtimeEnv,
|
|
42
|
-
}),
|
|
43
|
-
).resolves.toStrictEqual([]);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
it("lists peers from allowFrom", async () => {
|
|
47
|
-
await expectPeersFromAllowFrom(["zalo:123", "zl:234", "345"]);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("normalizes spaced zalo prefixes in allowFrom and pairing entries", async () => {
|
|
51
|
-
await expectPeersFromAllowFrom([" zalo:123 ", " zl:234 ", " 345 "]);
|
|
52
|
-
|
|
53
|
-
expect(zaloPlugin.pairing?.normalizeAllowEntry?.(" zalo:123 ")).toBe("123");
|
|
54
|
-
expect(zaloPlugin.messaging?.normalizeTarget?.(" zl:234 ")).toBe("234");
|
|
55
|
-
});
|
|
56
|
-
});
|
package/src/channel.runtime.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { createAccountStatusSink } from "klaw/plugin-sdk/channel-lifecycle";
|
|
2
|
-
import { probeZalo } from "./probe.js";
|
|
3
|
-
import { resolveZaloProxyFetch } from "./proxy.js";
|
|
4
|
-
import { PAIRING_APPROVED_MESSAGE, type ChannelPlugin, type KlawConfig } from "./runtime-api.js";
|
|
5
|
-
import { normalizeSecretInputString } from "./secret-input.js";
|
|
6
|
-
import { sendMessageZalo } from "./send.js";
|
|
7
|
-
import type { ResolvedZaloAccount } from "./types.js";
|
|
8
|
-
|
|
9
|
-
export async function notifyZaloPairingApproval(params: { cfg: KlawConfig; id: string }) {
|
|
10
|
-
const { resolveZaloAccount } = await import("./accounts.js");
|
|
11
|
-
const account = resolveZaloAccount({ cfg: params.cfg });
|
|
12
|
-
if (!account.token) {
|
|
13
|
-
throw new Error("Zalo token not configured");
|
|
14
|
-
}
|
|
15
|
-
await sendMessageZalo(params.id, PAIRING_APPROVED_MESSAGE, {
|
|
16
|
-
token: account.token,
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export async function sendZaloText(
|
|
21
|
-
params: Parameters<typeof sendMessageZalo>[2] & {
|
|
22
|
-
to: string;
|
|
23
|
-
text: string;
|
|
24
|
-
},
|
|
25
|
-
) {
|
|
26
|
-
return await sendMessageZalo(params.to, params.text, params);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function probeZaloAccount(params: {
|
|
30
|
-
account: import("./accounts.js").ResolvedZaloAccount;
|
|
31
|
-
timeoutMs?: number;
|
|
32
|
-
}) {
|
|
33
|
-
return await probeZalo(
|
|
34
|
-
params.account.token,
|
|
35
|
-
params.timeoutMs,
|
|
36
|
-
resolveZaloProxyFetch(params.account.config.proxy),
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export async function startZaloGatewayAccount(
|
|
41
|
-
ctx: Parameters<
|
|
42
|
-
NonNullable<NonNullable<ChannelPlugin<ResolvedZaloAccount>["gateway"]>["startAccount"]>
|
|
43
|
-
>[0],
|
|
44
|
-
) {
|
|
45
|
-
const account = ctx.account;
|
|
46
|
-
const token = account.token.trim();
|
|
47
|
-
const mode = account.config.webhookUrl ? "webhook" : "polling";
|
|
48
|
-
let zaloBotLabel = "";
|
|
49
|
-
const fetcher = resolveZaloProxyFetch(account.config.proxy);
|
|
50
|
-
try {
|
|
51
|
-
const probe = await probeZalo(token, 2500, fetcher);
|
|
52
|
-
const name = probe.ok ? probe.bot?.name?.trim() : null;
|
|
53
|
-
if (name) {
|
|
54
|
-
zaloBotLabel = ` (${name})`;
|
|
55
|
-
}
|
|
56
|
-
if (!probe.ok) {
|
|
57
|
-
ctx.log?.warn?.(
|
|
58
|
-
`[${account.accountId}] Zalo probe failed before provider start (${String(probe.elapsedMs)}ms): ${probe.error}`,
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
ctx.setStatus({
|
|
62
|
-
accountId: account.accountId,
|
|
63
|
-
bot: probe.bot,
|
|
64
|
-
});
|
|
65
|
-
} catch (err) {
|
|
66
|
-
ctx.log?.warn?.(
|
|
67
|
-
`[${account.accountId}] Zalo probe threw before provider start: ${err instanceof Error ? (err.stack ?? err.message) : String(err)}`,
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
const statusSink = createAccountStatusSink({
|
|
71
|
-
accountId: ctx.accountId,
|
|
72
|
-
setStatus: ctx.setStatus,
|
|
73
|
-
});
|
|
74
|
-
ctx.log?.info(`[${account.accountId}] starting provider${zaloBotLabel} mode=${mode}`);
|
|
75
|
-
const { monitorZaloProvider } = await import("./monitor.js");
|
|
76
|
-
return monitorZaloProvider({
|
|
77
|
-
token,
|
|
78
|
-
account,
|
|
79
|
-
config: ctx.cfg,
|
|
80
|
-
runtime: ctx.runtime,
|
|
81
|
-
abortSignal: ctx.abortSignal,
|
|
82
|
-
useWebhook: Boolean(account.config.webhookUrl),
|
|
83
|
-
webhookUrl: account.config.webhookUrl,
|
|
84
|
-
webhookSecret: normalizeSecretInputString(account.config.webhookSecret),
|
|
85
|
-
webhookPath: account.config.webhookPath,
|
|
86
|
-
fetcher,
|
|
87
|
-
statusSink,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
expectLifecyclePatch,
|
|
3
|
-
expectPendingUntilAbort,
|
|
4
|
-
startAccountAndTrackLifecycle,
|
|
5
|
-
waitForStartedMocks,
|
|
6
|
-
} from "klaw/plugin-sdk/channel-test-helpers";
|
|
7
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
8
|
-
import type { ResolvedZaloAccount } from "./accounts.js";
|
|
9
|
-
|
|
10
|
-
const hoisted = vi.hoisted(() => ({
|
|
11
|
-
monitorZaloProvider: vi.fn(),
|
|
12
|
-
probeZalo: vi.fn(async () => ({
|
|
13
|
-
ok: false as const,
|
|
14
|
-
error: "probe failed",
|
|
15
|
-
elapsedMs: 1,
|
|
16
|
-
})),
|
|
17
|
-
}));
|
|
18
|
-
|
|
19
|
-
vi.mock("./monitor.js", () => {
|
|
20
|
-
return {
|
|
21
|
-
monitorZaloProvider: hoisted.monitorZaloProvider,
|
|
22
|
-
};
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
vi.mock("./probe.js", () => {
|
|
26
|
-
return {
|
|
27
|
-
probeZalo: hoisted.probeZalo,
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
vi.mock("./channel.runtime.js", () => ({
|
|
32
|
-
probeZaloAccount: hoisted.probeZalo,
|
|
33
|
-
startZaloGatewayAccount: async (ctx: {
|
|
34
|
-
account: ResolvedZaloAccount;
|
|
35
|
-
abortSignal: AbortSignal;
|
|
36
|
-
setStatus: (patch: Partial<ResolvedZaloAccount>) => void;
|
|
37
|
-
}) => {
|
|
38
|
-
await hoisted.probeZalo();
|
|
39
|
-
ctx.setStatus({ accountId: ctx.account.accountId });
|
|
40
|
-
return await hoisted.monitorZaloProvider({
|
|
41
|
-
token: ctx.account.token,
|
|
42
|
-
account: ctx.account,
|
|
43
|
-
abortSignal: ctx.abortSignal,
|
|
44
|
-
useWebhook: false,
|
|
45
|
-
});
|
|
46
|
-
},
|
|
47
|
-
}));
|
|
48
|
-
|
|
49
|
-
import { zaloPlugin } from "./channel.js";
|
|
50
|
-
|
|
51
|
-
type ZaloGateway = NonNullable<typeof zaloPlugin.gateway>;
|
|
52
|
-
type ZaloStartAccount = NonNullable<ZaloGateway["startAccount"]>;
|
|
53
|
-
|
|
54
|
-
function requireStartAccount(): ZaloStartAccount {
|
|
55
|
-
const startAccount = zaloPlugin.gateway?.startAccount;
|
|
56
|
-
if (!startAccount) {
|
|
57
|
-
throw new Error("Expected Zalo gateway startAccount");
|
|
58
|
-
}
|
|
59
|
-
return startAccount;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function buildAccount(): ResolvedZaloAccount {
|
|
63
|
-
return {
|
|
64
|
-
accountId: "default",
|
|
65
|
-
enabled: true,
|
|
66
|
-
token: "test-token",
|
|
67
|
-
tokenSource: "config",
|
|
68
|
-
config: {},
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function requireMonitorArgs() {
|
|
73
|
-
const [call] = hoisted.monitorZaloProvider.mock.calls;
|
|
74
|
-
if (!call) {
|
|
75
|
-
throw new Error("expected Zalo monitor call");
|
|
76
|
-
}
|
|
77
|
-
const [monitorArgs] = call;
|
|
78
|
-
return monitorArgs;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
describe("zaloPlugin gateway.startAccount", () => {
|
|
82
|
-
afterEach(() => {
|
|
83
|
-
vi.clearAllMocks();
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("keeps startAccount pending until abort", async () => {
|
|
87
|
-
hoisted.monitorZaloProvider.mockImplementationOnce(
|
|
88
|
-
async ({ abortSignal }: { abortSignal: AbortSignal }) =>
|
|
89
|
-
await new Promise<void>((resolve) => {
|
|
90
|
-
if (abortSignal.aborted) {
|
|
91
|
-
resolve();
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
abortSignal.addEventListener("abort", () => resolve(), { once: true });
|
|
95
|
-
}),
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
const { abort, patches, task, isSettled } = startAccountAndTrackLifecycle({
|
|
99
|
-
startAccount: requireStartAccount(),
|
|
100
|
-
account: buildAccount(),
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
await expectPendingUntilAbort({
|
|
104
|
-
waitForStarted: waitForStartedMocks(hoisted.probeZalo, hoisted.monitorZaloProvider),
|
|
105
|
-
isSettled,
|
|
106
|
-
abort,
|
|
107
|
-
task,
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
expectLifecyclePatch(patches, { accountId: "default" });
|
|
111
|
-
expect(isSettled()).toBe(true);
|
|
112
|
-
expect(hoisted.monitorZaloProvider).toHaveBeenCalledTimes(1);
|
|
113
|
-
const monitorArgs = requireMonitorArgs();
|
|
114
|
-
expect(monitorArgs).toStrictEqual({
|
|
115
|
-
token: "test-token",
|
|
116
|
-
account: buildAccount(),
|
|
117
|
-
abortSignal: abort.signal,
|
|
118
|
-
useWebhook: false,
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
});
|