Python
from agent_hub_webhook import Client
c = Client("https://<tunnel>", "bota", "<api_key>")
c.send({"user_id": "u1", "user_name": "an", "group_id": "g1",
"text": "/agent dev status"})
c.on_reply(lambda u: print(u["chat"], u.get("text")))
| |
|---|
Client(base, intg, key) | Base URL (tunnel hoặc local), id integration, API key. |
send | Tự điền event="message" và message_id (UUID4) nếu thiếu. |
on_reply | Vòng pull chặn; ack offset; backoff khi 408 / 429 / 5xx. Truyền offset= để tiếp tục sau restart. |
c = Client("https://<tunnel>", "botb", "<api_key>")
def handle(body):
if body["type"] == "message":
return my_platform_post(body["chat"], body["text"]) # → message_id
return None
c.serve_callback(handle, host="0.0.0.0", port=8790, path="/")
Node 18+
import { Client } from "./agent-hub-webhook.mjs";
const c = new Client({ baseUrl: "https://<tunnel>", integration: "bota", key: "<api_key>" });
await c.send({ user_id: "u1", user_name: "an", group_id: "g1", text: "/agent dev status" });
const h = c.onReply(async (u) => console.log(u.chat, u.text));
process.on("SIGINT", () => h.stop());
await h.done;
export function sign(key, timestamp, body) {
const message = Buffer.concat([Buffer.from(timestamp + "."), Buffer.from(body)]);
return "sha256=" + createHmac("sha256", key).update(message).digest("hex");
}
| |
|---|
onReply → { stop, done } | Trả về ngay; handler có thể async, update sau chờ handler xong. |
done | Reject với WebhookError khi status không retry được (400, 401, 403, 404, 409). Luôn await. |
serveCallback → ready | Reject khi không bind được port. |
Ngữ nghĩa
- Pull là at-least-once: offset chỉ tiến sau khi handler xong — handler phải chịu được lặp.
- Chữ ký sai → 401, handler không được gọi. Handler lỗi → 500.
c.stop() dừng cả pull và receiver; Client đã stop không dùng lại.
Web chat (@agent-hub/chat)
@agent-hub/chat là UI chat nhúng được cho React 19, không gắn cứng vào một transport cụ thể — bạn tự cung cấp một ChatAdapter để nối với backend thật.
import { ChatProvider, ChatWindow } from "@agent-hub/chat";
import "@agent-hub/chat/styles.css";
| |
|---|
ChatProvider / ChatWindow | Hiển thị tin, thread, nút bấm, reply, typing và toast state. |
ChatAdapter | Do bạn truyền vào — package không tự biết cách gửi/nhận tin, chỉ render. |