Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/channels/telegram/src/TelegramAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -281,6 +285,14 @@ export class TelegramChannel extends ChannelBase {
private typingIntervals = new Map<string, ReturnType<typeof setInterval>>();
private activeTypingSessions = new Map<string, Set<string>>();

/**
* 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<string, string>();

private sendTyping(chatId: string): void {
try {
void this.bot.api.sendChatAction(chatId, 'typing').catch(() => {});
Expand Down Expand Up @@ -342,7 +354,7 @@ export class TelegramChannel extends ChannelBase {
}

async sendMessage(chatId: string, text: string): Promise<void> {
await this.sendTelegramMessage(chatId, text);
await this.sendTelegramMessage(chatId, text, this.chatThreadMap.get(chatId));
}

protected override async pushProactive(
Expand Down
Loading