Package Exports
- fca-mmtat
Readme
FCA-MMTAT
Facebook Chat API nâng cao cho Node.js — Được xây dựng dựa trên fca-unofficial với nhiều tính năng mới mạnh mẽ.
███████╗ ██████╗ █████╗ ███╗ ███╗███╗ ███╗████████╗ █████╗ ████████╗
██╔════╝██╔════╝██╔══██╗ ████╗ ████║████╗ ████║╚══██╔══╝██╔══██╗╚══██╔══╝
█████╗ ██║ ███████║█████╗██╔████╔██║██╔████╔██║ ██║ ███████║ ██║
██╔══╝ ██║ ██╔══██║╚════╝██║╚██╔╝██║██║╚██╔╝██║ ██║ ██╔══██║ ██║
██║ ╚██████╗██║ ██║ ██║ ╚═╝ ██║██║ ╚═╝ ██║ ██║ ██║ ██║ ██║
╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝✨ Tính năng mới so với fca-unofficial
| Tính năng | fca-unofficial | FCA-MMTAT |
|---|---|---|
| Rate Limiter tích hợp | ❌ | ✅ |
| Cache user/thread info | ❌ | ✅ |
| Plugin System động | ❌ | ✅ |
| CommandRouter | ❌ | ✅ |
| EventBus nội bộ | ❌ | ✅ |
| AntiSpam | ❌ | ✅ |
| Multi-Account Manager | ❌ | ✅ |
| broadcastMessage | ❌ | ✅ |
| sendMessageWithRetry | ❌ | ✅ |
| Log levels + file log | ❌ | ✅ |
| Banner đẹp | ❌ | ✅ |
| getSystemStats() | ❌ | ✅ |
📦 Cài đặt
npm install fca-mmtat
# hoặc sao chép thư mục trực tiếpYêu cầu: Node.js >= 14.0.0
🚀 Bắt đầu nhanh
const fcaMMTAT = require("fca-mmtat");
const fs = require("fs");
const appState = JSON.parse(fs.readFileSync("appstate.json", "utf8"));
fcaMMTAT({ appState }, (err, api) => {
if (err) return console.error(err);
// Bắt đầu lắng nghe
api.listen((err, event) => {
if (err) return;
if (event.type === "message" && event.body === "!ping") {
api.sendMessage("🏓 Pong!", event.threadID);
}
});
});⚙️ Cấu hình (fca-config.json)
Sao chép fca-config.example.json thành fca-config.json:
{
"rateLimiter": {
"enabled": true,
"globalRps": 5,
"globalRpm": 100,
"minDelay": 300
},
"cache": {
"enabled": true,
"defaultTtl": 300000
},
"antiSpam": {
"enabled": true,
"maxMessages": 5,
"windowMs": 5000,
"timeoutMs": 30000
},
"logger": {
"level": "info",
"showBanner": true
}
}📚 API Mới
api.createCommandRouter(options)
Tạo bộ xử lý lệnh tự động với prefix, cooldown, phân quyền.
const router = api.createCommandRouter({
prefix: "!",
cooldown: 2000,
adminUIDs: ["UID_ADMIN"],
});
router.command({
name: "ping",
description: "Kiểm tra bot",
execute: async (event, args, api) => {
api.sendMessage("Pong!", event.threadID);
},
});
// Xây dựng text help tự động
router.buildHelpText("📋 Danh sách lệnh");
// Dùng trong listen
api.listen((err, event) => {
router.handle(event, api);
});api.broadcastMessage(message, threadIDs, options)
Gửi tin nhắn đến nhiều threads cùng lúc.
const result = await api.broadcastMessage(
"Thông báo tới tất cả!",
["thread1", "thread2", "thread3"],
{
delay: 500, // ms giữa các tin
stopOnError: false, // tiếp tục khi lỗi
onSuccess: (tid) => console.log("Gửi thành công:", tid),
onError: (tid, err) => console.log("Lỗi:", tid, err),
}
);
console.log(result.success, result.failed);api.sendMessageWithRetry(message, threadID, options)
Gửi tin với tự động retry khi thất bại.
await api.sendMessageWithRetry("Tin quan trọng", threadID, {
maxRetries: 3,
retryDelay: 2000,
backoff: 1.5, // tăng delay: 2s, 3s, 4.5s
onRetry: (attempt, err, delay) => console.log(`Retry #${attempt} sau ${delay}ms`),
});api.eventBus
Bus sự kiện nội bộ để giao tiếp giữa các module.
// Lắng nghe tất cả tin nhắn
api.eventBus.on("message", (eventName, event) => {
console.log("Tin mới:", event.body);
});
// Chỉ một lần
api.eventBus.once("message:event", (name, event) => {
// xử lý sự kiện group
});
// Phát sự kiện tùy chỉnh
api.eventBus.emit("my:custom:event", { data: "hello" });api.cache
Cache thông tin để giảm request tới Facebook.
// Lưu vào cache
api.cache.set("user", "12345", userInfo, 600000); // TTL 10 phút
// Lấy từ cache
const info = api.cache.get("user", "12345");
// Lấy hoặc fetch nếu không có
const info = await api.cache.getOrFetch("thread", threadID, () =>
api.getThreadInfo(threadID)
);
// Thống kê
console.log(api.cache.getStats());api.rateLimiter
Kiểm soát tốc độ gửi tin (tự động được áp dụng vào sendMessage).
// Xem thống kê
console.log(api.rateLimiter.getStats());
// Thay đổi cấu hình runtime
api.rateLimiter.setGlobalRps(3);
api.rateLimiter.setMinDelay(500);
api.rateLimiter.setEnabled(false); // tắt tạmapi.antiSpam
Hệ thống chống spam người dùng gửi quá nhiều tin.
// Thêm whitelist (admin bypass spam check)
api.antiSpam.whitelist("ADMIN_UID");
// Kiểm tra thủ công
const { spam, remaining } = api.antiSpam.check(event.senderID);
// Bọc handler tự động
const safeHandler = api.antiSpam.wrap((event) => {
// Chỉ chạy nếu không spam
handleEvent(event);
}, { spamMessage: "⚠️ Gửi chậm thôi!" });
// Bỏ block thủ công
api.antiSpam.unblock("USER_UID");api.plugins
Hệ thống plugin động.
// Load plugin từ file
await api.plugins.load("./plugins/my-plugin.js");
// Load plugin inline
await api.plugins.load({
name: "MyPlugin",
version: "1.0",
async setup({ api, logger, addHook }) {
addHook("message", async (event) => {
// xử lý sự kiện
});
return {
async cleanup() { /* dọn dẹp */ }
};
}
});
// Load cả thư mục
await api.plugins.loadDir("./plugins");
// Unload plugin
await api.plugins.unload("MyPlugin");
// Xem danh sách
console.log(api.plugins.list());SessionManager — Multi-Account
Quản lý nhiều tài khoản Facebook cùng lúc.
const { SessionManager } = require("fca-mmtat");
const manager = new SessionManager({ strategy: "round-robin" });
await manager.add("bot1", { appState: appState1 });
await manager.add("bot2", { appState: appState2 });
// Lắng nghe tất cả
manager.listenAll((event, api, session) => {
console.log(`[${session.id}]`, event.body);
});
// Gửi tin (tự động chọn account)
await manager.sendMessage("Hello!", threadID);
// Thống kê
console.log(manager.getStats());api.getSystemStats()
Xem toàn bộ thống kê hệ thống.
const stats = api.getSystemStats();
console.log(stats);
// {
// version: "1.0.0",
// name: "FCA-MMTAT",
// userID: "...",
// region: "SIN",
// cache: { hitRate: "85.0%", totalItems: 42 },
// rateLimiter: { globalQueueLength: 0 },
// antiSpam: { total: 10, blocked: 1 },
// plugins: [...]
// }🔌 Viết Plugin
// plugins/my-plugin.js
module.exports = {
name: "MyPlugin",
version: "1.0.0",
description: "Plugin mẫu",
async setup({ api, eventBus, logger, options, addHook }) {
logger("Plugin đang khởi động", "info");
// Đăng ký hook xử lý message
addHook("message", async (event) => {
if (event.body === "hello") {
api.sendMessage("World!", event.threadID);
}
});
// Lắng nghe EventBus
eventBus.on("message", (name, event) => {
// xử lý sự kiện
});
return {
// Được gọi khi unload plugin
async cleanup() {
logger("Plugin đang dừng");
}
};
}
};📝 Tất cả API gốc vẫn hoạt động
Tất cả API từ fca-unofficial đều được giữ nguyên:
sendMessage, getThreadInfo, getUserInfo, changeNickname, setTitle, addUserToGroup, removeUserFromGroup, changeAdminStatus, createNewGroup, deleteMessage, editMessage, forwardAttachment, markAsRead, createPoll, setMessageReaction, listenMqtt, và nhiều hơn nữa.
📄 License
MIT — Tự do sử dụng, chỉnh sửa và phân phối.
Được xây dựng bởi FCA-MMTAT Team — Dựa trên fca-unofficial của DongDev "# FCA-MTAT"