mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-14 19:40:40 +00:00
refactor(telegram): centralize target chat type parsing
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { TelegramInlineButtonsScope } from "../config/types.telegram.js";
|
||||
import { listTelegramAccountIds, resolveTelegramAccount } from "./accounts.js";
|
||||
import { parseTelegramTarget } from "./targets.js";
|
||||
|
||||
const DEFAULT_INLINE_BUTTONS_SCOPE: TelegramInlineButtonsScope = "allowlist";
|
||||
|
||||
@@ -65,17 +64,4 @@ export function isTelegramInlineButtonsEnabled(params: {
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveTelegramTargetChatType(target: string): "direct" | "group" | "unknown" {
|
||||
if (!target.trim()) {
|
||||
return "unknown";
|
||||
}
|
||||
const parsed = parseTelegramTarget(target);
|
||||
const chatId = parsed.chatId.trim();
|
||||
if (!chatId) {
|
||||
return "unknown";
|
||||
}
|
||||
if (/^-?\d+$/.test(chatId)) {
|
||||
return chatId.startsWith("-") ? "group" : "direct";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
export { resolveTelegramTargetChatType } from "./targets.js";
|
||||
|
||||
@@ -23,12 +23,14 @@ describe("parseTelegramTarget", () => {
|
||||
it("parses plain chatId", () => {
|
||||
expect(parseTelegramTarget("-1001234567890")).toEqual({
|
||||
chatId: "-1001234567890",
|
||||
chatType: "group",
|
||||
});
|
||||
});
|
||||
|
||||
it("parses @username", () => {
|
||||
expect(parseTelegramTarget("@mychannel")).toEqual({
|
||||
chatId: "@mychannel",
|
||||
chatType: "unknown",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,6 +38,7 @@ describe("parseTelegramTarget", () => {
|
||||
expect(parseTelegramTarget("-1001234567890:123")).toEqual({
|
||||
chatId: "-1001234567890",
|
||||
messageThreadId: 123,
|
||||
chatType: "group",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -43,6 +46,7 @@ describe("parseTelegramTarget", () => {
|
||||
expect(parseTelegramTarget("-1001234567890:topic:456")).toEqual({
|
||||
chatId: "-1001234567890",
|
||||
messageThreadId: 456,
|
||||
chatType: "group",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -50,12 +54,14 @@ describe("parseTelegramTarget", () => {
|
||||
expect(parseTelegramTarget(" -1001234567890:99 ")).toEqual({
|
||||
chatId: "-1001234567890",
|
||||
messageThreadId: 99,
|
||||
chatType: "group",
|
||||
});
|
||||
});
|
||||
|
||||
it("does not treat non-numeric suffix as topicId", () => {
|
||||
expect(parseTelegramTarget("-1001234567890:abc")).toEqual({
|
||||
chatId: "-1001234567890:abc",
|
||||
chatType: "unknown",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -63,6 +69,7 @@ describe("parseTelegramTarget", () => {
|
||||
expect(parseTelegramTarget("telegram:group:-1001234567890:topic:456")).toEqual({
|
||||
chatId: "-1001234567890",
|
||||
messageThreadId: 456,
|
||||
chatType: "group",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export type TelegramTarget = {
|
||||
chatId: string;
|
||||
messageThreadId?: number;
|
||||
chatType: "direct" | "group" | "unknown";
|
||||
};
|
||||
|
||||
export function stripTelegramInternalPrefixes(to: string): string {
|
||||
@@ -33,6 +34,17 @@ export function stripTelegramInternalPrefixes(to: string): string {
|
||||
* - `chatId:topicId` (numeric topic/thread ID)
|
||||
* - `chatId:topic:topicId` (explicit topic marker; preferred)
|
||||
*/
|
||||
function resolveTelegramChatType(chatId: string): "direct" | "group" | "unknown" {
|
||||
const trimmed = chatId.trim();
|
||||
if (!trimmed) {
|
||||
return "unknown";
|
||||
}
|
||||
if (/^-?\d+$/.test(trimmed)) {
|
||||
return trimmed.startsWith("-") ? "group" : "direct";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
export function parseTelegramTarget(to: string): TelegramTarget {
|
||||
const normalized = stripTelegramInternalPrefixes(to);
|
||||
|
||||
@@ -41,6 +53,7 @@ export function parseTelegramTarget(to: string): TelegramTarget {
|
||||
return {
|
||||
chatId: topicMatch[1],
|
||||
messageThreadId: Number.parseInt(topicMatch[2], 10),
|
||||
chatType: resolveTelegramChatType(topicMatch[1]),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,8 +62,16 @@ export function parseTelegramTarget(to: string): TelegramTarget {
|
||||
return {
|
||||
chatId: colonMatch[1],
|
||||
messageThreadId: Number.parseInt(colonMatch[2], 10),
|
||||
chatType: resolveTelegramChatType(colonMatch[1]),
|
||||
};
|
||||
}
|
||||
|
||||
return { chatId: normalized };
|
||||
return {
|
||||
chatId: normalized,
|
||||
chatType: resolveTelegramChatType(normalized),
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveTelegramTargetChatType(target: string): "direct" | "group" | "unknown" {
|
||||
return parseTelegramTarget(target).chatType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user