Facebook Auto Poster Bot

Automates Facebook group posting with Playwright and SQLite.

Overview

Facebook Auto Poster Bot is a Node.js and TypeScript automation script designed to schedule and publish content across multiple Facebook groups. It acts as a reliable social media assistant by utilizing a persistent browser session to mimic human interaction, drastically reducing the chances of triggering spam detection or security checkpoints.

Features

  • Persistent Browser Sessions: Reuses cookies and local storage to maintain logged-in states between runs.
  • Spintax Support: Allows dynamic text variations (e.g., {Hello|Hi}) to ensure each post is unique.
  • Media Uploads: Supports uploading single or multiple image files alongside text content.
  • Smart Delay & Cooldowns: Implements random delays between posts and a strict cooldown interval per group to prevent spamming.
  • Auto-Login with Checkpoint Handling: Automatically fills login credentials when needed, while gracefully pausing for manual CAPTCHA or 2FA resolution.
  • Lexical Editor Compatibility: Successfully interacts with Facebook's modern Lexical text editor by simulating genuine keystrokes.
  • Database Tracking: Logs all active groups, post history, and timestamps using SQLite.
  • Headless Mode: Can run entirely in the background or in a visible browser window for debugging.

Technical Implementation

The bot relies heavily on DOM manipulation and robust element selection to navigate Facebook's complex and frequently changing user interface. It utilizes a stateful approach, remembering when a group was last posted to, and logging successes or failures.

Requirements

  • Node.js (v18+)
  • npm

Installation

  1. Install dependencies:
    bash
    npm install
    
  2. Install Playwright browsers:
    bash
    npx playwright install chromium
    

Configuration

The project uses several Git-ignored configuration files:

  1. Environment Variables (.env): Create a .env file from .env.example:

    env
    HEADLESS=false
    FB_USER_DATA_DIR=./user_data
    MIN_DELAY_SECONDS=60
    MAX_DELAY_SECONDS=180
    POST_INTERVAL_MINUTES=60
    POST_TEMPLATE_PATH=post_template.txt
    IMAGE_PATH=img.jpg,img2.jpg
    
  2. Login Credentials (config.json): Provides email and password for automatic login attempts.

    json
    {
      "email": "your_email",
      "password": "your_password"
    }
    
  3. Target Groups (groups.json): An array of Facebook group objects to target.

    json
    [
      {
        "name": "Target Group 1",
        "url": "https://www.facebook.com/groups/123456789/"
      }
    ]
    
  4. Post Content (post_template.txt): The text content for your posts, supporting Spintax.

Database Setup

The script automatically initializes an SQLite database (facebook.db) on the first run, creating two primary tables: groups and posts_history. It also syncs the groups.json file into the database automatically.

How to Run

Compile and run the bot using:

bash
npm start

Note: npm start executes ts-node src/bot.ts.

Main Workflow & Usage Notes

  • First Run: Set HEADLESS=false. The bot will attempt to log in using config.json. You must manually complete any security checkpoints or CAPTCHAs in the open browser window. Once the feed is visible, the bot resumes automation.
  • Subsequent Runs: Set HEADLESS=true. The bot uses the saved session in ./user_data and skips the login process entirely.
  • Posting Logic: Before posting to a group, the bot checks the database to ensure POST_INTERVAL_MINUTES has passed since the last post. If so, it navigates to the group, locates the composer, uploads images (if configured), types the text line-by-line, and clicks post.

Development

The bot is developed as a modular TypeScript application.

Technologies & Libraries

  • TypeScript: Provides static typing for robust application structure and Playwright integration.
  • Playwright: The core browser automation engine used to manipulate the DOM, handle file inputs, and manage persistent profiles.
  • Better-SQLite3: A synchronous, fast SQLite driver used for storing group states, cooldown timestamps, and logging execution history.
  • Dotenv: Manages environment variables.
  • ts-node: Allows direct execution of TypeScript files without a separate compilation step.

Highlights

Bypassing Facebook's Lexical Editor

One of the most notable technical challenges in this project is interacting with Facebook's modern "Lexical" rich-text editor. Standard Playwright fill() or type() commands often fail to register line breaks correctly or trigger the required UI updates. The bot solves this by simulating precise, character-by-character keystrokes and explicitly dispatching Enter key presses to force the editor to generate proper <p> tags, ensuring formatting is perfectly preserved.

Robust Login State Detection

Facebook lacks a consistent URL structure for authentication states, relying heavily on dynamic client-side rendering. The bot implements a sophisticated, multi-tiered heuristic checking system. It polls the DOM for an array of specific selectors (like profile navigation icons, search bars, and localized login buttons in various languages) and falls back to URL pattern matching to definitively determine if the browser is logged in, on a security checkpoint, or stuck on a standard login prompt.

Safe Automation Practices

To avoid triggering Facebook's automated anti-spam systems, the bot injects an initialization script to hide the webdriver flag from the browser. Furthermore, it separates media uploading from text insertion to ensure DOM stability, waits for random intervals between group interactions, and relies heavily on a persistent data directory rather than repeatedly authenticating.