Skip to content

Commit d84ef0a

Browse files
committed
fix(zalo): accept hex-like chat_id values in targetResolver
The Zalo Bot API accepts both numeric and hex-like chat_id values. However, targetResolver.looksLikeId was using isNumericTargetId, which only accepted 3+ digit strings. This caused the CLI to reject valid hex chat IDs with 'Unknown target'. - Replace isNumericTargetId with isZaloTargetId in the Zalo plugin - isZaloTargetId accepts numeric (3+ digits) and hex-like (16+ chars) - Add unit tests for the new validation Fixes #57594
1 parent 153a2ba commit d84ef0a

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from "vitest";
2+
import { zaloPlugin } from "./channel.js";
3+
4+
describe("zalo targetResolver", () => {
5+
const looksLikeId = zaloPlugin.messaging?.targetResolver?.looksLikeId;
6+
7+
it("accepts numeric chat_id (3+ digits)", () => {
8+
expect(looksLikeId?.("123456")).toBe(true);
9+
expect(looksLikeId?.("1234567890")).toBe(true);
10+
});
11+
12+
it("accepts hex-like chat_id (16+ hex chars)", () => {
13+
expect(looksLikeId?.("5a0b1c2d3e4f5a6b")).toBe(true);
14+
expect(looksLikeId?.("5A0B1C2D3E4F5A6B")).toBe(true);
15+
expect(looksLikeId?.("a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6")).toBe(true);
16+
});
17+
18+
it("rejects too short numeric strings", () => {
19+
expect(looksLikeId?.("12")).toBe(false);
20+
expect(looksLikeId?.("1")).toBe(false);
21+
});
22+
23+
it("rejects too short hex strings", () => {
24+
expect(looksLikeId?.("abc123")).toBe(false);
25+
expect(looksLikeId?.("a1b2c3d4")).toBe(false);
26+
});
27+
28+
it("rejects invalid characters", () => {
29+
expect(looksLikeId?.("hello")).toBe(false);
30+
expect(looksLikeId?.("zalo:123")).toBe(false);
31+
expect(looksLikeId?.("")).toBe(false);
32+
});
33+
34+
it("trims whitespace before checking", () => {
35+
expect(looksLikeId?.(" 123456 ")).toBe(true);
36+
expect(looksLikeId?.(" 5a0b1c2d3e4f5a6b ")).toBe(true);
37+
});
38+
});

extensions/zalo/src/channel.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ import { createStaticReplyToModeResolver } from "openclaw/plugin-sdk/conversatio
3030
import { createChannelDirectoryAdapter } from "openclaw/plugin-sdk/directory-runtime";
3131
import { listResolvedDirectoryUserEntriesFromAllowFrom } from "openclaw/plugin-sdk/directory-runtime";
3232
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
33-
import {
34-
isNumericTargetId,
35-
sendPayloadWithChunkedTextAndMedia,
36-
} from "openclaw/plugin-sdk/reply-payload";
33+
import { sendPayloadWithChunkedTextAndMedia } from "openclaw/plugin-sdk/reply-payload";
3734
import {
3835
createComputedAccountStatusAdapter,
3936
createDefaultChannelRuntimeState,
@@ -74,6 +71,23 @@ function normalizeZaloMessagingTarget(raw: string): string | undefined {
7471
return trimmed.replace(/^(zalo|zl):/i, "").trim();
7572
}
7673

74+
/** Detect Zalo Bot API chat_id values, which may be numeric or hex-like. */
75+
function isZaloTargetId(raw: string): boolean {
76+
const trimmed = raw?.trim();
77+
if (!trimmed) {
78+
return false;
79+
}
80+
// Numeric chat_id (original check)
81+
if (/^\d{3,}$/.test(trimmed)) {
82+
return true;
83+
}
84+
// Hex-like chat_id (e.g. 5a0b1c2d3e4f5a6b)
85+
if (/^[a-f0-9]{16,}$/i.test(trimmed)) {
86+
return true;
87+
}
88+
return false;
89+
}
90+
7791
const loadZaloChannelRuntime = createLazyRuntimeModule(() => import("./channel.runtime.js"));
7892
const zaloSetupWizard = createZaloSetupWizardProxy(
7993
async () => (await import("./setup-surface.js")).zaloSetupWizard,
@@ -234,7 +248,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount, ZaloProbeResult> =
234248
normalizeTarget: normalizeZaloMessagingTarget,
235249
resolveOutboundSessionRoute: (params) => resolveZaloOutboundSessionRoute(params),
236250
targetResolver: {
237-
looksLikeId: isNumericTargetId,
251+
looksLikeId: isZaloTargetId,
238252
hint: "<chatId>",
239253
},
240254
},

0 commit comments

Comments
 (0)