Files
Moltbot/extensions/telegram/src/update-offset-store.ts
scoootscooob e5bca0832f refactor: move Telegram channel implementation to extensions/ (#45635)
* refactor: move Telegram channel implementation to extensions/telegram/src/

Move all Telegram channel code (123 files + 10 bot/ files + 8 channel plugin
files) from src/telegram/ and src/channels/plugins/*/telegram.ts to
extensions/telegram/src/. Leave thin re-export shims at original locations so
cross-cutting src/ imports continue to resolve.

- Fix all relative import paths in moved files (../X/ -> ../../../src/X/)
- Fix vi.mock paths in 60 test files
- Fix inline typeof import() expressions
- Update tsconfig.plugin-sdk.dts.json rootDir to "." for cross-directory DTS
- Update write-plugin-sdk-entry-dts.ts for new rootDir structure
- Move channel plugin files with correct path remapping

* fix: support keyed telegram send deps

* fix: sync telegram extension copies with latest main

* fix: correct import paths and remove misplaced files in telegram extension

* fix: sync outbound-adapter with main (add sendTelegramPayloadMessages) and fix delivery.test import path
2026-03-14 02:50:17 -07:00

141 lines
3.8 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { resolveStateDir } from "../../../src/config/paths.js";
import { writeJsonAtomic } from "../../../src/infra/json-files.js";
const STORE_VERSION = 2;
type TelegramUpdateOffsetState = {
version: number;
lastUpdateId: number | null;
botId: string | null;
};
function isValidUpdateId(value: unknown): value is number {
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
}
function normalizeAccountId(accountId?: string) {
const trimmed = accountId?.trim();
if (!trimmed) {
return "default";
}
return trimmed.replace(/[^a-z0-9._-]+/gi, "_");
}
function resolveTelegramUpdateOffsetPath(
accountId?: string,
env: NodeJS.ProcessEnv = process.env,
): string {
const stateDir = resolveStateDir(env, os.homedir);
const normalized = normalizeAccountId(accountId);
return path.join(stateDir, "telegram", `update-offset-${normalized}.json`);
}
function extractBotIdFromToken(token?: string): string | null {
const trimmed = token?.trim();
if (!trimmed) {
return null;
}
const [rawBotId] = trimmed.split(":", 1);
if (!rawBotId || !/^\d+$/.test(rawBotId)) {
return null;
}
return rawBotId;
}
function safeParseState(raw: string): TelegramUpdateOffsetState | null {
try {
const parsed = JSON.parse(raw) as {
version?: number;
lastUpdateId?: number | null;
botId?: string | null;
};
if (parsed?.version !== STORE_VERSION && parsed?.version !== 1) {
return null;
}
if (parsed.lastUpdateId !== null && !isValidUpdateId(parsed.lastUpdateId)) {
return null;
}
if (
parsed.version === STORE_VERSION &&
parsed.botId !== null &&
typeof parsed.botId !== "string"
) {
return null;
}
return {
version: STORE_VERSION,
lastUpdateId: parsed.lastUpdateId ?? null,
botId: parsed.version === STORE_VERSION ? (parsed.botId ?? null) : null,
};
} catch {
return null;
}
}
export async function readTelegramUpdateOffset(params: {
accountId?: string;
botToken?: string;
env?: NodeJS.ProcessEnv;
}): Promise<number | null> {
const filePath = resolveTelegramUpdateOffsetPath(params.accountId, params.env);
try {
const raw = await fs.readFile(filePath, "utf-8");
const parsed = safeParseState(raw);
const expectedBotId = extractBotIdFromToken(params.botToken);
if (expectedBotId && parsed?.botId && parsed.botId !== expectedBotId) {
return null;
}
if (expectedBotId && parsed?.botId === null) {
return null;
}
return parsed?.lastUpdateId ?? null;
} catch (err) {
const code = (err as { code?: string }).code;
if (code === "ENOENT") {
return null;
}
return null;
}
}
export async function writeTelegramUpdateOffset(params: {
accountId?: string;
updateId: number;
botToken?: string;
env?: NodeJS.ProcessEnv;
}): Promise<void> {
if (!isValidUpdateId(params.updateId)) {
throw new Error("Telegram update offset must be a non-negative safe integer.");
}
const filePath = resolveTelegramUpdateOffsetPath(params.accountId, params.env);
const payload: TelegramUpdateOffsetState = {
version: STORE_VERSION,
lastUpdateId: params.updateId,
botId: extractBotIdFromToken(params.botToken),
};
await writeJsonAtomic(filePath, payload, {
mode: 0o600,
trailingNewline: true,
ensureDirMode: 0o700,
});
}
export async function deleteTelegramUpdateOffset(params: {
accountId?: string;
env?: NodeJS.ProcessEnv;
}): Promise<void> {
const filePath = resolveTelegramUpdateOffsetPath(params.accountId, params.env);
try {
await fs.unlink(filePath);
} catch (err) {
const code = (err as { code?: string }).code;
if (code === "ENOENT") {
return;
}
throw err;
}
}