feat(telegram): improve DM topics support (#30579) (thanks @kesor)

This commit is contained in:
Ayaan Zaidi
2026-03-02 09:06:10 +05:30
committed by Ayaan Zaidi
parent aafc4d56e3
commit c13b35b83d
16 changed files with 335 additions and 44 deletions

View File

@@ -293,7 +293,9 @@ function resolveTelegramSession(
(chatType === "unknown" &&
params.resolvedTarget?.kind &&
params.resolvedTarget.kind !== "user");
const peerId = isGroup ? buildTelegramGroupPeerId(chatId, resolvedThreadId) : chatId;
// For groups: include thread ID in peerId. For DMs: use simple chatId (thread handled via suffix).
const peerId =
isGroup && resolvedThreadId ? buildTelegramGroupPeerId(chatId, resolvedThreadId) : chatId;
const peer: RoutePeer = {
kind: isGroup ? "group" : "direct",
id: peerId,
@@ -305,12 +307,21 @@ function resolveTelegramSession(
accountId: params.accountId,
peer,
});
// Use thread suffix for DM topics to match inbound session key format
const threadKeys =
resolvedThreadId && !isGroup
? { sessionKey: `${baseSessionKey}:thread:${resolvedThreadId}` }
: null;
return {
sessionKey: baseSessionKey,
sessionKey: threadKeys?.sessionKey ?? baseSessionKey,
baseSessionKey,
peer,
chatType: isGroup ? "group" : "direct",
from: isGroup ? `telegram:group:${peerId}` : `telegram:${chatId}`,
from: isGroup
? `telegram:group:${peerId}`
: resolvedThreadId
? `telegram:${chatId}:topic:${resolvedThreadId}`
: `telegram:${chatId}`,
to: `telegram:${chatId}`,
threadId: resolvedThreadId,
};

View File

@@ -925,6 +925,19 @@ describe("resolveOutboundSessionRoute", () => {
threadId: 42,
},
},
{
name: "Telegram DM with topic",
cfg: perChannelPeerCfg,
channel: "telegram",
target: "123456789:topic:99",
expected: {
sessionKey: "agent:main:telegram:direct:123456789:thread:99",
from: "telegram:123456789:topic:99",
to: "telegram:123456789",
threadId: 99,
chatType: "direct",
},
},
{
name: "Telegram unresolved username DM",
cfg: perChannelPeerCfg,