A modular, event-driven Facebook Messenger bot β reads session from
appstate.json, authenticates throughfca-unofficial, and dispatches incoming messages and group events to pluggable command and event modules.
appstate.json
β
βΌ
βββββββββββββββββββββββββββ
β index.js β
β (loader / dispatcher) β
ββββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β fca-unofficial β
β (Facebook Chat API) β
ββββββββββββββ¬βββββββββββββ
β MQTT / HTTPS
βΌ
βββββββββββββββββββββββββββ
β Facebook Messenger β
β Servers β
βββββββββββββββββββββββββββ
index.js loads session cookies from appstate.json, authenticates via fca-unofficial, and opens a persistent MQTT connection to Facebook's servers. Incoming messages are routed to command handlers (prefix-triggered via onStart) or chat handlers (every message via onChat). Group events are routed to event handlers by logMessageType.
- Modular Command System β add, remove, or modify commands without touching core logic
- Event-Driven Architecture β handle group events such as member joins and leaves
- Text-to-Speech β convert text to audio in 100+ languages
- Translation β translate text between languages using Google Translate
- QR Code Generator β generate QR codes from any text or URL
- System Info β view bot uptime, memory usage, and platform details
- Welcome / Goodbye Messages β automatic greetings for group members
- Node.js v18 or higher
- A Facebook account
- Facebook session cookies exported as
appstate.json
1. Clone the repository
git clone <repository-url>
cd fb-bot2. Install dependencies
npm install3. Configure AppState
Create appstate.json in the root directory with your Facebook session cookies. Tools like c3c-ufc-utility or browser cookie-export extensions can generate this file.
[
{
"key": "cookie_name",
"value": "cookie_value",
"domain": "facebook.com",
"path": "/",
"hostOnly": false,
"creation": "2024-01-01T00:00:00.000Z",
"lastAccessed": "2024-01-01T00:00:00.000Z"
}
]4. Start the bot
npm start| Command | Description | Usage |
|---|---|---|
/help |
List all available commands | /help |
/hi |
Greet the user | /hi |
/ping |
Check bot latency | /ping |
/system |
View uptime, memory, and platform info | /system |
/qr |
Generate a QR code | /qr <text> or reply with /qr |
/say |
Convert text to speech | /say <text> | <lang> |
/trans |
Translate text | /trans <text> | <lang> |
/qr https://example.com
/qr Hello World!
/say Hello world | en
/say μλ
νμΈμ | ko
/trans Bonjour | en
/trans Hello | ja
en (English) Β· ko (Korean) Β· ja (Japanese) Β· zh (Chinese) Β· vi (Vietnamese) Β· th (Thai) Β· fr (French) Β· de (German) Β· es (Spanish) Β· fil (Filipino) Β· id (Indonesian)
fb-bot/
βββ index.js # Main entry point β loader, dispatcher, lifecycle
βββ package.json # Dependencies and scripts
βββ appstate.json # Facebook session (DO NOT SHARE)
βββ modules/
β βββ commands/ # Command modules (prefix-triggered)
β βββ events/ # Event handler modules (group events)
βββ lib/
βββ fca-unofficial/ # Facebook Chat API library
Add a .js file to modules/commands/. Export config and at least one of onStart (runs when the command prefix is matched) or onChat (runs on every message).
export const config = {
name: "example",
description: "Description of the command",
};
// Runs when a user sends /example
export const onStart = async ({ api, event, args, prefix }) => {
const { threadID, messageID } = event;
api.sendMessage("Hello from example command!", threadID, messageID);
};
// Optional: runs on every message without a prefix
export const onChat = async ({ api, event }) => {
console.log(`Message received: ${event.body}`);
};| Property | Type | Description |
|---|---|---|
api |
Object | Facebook API β send messages, reactions, etc. |
event |
Object | Message event with threadID, senderID, body, args |
args |
string[] | Arguments after the command name |
prefix |
string | Configured command prefix (default /) |
commands |
Map | All loaded command modules |
Add a .js file to modules/events/. Export config (with an eventType array) and onStart.
export const config = {
name: "example",
description: "Handles specific events",
eventType: ["log:subscribe", "log:unsubscribe"],
};
export const onStart = async ({ api, event }) => {
const { threadID, logMessageType, logMessageData } = event;
api.sendMessage("An event occurred!", threadID);
};| Event Type | Trigger |
|---|---|
log:subscribe |
Member added to group |
log:unsubscribe |
Member left or was removed |
log:thread-name |
Group name changed |
log:thread-icon |
Group icon changed |
log:thread-color |
Chat color changed |
Edit the CONFIG object in index.js:
const CONFIG = {
prefix: "/", // Command prefix
appStatePath: "appstate.json", // Path to session file
commandsPath: join(__dirname, "modules", "commands"),
eventsPath: join(__dirname, "modules", "events"),
listenOptions: {
listenEvents: true, // Listen to group events
selfListen: false, // Ignore bot's own messages
logLevel: "silent", // Logging level
},
};
β οΈ Important
- Never share
appstate.jsonβ it contains your Facebook session and grants full account access - Add
appstate.jsonto.gitignorebefore committing - Use environment variables for sensitive configuration in production
- Regularly refresh your AppState β sessions expire; the bot handles this gracefully
Bot not starting β verify appstate.json exists and contains valid session data; confirm your account is not locked or checkpointed; run npm install to ensure all dependencies are present.
Bot not receiving messages β confirm listenEvents: true in options; check the console for connection errors; your session may have expired and need refreshing.
Commands not working β ensure commands use the correct prefix (default /); confirm command files export both config and onStart; review the console for module load errors.
Rate limiting β Facebook may temporarily restrict accounts that send too many messages; add delays between messages if sending in bulk; avoid automated mass messaging.
For educational purposes. Use responsibly and in accordance with Facebook's Terms of Service.