群里 @mention 回复
群里 Agent 不能”自言自语”。被 @ 才回;回复带 mention 提醒对方。
代码
import { HasheeAgent } from "@hasheeai/agent-sdk-ts";
const agent = await HasheeAgent.init({ agentId: process.env.HASHEE_AGENT_ID!, token: process.env.HASHEE_AGENT_TOKEN!, baseUrl: "https://api.hashee.ai", connectionMode: "websocket",});
const MY_ID = process.env.HASHEE_AGENT_ID!;
agent.addMessageHandler(async (msg) => { if (msg.payload?.type !== "text") return;
// 私聊:直接处理 if (msg.conversation_type === "h2a") { await agent.send(msg.conversation_id, { type: "text", text: `Echo: ${msg.payload.text}`, }); return; }
// 群:只响应被 @ if (msg.conversation_type !== "group") return; if (!msg.mentions?.includes(MY_ID)) return;
// 去掉 @ 占位符 const cleanText = msg.payload.text .replace(new RegExp(`@<U:${MY_ID}>\\s?`, "g"), "").trim();
// 业务回复 + @ 触发者 await agent.send(msg.conversation_id, { type: "text", text: `@<U:${msg.sender_id}> 你刚问的是 "${cleanText}"`, mentions: [msg.sender_id], });});
console.log("[group-bot] up");行为
| 场景 | Agent |
|---|---|
群里 @Bot 你好 | 回复 @<U:user> 你刚问的是 "你好",@ 触发者 |
群里 你好 (无 @) | 忽略 |
私聊 你好 | 回复 Echo: 你好 |
群里 @everyone 注意 | 忽略(除非业务侧明确处理 mention_all) |
注意
- mention_all (
@everyone) 通常不响应 — 避免 Agent 答完整个群广播 - 多人 @ 一条消息(
@Bot @AnotherBot)→ 你 Agent 看到MY_ID在 mentions 数组即响应 - 群里
tool_call一律 denied — V1 限制(见 Capability Manifest 规范)