mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 13:44:58 +00:00
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:
@@ -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