mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-04 22:01:15 +00:00
Add shared normalizeTelegramReplyToMessageId() that rejects non-numeric, NaN, and mixed-content strings before they reach the Telegram Bot API. Apply at all four API sinks: direct send, bot delivery, draft stream, and bot helpers. Prevents GrammyError 400 when non-numeric values from session metadata slip through typed boundaries. Fixes #37222
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
function parseIntegerId(value: string): number | undefined {
|
|
if (!/^-?\d+$/.test(value)) {
|
|
return undefined;
|
|
}
|
|
const parsed = Number.parseInt(value, 10);
|
|
return Number.isFinite(parsed) ? parsed : undefined;
|
|
}
|
|
|
|
export function normalizeTelegramReplyToMessageId(value: unknown): number | undefined {
|
|
if (typeof value === "number") {
|
|
return Number.isFinite(value) ? Math.trunc(value) : undefined;
|
|
}
|
|
if (typeof value !== "string") {
|
|
return undefined;
|
|
}
|
|
const trimmed = value.trim();
|
|
return trimmed ? parseIntegerId(trimmed) : undefined;
|
|
}
|
|
|
|
export function parseTelegramReplyToMessageId(replyToId?: string | null): number | undefined {
|
|
return normalizeTelegramReplyToMessageId(replyToId);
|
|
}
|
|
|
|
export function parseTelegramThreadId(threadId?: string | number | null): number | undefined {
|
|
if (threadId == null) {
|
|
return undefined;
|
|
}
|
|
if (typeof threadId === "number") {
|
|
return Number.isFinite(threadId) ? Math.trunc(threadId) : undefined;
|
|
}
|
|
const trimmed = threadId.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
// DM topic session keys may scope thread ids as "<chatId>:<threadId>".
|
|
const scopedMatch = /^-?\d+:(-?\d+)$/.exec(trimmed);
|
|
const rawThreadId = scopedMatch ? scopedMatch[1] : trimmed;
|
|
return parseIntegerId(rawThreadId);
|
|
}
|