Skip to content

Commit 70c8abd

Browse files
authored
refactor(telegram): keep topic thread mapping plugin-local
* refactor(telegram): keep topic thread mapping plugin-local * fix(telegram): preserve native topic ids for username targets
1 parent 9e2bd8b commit 70c8abd

2 files changed

Lines changed: 85 additions & 8 deletions

File tree

‎extensions/telegram/src/channel.ts‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -553,28 +553,60 @@ function resolveTelegramOutboundSessionRoute(params: {
553553
if (isGroup) {
554554
return baseRoute;
555555
}
556-
const outboundSessionThreadId =
557-
resolvedThreadId !== undefined ? `${chatId}:${resolvedThreadId}` : undefined;
556+
const canonicalThreadId =
557+
resolvedThreadId !== undefined
558+
? buildTelegramCanonicalTopicThreadId({ chatId, topicId: resolvedThreadId })
559+
: undefined;
558560
const route = buildThreadAwareOutboundSessionRoute({
559561
route: baseRoute,
560-
threadId: outboundSessionThreadId,
562+
threadId: canonicalThreadId,
561563
currentSessionKey: params.currentSessionKey,
562564
precedence: ["threadId", "currentSession"],
563565
canRecoverCurrentThread: ({ route }) =>
564566
route.chatType !== "direct" || (params.cfg.session?.dmScope ?? "main") !== "main",
565567
});
566-
const deliveryThreadId =
567-
resolvedThreadId ?? parseTelegramThreadId(route.threadId) ?? route.threadId;
568+
const routeThreadId = resolveTelegramNativeTopicThreadId(route.threadId, resolvedThreadId);
568569
return {
569570
...route,
570-
...(deliveryThreadId !== undefined ? { threadId: deliveryThreadId } : {}),
571+
...(routeThreadId !== undefined ? { threadId: routeThreadId } : {}),
571572
from:
572-
deliveryThreadId !== undefined
573-
? `telegram:${chatId}:topic:${deliveryThreadId}`
573+
routeThreadId !== undefined
574+
? `telegram:${chatId}:topic:${routeThreadId}`
574575
: `telegram:${chatId}`,
575576
};
576577
}
577578

579+
function buildTelegramCanonicalTopicThreadId(params: { chatId: string; topicId: number }): string {
580+
// Core session routing sees one canonical thread id. Telegram topic ids are
581+
// chat-scoped, so direct-topic sessions include the chat id to avoid collisions.
582+
return `${params.chatId}:${params.topicId}`;
583+
}
584+
585+
function resolveTelegramNativeTopicThreadId(
586+
threadId?: string | number,
587+
nativeTopicId?: number,
588+
): string | number | undefined {
589+
if (nativeTopicId !== undefined) {
590+
return nativeTopicId;
591+
}
592+
// Keep the chat-scoped canonical id inside OpenClaw state; translate it back
593+
// only when returning Telegram route metadata used by send/typing paths.
594+
if (threadId === undefined) {
595+
return undefined;
596+
}
597+
const parsedThreadId = parseTelegramThreadId(threadId);
598+
if (parsedThreadId !== undefined) {
599+
return parsedThreadId;
600+
}
601+
if (typeof threadId === "string") {
602+
const canonicalMatch = /:(\d+)$/.exec(threadId.trim());
603+
if (canonicalMatch?.[1]) {
604+
return Number(canonicalMatch[1]);
605+
}
606+
}
607+
return threadId;
608+
}
609+
578610
async function resolveTelegramTargets(params: {
579611
cfg: OpenClawConfig;
580612
accountId?: string | null;

‎extensions/telegram/src/session-route.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ describe("telegram session route", () => {
1414
expect(route?.threadId).toBe(99);
1515
});
1616

17+
it("keeps same direct topic ids distinct across chats", async () => {
18+
const first = await telegramPlugin.messaging?.resolveOutboundSessionRoute?.({
19+
cfg: {},
20+
agentId: "main",
21+
target: "12345:topic:99",
22+
});
23+
const second = await telegramPlugin.messaging?.resolveOutboundSessionRoute?.({
24+
cfg: {},
25+
agentId: "main",
26+
target: "67890:topic:99",
27+
});
28+
29+
expect(first?.sessionKey).toBe("agent:main:main:thread:12345:99");
30+
expect(second?.sessionKey).toBe("agent:main:main:thread:67890:99");
31+
expect(first?.threadId).toBe(99);
32+
expect(second?.threadId).toBe(99);
33+
});
34+
35+
it("returns native topic ids for username direct topic targets", async () => {
36+
const route = await telegramPlugin.messaging?.resolveOutboundSessionRoute?.({
37+
cfg: {},
38+
agentId: "main",
39+
target: "@alice:topic:99",
40+
});
41+
42+
expect(route?.sessionKey).toBe("agent:main:main:thread:@alice:99");
43+
expect(route?.baseSessionKey).toBe("agent:main:main");
44+
expect(route?.threadId).toBe(99);
45+
expect(route?.from).toBe("telegram:@alice:topic:99");
46+
});
47+
1748
it("aligns isolated direct topic sessions with inbound reply routing", async () => {
1849
const route = await telegramPlugin.messaging?.resolveOutboundSessionRoute?.({
1950
cfg: { session: { dmScope: "per-account-channel-peer" } },
@@ -44,6 +75,20 @@ describe("telegram session route", () => {
4475
expect(route?.from).toBe("telegram:12345:topic:99");
4576
});
4677

78+
it("recovers username direct topic thread routes from currentSessionKey", async () => {
79+
const route = await telegramPlugin.messaging?.resolveOutboundSessionRoute?.({
80+
cfg: { session: { dmScope: "per-channel-peer" } },
81+
agentId: "main",
82+
target: "@alice",
83+
currentSessionKey: "agent:main:telegram:direct:@alice:thread:@alice:99",
84+
});
85+
86+
expect(route?.sessionKey).toBe("agent:main:telegram:direct:@alice:thread:@alice:99");
87+
expect(route?.baseSessionKey).toBe("agent:main:telegram:direct:@alice");
88+
expect(route?.threadId).toBe(99);
89+
expect(route?.from).toBe("telegram:@alice:topic:99");
90+
});
91+
4792
it('does not recover currentSessionKey threads for shared dmScope "main" DMs', async () => {
4893
const route = await telegramPlugin.messaging?.resolveOutboundSessionRoute?.({
4994
cfg: {},

0 commit comments

Comments
 (0)