fix: normalize telegram command mentions

This commit is contained in:
Ayaan Zaidi
2026-01-11 21:06:04 +05:30
parent 933c157092
commit 68f6f3f0bd
5 changed files with 65 additions and 9 deletions

View File

@@ -361,7 +361,14 @@ export function buildCommandText(commandName: string, args?: string): string {
return trimmedArgs ? `/${commandName} ${trimmedArgs}` : `/${commandName}`;
}
export function normalizeCommandBody(raw: string): string {
export type CommandNormalizeOptions = {
botUsername?: string;
};
export function normalizeCommandBody(
raw: string,
options?: CommandNormalizeOptions,
): string {
const trimmed = raw.trim();
if (!trimmed.startsWith("/")) return trimmed;
@@ -378,17 +385,26 @@ export function normalizeCommandBody(raw: string): string {
})()
: singleLine;
const lowered = normalized.toLowerCase();
const normalizedBotUsername = options?.botUsername?.trim().toLowerCase();
const mentionMatch = normalizedBotUsername
? normalized.match(/^\/([^\s@]+)@([^\s]+)(.*)$/)
: null;
const commandBody =
mentionMatch && mentionMatch[2].toLowerCase() === normalizedBotUsername
? `/${mentionMatch[1]}${mentionMatch[3] ?? ""}`
: normalized;
const lowered = commandBody.toLowerCase();
const exact = TEXT_ALIAS_MAP.get(lowered);
if (exact) return exact.canonical;
const tokenMatch = normalized.match(/^\/([^\s]+)(?:\s+([\s\S]+))?$/);
if (!tokenMatch) return normalized;
const tokenMatch = commandBody.match(/^\/([^\s]+)(?:\s+([\s\S]+))?$/);
if (!tokenMatch) return commandBody;
const [, token, rest] = tokenMatch;
const tokenKey = `/${token.toLowerCase()}`;
const tokenSpec = TEXT_ALIAS_MAP.get(tokenKey);
if (!tokenSpec) return normalized;
if (rest && !tokenSpec.acceptsArgs) return normalized;
if (!tokenSpec) return commandBody;
if (rest && !tokenSpec.acceptsArgs) return commandBody;
const normalizedRest = rest?.trimStart();
return normalizedRest
? `${tokenSpec.canonical} ${normalizedRest}`