From 682154d9ecbc1d6e91c04e85e03daceb764ccc27 Mon Sep 17 00:00:00 2001 From: William Valentin Date: Thu, 19 Feb 2026 08:55:41 -0800 Subject: [PATCH] fix(telegram): avoid awaiting long-running polling start --- src/channels/telegram/adapter.ts | 39 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/channels/telegram/adapter.ts b/src/channels/telegram/adapter.ts index dd75356..02c8ee7 100644 --- a/src/channels/telegram/adapter.ts +++ b/src/channels/telegram/adapter.ts @@ -463,26 +463,27 @@ export class TelegramAdapter implements ChannelAdapter { // ── Start long polling ── - try { - await bot.start({ - onStart: (botInfo) => { - console.log(`Telegram bot started: @${botInfo.username}`); - this.botInfo = { id: botInfo.id, username: botInfo.username }; - this._status = 'connected'; - this.reconnectAttempt = 0; - this._lastError = undefined; - this._lastErrorAt = undefined; - }, - }); - if (this._status === 'connecting') { - // For mocked/runtime environments where onStart is not surfaced synchronously. + // bot.start() is a long-running method that resolves only when the bot stops. + // Do NOT await it — fire-and-forget so connect() resolves immediately. + bot.start({ + onStart: (botInfo) => { + console.log(`Telegram bot started: @${botInfo.username}`); + this.botInfo = { id: botInfo.id, username: botInfo.username }; this._status = 'connected'; - } - } catch (error) { + this.reconnectAttempt = 0; + this._lastError = undefined; + this._lastErrorAt = undefined; + }, + }).catch((error) => { const description = error instanceof Error ? error.message : String(error); this.recordAdapterError(`Telegram connect failed: ${description}`); this.scheduleReconnect(); - throw error; + }); + + // Set connected optimistically for mocked/runtime environments + // where onStart is not surfaced synchronously. + if (this._status === 'connecting') { + this._status = 'connected'; } } @@ -632,7 +633,7 @@ export class TelegramAdapter implements ChannelAdapter { this._status = 'connecting'; await bot.stop(); - await bot.start({ + bot.start({ onStart: (botInfo) => { console.log(`Telegram bot reconnected: @${botInfo.username}`); this.botInfo = { id: botInfo.id, username: botInfo.username }; @@ -641,6 +642,10 @@ export class TelegramAdapter implements ChannelAdapter { this._lastError = undefined; this._lastErrorAt = undefined; }, + }).catch((error) => { + const description = error instanceof Error ? error.message : String(error); + this.recordAdapterError(`Telegram reconnect polling error: ${description}`); + this.scheduleReconnect(); }); if (this._status === 'connecting') {