{"record":{"id":"d7c5db132e450ec6","repo":"DIYgod/RSSHub","slug":"cannot-start-tg-err","errorCode":null,"errorMessage":"Cannot start TG: ${err}","messagePattern":"Cannot start TG: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":503,"severity":"critical","filePath":"lib/routes/telegram/tglib/client.ts","lineNumber":39,"sourceCode":"        connectionRetries: Infinity,\n        autoReconnect: true,\n        retryDelay: 3000,\n        maxConcurrentDownloads: Number(config.telegram.maxConcurrentDownloads ?? 10),\n        proxy:\n            config.telegram.proxy?.host && config.telegram.proxy.port && config.telegram.proxy.secret\n                ? {\n                      ip: config.telegram.proxy.host,\n                      port: Number(config.telegram.proxy.port),\n                      MTProxy: true,\n                      secret: config.telegram.proxy.secret,\n                  }\n                : undefined,\n    });\n\n    await client.start(\n        Object.assign(authParams ?? {}, {\n            onError: (err: Error) => {\n                throw new Error('Cannot start TG: ' + err);\n            },\n        }) as any\n    );\n    return client;\n}\n\nexport function getFilename(x: Api.TypeMessageMedia) {\n    if (x instanceof Api.MessageMediaDocument) {\n        for (const a of (x.document as Api.Document).attributes) {\n            if (a instanceof Api.DocumentAttributeFilename) {\n                return a.fileName;\n            }\n        }\n    }\n    return x.className;\n}\n\nexport function getDocument(m: Api.TypeMessageMedia) {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/routes/telegram/tglib/client.ts#L21-L57","documentation":"getClient calls client.start(...) with an onError callback that re-throws the underlying Telegram client start error wrapped as 'Cannot start TG: <err>'. The wrapped error is whatever caused MTProto connection/authentication to fail: bad session, auth challenge, proxy failure, or network error.","triggerScenarios":"client.start tries to connect to Telegram over the configured (optional) MTProxy or direct connection; the start promise rejects because the session is invalid, the API credentials are wrong, the proxy is unreachable, or Telegram rate-limits the connection.","commonSituations":"TELEGRAM_SESSION string is malformed or logged-out; TELEGRAM_API_ID/API_HASH mismatch; a configured MTProxy (config.telegram.proxy) is down; floodwait/TooManyRequests from repeated connect attempts; connectionRetries:Infinity amplifying a flapping network.","solutions":["Read the wrapped err string to identify the root cause (auth_key not found, SESSION_REVOKED, FLOOD_WAIT, proxy timeout) before changing anything.","Regenerate TELEGRAM_SESSION and verify TELEGRAM_API_ID/TELEGRAM_API_HASH are correct for your app.","If using config.telegram.proxy, confirm the proxy host/port/secret are reachable and valid; test without the proxy first.","Reduce retry pressure: the connectionRetries:Infinity setting can turn a transient error into a floodwait; retry the whole feed fetch after a delay instead of relying on infinite MTProto retries."],"exampleFix":"// before\nawait client.start(\n    Object.assign(authParams ?? {}, {\n        onError: (err: Error) => {\n            throw new Error('Cannot start TG: ' + err);\n        },\n    }) as any\n);\n\n// after: preserve the original error type for upstream handling\nawait client.start(\n    Object.assign(authParams ?? {}, {\n        onError: (err: Error) => {\n            const e = new Error('Cannot start TG: ' + err.message, { cause: err });\n            e.name = err.name || 'TelegramStartError';\n            throw e;\n        },\n    }) as any\n);","handlingStrategy":"try-catch","validationCode":"import { config } from '@/config';\nfunction canStartTelegram(): boolean {\n    return Boolean(config.telegram?.session) && Boolean(config.telegram?.apiId) && Boolean(config.telegram?.apiHash);\n}\n// do not call getClient() until canStartTelegram() is true","typeGuard":"function telegramStartConfigComplete(): boolean {\n    const t = config.telegram as { session?: string; apiId?: string | number; apiHash?: string } | undefined;\n    return Boolean(t?.session && t?.apiHash);\n}","tryCatchPattern":"try {\n    return await getClient(authParams);\n} catch (e) {\n    if (e instanceof Error && /Cannot start TG:/.test(e.message)) {\n        const cause = e.message;\n        if (/FLOOD_WAIT/i.test(cause)) {\n            await sleep(60_000);\n            return await getClient(authParams);\n        }\n        if (/SESSION_REVOKED|AUTH_KEY/i.test(cause)) {\n            throw new Error('TELEGRAM_SESSION is invalid or revoked; regenerate it.');\n        }\n        throw e;\n    }\n    throw e;\n}","preventionTips":["Verify TELEGRAM_SESSION, TELEGRAM_API_ID, and TELEGRAM_API_HASH together before starting.","If using a proxy, test connectivity to it before relying on client.start.","Avoid connectionRetries:Infinity for flapping networks; it can trigger floodwaits.","Preserve the original error (Error cause) so the wrapped message is diagnosable."],"tags":["telegram","auth","network","config"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}