





















@@ -22,6 +22,10 @@ import {
2222} from "./network-errors.js";
2323import { acquireTelegramPollingLease } from "./polling-lease.js";
2424import { makeProxyFetch } from "./proxy.js";
25+import type {
26+TelegramOffsetRotationReason,
27+TelegramUpdateOffsetRotationInfo,
28+} from "./update-offset-store.js";
25292630export type { MonitorTelegramOpts } from "./monitor.types.js";
2731@@ -57,6 +61,21 @@ function normalizePersistedUpdateId(value: number | null): number | null {
5761return value;
5862}
596364+const TELEGRAM_OFFSET_ROTATION_LABELS: Record<TelegramOffsetRotationReason, string> = {
65+"bot-id-changed": "bot identity change",
66+"legacy-state": "legacy update offset",
67+"token-rotated": "token rotation",
68+};
69+70+function formatTelegramOffsetRotationMessage(
71+accountId: string,
72+info: TelegramUpdateOffsetRotationInfo,
73+): string {
74+const previousLabel = info.previousBotId ?? "(legacy unscoped offset)";
75+const reasonLabel = TELEGRAM_OFFSET_ROTATION_LABELS[info.reason];
76+return `[telegram] Detected ${reasonLabel} for account "${accountId}" (was ${previousLabel}, now ${info.currentBotId}); discarding stale update offset ${info.staleLastUpdateId} and starting fresh.`;
77+}
78+6079/** Check if error is a Grammy HttpError (used to scope unhandled rejection handling) */
6180const isGrammyHttpError = (err: unknown): boolean => {
6281if (!err || typeof err !== "object") {
@@ -170,8 +189,8 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
170189}
171190172191const {
173- TelegramOffsetRotationHandler,
174192 TelegramPollingSession,
193+ deleteTelegramUpdateOffset,
175194 readTelegramUpdateOffset,
176195 writeTelegramUpdateOffset,
177196} = await loadTelegramMonitorPollingRuntime();
@@ -204,15 +223,19 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
204223});
205224}
206225207-const rotationHandler = new TelegramOffsetRotationHandler({
208-accountId: account.accountId,
209- log,
210-logError: (line) => (opts.runtime?.error ?? console.error)(line),
211-});
212226const persistedOffsetRaw = await readTelegramUpdateOffset({
213227accountId: account.accountId,
214228botToken: token,
215-onRotationDetected: (info) => rotationHandler.handle(info),
229+onRotationDetected: async (info) => {
230+log(formatTelegramOffsetRotationMessage(account.accountId, info));
231+try {
232+await deleteTelegramUpdateOffset({ accountId: account.accountId });
233+} catch (err) {
234+(opts.runtime?.error ?? console.error)(
235+`telegram: failed to delete stale update offset after rotation: ${String(err)}`,
236+);
237+}
238+},
216239});
217240let lastUpdateId = normalizePersistedUpdateId(persistedOffsetRaw);
218241if (persistedOffsetRaw !== null && lastUpdateId === null) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。