refactor(telegram): simplify send/dispatch/target handling (#17819)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: fcb7aeeca3
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Reviewed-by: @obviyus
This commit is contained in:
Ayaan Zaidi
2026-02-16 14:00:34 +05:30
committed by GitHub
parent 1f607bec49
commit b6a9741ba4
16 changed files with 1125 additions and 1141 deletions

View File

@@ -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;
}