fix(telegram): avoid awaiting long-running polling start

This commit is contained in:
William Valentin
2026-02-19 08:55:41 -08:00
parent b6c8d8ddf4
commit 682154d9ec
+22 -17
View File
@@ -463,26 +463,27 @@ export class TelegramAdapter implements ChannelAdapter {
// ── Start long polling ── // ── Start long polling ──
try { // bot.start() is a long-running method that resolves only when the bot stops.
await bot.start({ // Do NOT await it — fire-and-forget so connect() resolves immediately.
onStart: (botInfo) => { bot.start({
console.log(`Telegram bot started: @${botInfo.username}`); onStart: (botInfo) => {
this.botInfo = { id: botInfo.id, username: botInfo.username }; console.log(`Telegram bot started: @${botInfo.username}`);
this._status = 'connected'; this.botInfo = { id: botInfo.id, username: botInfo.username };
this.reconnectAttempt = 0;
this._lastError = undefined;
this._lastErrorAt = undefined;
},
});
if (this._status === 'connecting') {
// For mocked/runtime environments where onStart is not surfaced synchronously.
this._status = 'connected'; this._status = 'connected';
} this.reconnectAttempt = 0;
} catch (error) { this._lastError = undefined;
this._lastErrorAt = undefined;
},
}).catch((error) => {
const description = error instanceof Error ? error.message : String(error); const description = error instanceof Error ? error.message : String(error);
this.recordAdapterError(`Telegram connect failed: ${description}`); this.recordAdapterError(`Telegram connect failed: ${description}`);
this.scheduleReconnect(); 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'; this._status = 'connecting';
await bot.stop(); await bot.stop();
await bot.start({ bot.start({
onStart: (botInfo) => { onStart: (botInfo) => {
console.log(`Telegram bot reconnected: @${botInfo.username}`); console.log(`Telegram bot reconnected: @${botInfo.username}`);
this.botInfo = { id: botInfo.id, username: botInfo.username }; this.botInfo = { id: botInfo.id, username: botInfo.username };
@@ -641,6 +642,10 @@ export class TelegramAdapter implements ChannelAdapter {
this._lastError = undefined; this._lastError = undefined;
this._lastErrorAt = 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') { if (this._status === 'connecting') {