openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
101 lines (100 loc) • 3.64 kB
JavaScript
import { y as parseStrictNonNegativeInteger } from "./number-coercion-CJQ8TR--.js";
import "./number-runtime-DBLVDypr.js";
//#region extensions/telegram/src/targets.ts
const TELEGRAM_NUMERIC_CHAT_ID_REGEX = /^-?\d+$/;
const TELEGRAM_USERNAME_REGEX = /^[A-Za-z0-9_]{5,}$/i;
function stripTelegramInternalPrefixes(to) {
let trimmed = to.trim();
let strippedTelegramPrefix = false;
while (true) {
const next = (() => {
if (/^(telegram|tg):/i.test(trimmed)) {
strippedTelegramPrefix = true;
return trimmed.replace(/^(telegram|tg):/i, "").trim();
}
if (strippedTelegramPrefix && /^group:/i.test(trimmed)) return trimmed.replace(/^group:/i, "").trim();
return trimmed;
})();
if (next === trimmed) return trimmed;
trimmed = next;
}
}
function normalizeTelegramChatId(raw) {
const stripped = stripTelegramInternalPrefixes(raw);
if (!stripped) return;
if (TELEGRAM_NUMERIC_CHAT_ID_REGEX.test(stripped)) return stripped;
}
function isNumericTelegramChatId(raw) {
return TELEGRAM_NUMERIC_CHAT_ID_REGEX.test(raw.trim());
}
function normalizeTelegramOutboundTarget(raw) {
const trimmed = raw.trim();
const legacyGroupMatch = /^group:(-?\d+(?::topic:\d+|:\d+)?)$/i.exec(trimmed);
if (legacyGroupMatch?.[1]) return legacyGroupMatch[1];
return raw;
}
function normalizeTelegramLookupTarget(raw) {
const stripped = stripTelegramInternalPrefixes(raw);
if (!stripped) return;
if (isNumericTelegramChatId(stripped)) return stripped;
const tmeMatch = /^(?:https?:\/\/)?t\.me\/([A-Za-z0-9_]+)$/i.exec(stripped);
if (tmeMatch?.[1]) return `@${tmeMatch[1]}`;
if (stripped.startsWith("@")) {
const handle = stripped.slice(1);
if (!handle || !TELEGRAM_USERNAME_REGEX.test(handle)) return;
return `@${handle}`;
}
if (TELEGRAM_USERNAME_REGEX.test(stripped)) return `@${stripped}`;
}
/**
* 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)
*/
function resolveTelegramChatType(chatId) {
const trimmed = chatId.trim();
if (!trimmed) return "unknown";
if (isNumericTelegramChatId(trimmed)) return trimmed.startsWith("-") ? "group" : "direct";
return "unknown";
}
function parseTelegramTarget(to) {
const normalized = stripTelegramInternalPrefixes(to);
const topicMatch = /^(.+?):topic:(\d+)$/.exec(normalized);
if (topicMatch) {
const messageThreadId = parseStrictNonNegativeInteger(topicMatch[2]);
if (messageThreadId === void 0) return {
chatId: normalized,
chatType: resolveTelegramChatType(normalized)
};
return {
chatId: topicMatch[1],
messageThreadId,
chatType: resolveTelegramChatType(topicMatch[1])
};
}
const colonMatch = /^(.+):(\d+)$/.exec(normalized);
if (colonMatch) {
const messageThreadId = parseStrictNonNegativeInteger(colonMatch[2]);
if (messageThreadId === void 0) return {
chatId: normalized,
chatType: resolveTelegramChatType(normalized)
};
return {
chatId: colonMatch[1],
messageThreadId,
chatType: resolveTelegramChatType(colonMatch[1])
};
}
return {
chatId: normalized,
chatType: resolveTelegramChatType(normalized)
};
}
function resolveTelegramTargetChatType(target) {
return parseTelegramTarget(target).chatType;
}
//#endregion
export { parseTelegramTarget as a, normalizeTelegramOutboundTarget as i, normalizeTelegramChatId as n, resolveTelegramTargetChatType as o, normalizeTelegramLookupTarget as r, stripTelegramInternalPrefixes as s, isNumericTelegramChatId as t };