Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: stop Telegram polling after token validation fails

[._.]/ Adam Eivy65e85c25927c
diff --git a/server/services/telegram.js b/server/services/telegram.js
index c59c1b645c..67cc27d10d 100644
--- a/server/services/telegram.js
+++ b/server/services/telegram.js
@@ -79,9 +79,10 @@ export async function init(sendTestMessage = false) {
reconnectionAttempts = 0;
// Validate token
- const me = await bot.getMe().catch(err => {
+ const me = await bot.getMe().catch(async err => {
console.error(`📱 Telegram: invalid token — ${err.message}`);
- bot = null;
+ // Polling starts at construction; stop it before dropping the failed bot.
+ await cleanup();
return null;
});
diff --git a/server/services/telegram.test.js b/server/services/telegram.test.js
index 168290d619..d323877646 100644
--- a/server/services/telegram.test.js
+++ b/server/services/telegram.test.js
@@ -164,6 +164,34 @@ describe('telegram service', () => {
return active;
}
+ it('stops a failed startup before returning and can connect again', async () => {
+ const telegram = await loadTelegramActive();
+ h.getMe.mockRejectedValueOnce(new Error('Temporary validation outage'));
+ let finishShutdown;
+ const shutdown = new Promise(resolve => { finishShutdown = resolve; });
+ h.stopPolling.mockReturnValueOnce(shutdown);
+
+ let initialized = false;
+ const failedInit = telegram.init(false).then(() => { initialized = true; });
+ await vi.waitFor(() => expect(h.stopPolling).toHaveBeenCalledTimes(1));
+ expect(initialized).toBe(false);
+ finishShutdown();
+ await failedInit;
+ expect(telegram.getStatus()).toMatchObject({ connected: false, botUsername: null });
+ expect(h.textHandlers).toHaveLength(0);
+
+ await telegram.init(false);
+ expect(h.stopPolling).toHaveBeenCalledTimes(1);
+ expect(telegram.getStatus()).toMatchObject({ connected: true, botUsername: 'example_bot' });
+ const help = h.textHandlers.find(({ regex }) => regex.test('/help'));
+ await help.fn({ chat: { id: 42 }, text: '/help' });
+ expect(h.sendMessage).toHaveBeenCalledWith('42', expect.stringContaining('PortOS Bot Commands'), { parse_mode: 'HTML' });
+
+ await telegram.cleanup();
+ expect(h.stopPolling).toHaveBeenCalledTimes(2);
+ expect(telegram.getStatus()).toMatchObject({ connected: false, botUsername: null });
+ });
+
describe('rate limiting (token bucket)', () => {
it('sends up to the bucket max then rejects further messages in the same window', async () => {
const telegram = await loadTelegramActive();