From 68e0cc32ba65ef470a2a0401559fef616b656e03 Mon Sep 17 00:00:00 2001 From: xmanrui <841206367@qq.com> Date: Sat, 28 Feb 2026 23:40:43 +0800 Subject: [PATCH] Fix telegram/whatsapp card links to direct sessions --- app/api/config/route.ts | 41 +++++++++++++++++++++++++++++++++++++---- app/page.tsx | 4 ++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/api/config/route.ts b/app/api/config/route.ts index fba794a..3634bf3 100644 --- a/app/api/config/route.ts +++ b/app/api/config/route.ts @@ -203,6 +203,33 @@ function getFeishuUserOpenIds(agentIds: string[], sessionsMap: Map) } return map; } + +function getChannelDirectPeerIds( + agentIds: string[], + sessionsMap: Map, + channel: "telegram" | "whatsapp" +): Record { + const map: Record = {}; + const pattern = new RegExp(`^agent:[^:]+:${channel}:direct:(.+)$`); + for (const agentId of agentIds) { + try { + const sessions = sessionsMap.get(agentId); + if (!sessions) continue; + let best: { peerId: string; updatedAt: number } | null = null; + for (const [key, val] of Object.entries(sessions)) { + const m = key.match(pattern); + if (m) { + const updatedAt = (val as any).updatedAt || 0; + if (!best || updatedAt > best.updatedAt) { + best = { peerId: m[1], updatedAt }; + } + } + } + if (best) map[agentId] = best.peerId; + } catch {} + } + return map; +} // 从 IDENTITY.md 读取机器人名字 function readIdentityName(agentId: string, agentDir?: string, workspace?: string): string | null { const candidates = [ @@ -301,6 +328,8 @@ export async function GET() { // 从预读的 sessions 数据获取飞书用户 open_id const feishuUserOpenIds = getFeishuUserOpenIds(agentIds, sessionsMap); + const telegramDirectPeerIds = getChannelDirectPeerIds(agentIds, sessionsMap, "telegram"); + const whatsappDirectPeerIds = getChannelDirectPeerIds(agentIds, sessionsMap, "whatsapp"); const discordDmAllowFrom = channels.discord?.dm?.allowFrom || []; // Build a set of agent IDs that have explicit feishu bindings @@ -356,10 +385,12 @@ export async function GET() { platforms.push({ name: "discord", ...(botUserId && { botUserId }) }); } if (channels.telegram && channels.telegram.enabled !== false) { - platforms.push({ name: "telegram" }); + const botUserId = telegramDirectPeerIds[id] || null; + platforms.push({ name: "telegram", ...(botUserId && { botUserId }) }); } if (channels.whatsapp && channels.whatsapp.enabled !== false) { - platforms.push({ name: "whatsapp" }); + const botUserId = whatsappDirectPeerIds[id] || null; + platforms.push({ name: "whatsapp", ...(botUserId && { botUserId }) }); } } @@ -375,13 +406,15 @@ export async function GET() { (b: any) => b.agentId === id && b.match?.channel === "telegram" ); if (telegramBinding) { - platforms.push({ name: "telegram" }); + const botUserId = telegramDirectPeerIds[id] || null; + platforms.push({ name: "telegram", ...(botUserId && { botUserId }) }); } const whatsappBinding = bindings.find( (b: any) => b.agentId === id && b.match?.channel === "whatsapp" ); if (whatsappBinding) { - platforms.push({ name: "whatsapp" }); + const botUserId = whatsappDirectPeerIds[id] || null; + platforms.push({ name: "whatsapp", ...(botUserId && { botUserId }) }); } } diff --git a/app/page.tsx b/app/page.tsx index 2fa3639..e9fe45a 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -184,6 +184,10 @@ function PlatformBadge({ platform, agentId, gatewayPort, gatewayToken, t, testRe sessionKey = `agent:${agentId}:feishu:direct:${platform.botOpenId}`; } else if (pName === "discord" && platform.botUserId) { sessionKey = `agent:${agentId}:discord:direct:${platform.botUserId}`; + } else if (pName === "telegram" && platform.botUserId) { + sessionKey = `agent:${agentId}:telegram:direct:${platform.botUserId}`; + } else if (pName === "whatsapp" && platform.botUserId) { + sessionKey = `agent:${agentId}:whatsapp:direct:${platform.botUserId}`; } else { sessionKey = `agent:${agentId}:main`; }