fix(telegram): avoid awaiting long-running polling start
This commit is contained in:
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user