mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 22:01:24 +00:00
refactor(telegram): centralize target parsing
This commit is contained in:
43
src/telegram/targets.ts
Normal file
43
src/telegram/targets.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
export type TelegramTarget = {
|
||||
chatId: string;
|
||||
messageThreadId?: number;
|
||||
};
|
||||
|
||||
export function stripTelegramInternalPrefixes(to: string): string {
|
||||
let trimmed = to.trim();
|
||||
while (true) {
|
||||
const next = trimmed.replace(/^(telegram|tg|group):/i, "").trim();
|
||||
if (next === trimmed) return trimmed;
|
||||
trimmed = next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a Telegram delivery target into chatId and optional topic/thread ID.
|
||||
*
|
||||
* Supported formats:
|
||||
* - `chatId` (plain chat ID, t.me link, @username, or internal prefixes like `telegram:...`)
|
||||
* - `chatId:topicId` (numeric topic/thread ID)
|
||||
* - `chatId:topic:topicId` (explicit topic marker; preferred)
|
||||
*/
|
||||
export function parseTelegramTarget(to: string): TelegramTarget {
|
||||
const normalized = stripTelegramInternalPrefixes(to);
|
||||
|
||||
const topicMatch = /^(.+?):topic:(\d+)$/.exec(normalized);
|
||||
if (topicMatch) {
|
||||
return {
|
||||
chatId: topicMatch[1],
|
||||
messageThreadId: Number.parseInt(topicMatch[2], 10),
|
||||
};
|
||||
}
|
||||
|
||||
const colonMatch = /^(.+):(\d+)$/.exec(normalized);
|
||||
if (colonMatch) {
|
||||
return {
|
||||
chatId: colonMatch[1],
|
||||
messageThreadId: Number.parseInt(colonMatch[2], 10),
|
||||
};
|
||||
}
|
||||
|
||||
return { chatId: normalized };
|
||||
}
|
||||
Reference in New Issue
Block a user