Skip to content

johnlester-0369/fb-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FB-Bot πŸ€–

A modular, event-driven Facebook Messenger bot β€” reads session from appstate.json, authenticates through fca-unofficial, and dispatches incoming messages and group events to pluggable command and event modules.

Node.js License

Architecture

         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.

Features

  • 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

Prerequisites

  • Node.js v18 or higher
  • A Facebook account
  • Facebook session cookies exported as appstate.json

Installation

1. Clone the repository

git clone <repository-url>
cd fb-bot

2. Install dependencies

npm install

3. 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

Commands

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>

Examples

/qr https://example.com
/qr Hello World!

/say Hello world | en
/say μ•ˆλ…•ν•˜μ„Έμš” | ko

/trans Bonjour | en
/trans Hello | ja

Language Codes

en (English) Β· ko (Korean) Β· ja (Japanese) Β· zh (Chinese) Β· vi (Vietnamese) Β· th (Thai) Β· fr (French) Β· de (German) Β· es (Spanish) Β· fil (Filipino) Β· id (Indonesian)

Project Structure

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

Creating Commands

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}`);
};

Command Context

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

Creating Event Handlers

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 Types

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

Configuration

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
  },
};

Security

⚠️ Important

  • Never share appstate.json β€” it contains your Facebook session and grants full account access
  • Add appstate.json to .gitignore before committing
  • Use environment variables for sensitive configuration in production
  • Regularly refresh your AppState β€” sessions expire; the bot handles this gracefully

Troubleshooting

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.

License

For educational purposes. Use responsibly and in accordance with Facebook's Terms of Service.

About

A minimal and simple Facebook Messenger bot built with a modular, event-driven architecture

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors