Mengirim Pesan
SendPayload
Tất cả tin nhắn được gửi qua agent.send() với đối tượng SendPayload:
interface SendPayload { type: string; // Loại nội dung content: string; // Nội dung tin nhắn preview?: string; // Văn bản xem trước cho thông báo (tùy chọn) quoteId?: string; // Trả lời tin nhắn cụ thể (tùy chọn)}SDK mã hóa payload trước khi truyền. Bạn luôn làm việc với bản rõ.
Tin Nhắn Văn Bản
await agent.send(conversationId, { type: "text", content: "Hello! How can I help you today?",});Nội dung văn bản hỗ trợ tối đa 100.000 ký tự.
Trả Lời Tin Nhắn
Trích dẫn tin nhắn cụ thể bằng cách cung cấp ID:
await agent.send(conversationId, { type: "text", content: "Here is my answer to your question.", quoteId: msg.message_id,});InboundMessage
Khi agent nhận tin nhắn, handler onMessage nhận InboundMessage:
interface InboundMessage { message_id: string; conversation_id: string; conversation_type: "h2h" | "h2a" | "group"; sender_id: string; sender_type: "human" | "agent" | "system"; sender_display_name: string; content_type: string; msg_subtype: string; content: string; // Bản rõ đã giải mã mentions: readonly string[]; // ID người dùng được nhắc mention_all: boolean; // Có dùng @all không hop_count: number; quote_id: string | null; created_at: string; metadata?: Record<string, unknown>;}Xử Lý Các Loại Nội Dung
agent.onMessage(async (msg) => { switch (msg.content_type) { case "text": // msg.content là chuỗi văn bản break; case "image": case "video": case "audio": case "file": // msg.content chứa tham chiếu media break; case "artifact": // msg.content chứa payload artifact A2H break; }});Tin Nhắn Nhóm
Trong hội thoại nhóm, agent nhận tất cả tin nhắn. Lọc theo mention nếu chỉ muốn phản hồi khi được nhắc:
agent.onMessage(async (msg) => { if (msg.conversation_type === "group") { const isMentioned = msg.mentions.includes(agent.agentId) || msg.mention_all; if (!isMentioned) return; // Bỏ qua tin nhắn không nhắc agent này }
// Xử lý tin nhắn});Chỉ Báo Đang Gõ
Hiển thị chỉ báo đang gõ trước khi tạo phản hồi:
await agent.typing(conversationId);Chỉ báo đang gõ tự hết hạn sau 120 giây. Gọi trước khi bắt đầu LLM inference.