From dd282120517ed5d9b1a26c0e6831e5e60aee4be9 Mon Sep 17 00:00:00 2001 From: tranvantruonggit Date: Thu, 23 Jul 2026 21:58:07 +0700 Subject: [PATCH] fix(channels): route Telegram replies to the correct forum topic sendMessage() now looks up the cached threadId from chatThreadMap (populated from inbound envelopes) and passes it to sendTelegramMessage, so bot replies appear in the same Telegram forum topic the user messaged from instead of always landing in #general. Closes #7609 --- packages/channels/telegram/src/TelegramAdapter.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/channels/telegram/src/TelegramAdapter.ts b/packages/channels/telegram/src/TelegramAdapter.ts index afb127459eb0..629f0bc6fe75 100644 --- a/packages/channels/telegram/src/TelegramAdapter.ts +++ b/packages/channels/telegram/src/TelegramAdapter.ts @@ -100,6 +100,7 @@ export class TelegramChannel extends ChannelBase { const envelope = this.buildEnvelope(msg, text, msg.entities); + if (envelope.threadId) this.chatThreadMap.set(envelope.chatId, envelope.threadId); // Don't await — long prompts would block the update loop this.handleInbound(envelope).catch((err) => { process.stderr.write( @@ -138,6 +139,7 @@ export class TelegramChannel extends ChannelBase { ); } + if (envelope.threadId) this.chatThreadMap.set(envelope.chatId, envelope.threadId); this.handleInbound(envelope).catch((err) => { process.stderr.write( `[Telegram:${this.name}] Error handling message: ${err}\n`, @@ -191,6 +193,7 @@ export class TelegramChannel extends ChannelBase { `\n\n(User sent a file "${fileName}" but download failed)`; } + if (envelope.threadId) this.chatThreadMap.set(envelope.chatId, envelope.threadId); this.handleInbound(envelope).catch((err) => { process.stderr.write( `[Telegram:${this.name}] Error handling message: ${err}\n`, @@ -244,6 +247,7 @@ export class TelegramChannel extends ChannelBase { `\n\n(User sent a voice message but download failed)`; } + if (envelope.threadId) this.chatThreadMap.set(envelope.chatId, envelope.threadId); this.handleInbound(envelope).catch((err) => { process.stderr.write( `[Telegram:${this.name}] Error handling message: ${err}\n`, @@ -281,6 +285,14 @@ export class TelegramChannel extends ChannelBase { private typingIntervals = new Map>(); private activeTypingSessions = new Map>(); + /** + * Cache of the most recent threadId per chatId, populated from inbound + * envelopes. Used by {@link sendMessage} to route replies back to the + * correct Telegram forum topic when {@link ChannelBase} does not supply + * a threadId. + */ + private chatThreadMap = new Map(); + private sendTyping(chatId: string): void { try { void this.bot.api.sendChatAction(chatId, 'typing').catch(() => {}); @@ -342,7 +354,7 @@ export class TelegramChannel extends ChannelBase { } async sendMessage(chatId: string, text: string): Promise { - await this.sendTelegramMessage(chatId, text); + await this.sendTelegramMessage(chatId, text, this.chatThreadMap.get(chatId)); } protected override async pushProactive(