Skip to main content

Modern, account-based Python library for the unofficial Facebook Messenger API — with E2EE support.

Project description

fbchat-v2

PyPI Python Downloads License: MIT Status Telegram

A modern, unofficial Python library for the Facebook Messenger API — driven by a real user account (cookies / login). Now with End-to-End Encryption (E2EE) support for 1-on-1 Messenger chats via a Go bridge.


⚠️ Disclaimer

This is not an official Facebook product. Facebook provides an official Messenger Platform API here. fbchat-v2 differs in that it authenticates as a real user account via cookies or username/password, which carries inherent risks (account flags, rate-limits, ToS considerations). Use at your own risk and never share your cookies/tokens.

Since November 2024, all 1-on-1 Messenger chats are End-to-End Encrypted by default. fbchat-v2 v2.1.0 ships an E2EE listener (listeningE2EEEvent) that decrypts those messages by spawning a Go subprocess (fbchat-bridge-e2ee); group chats continue to use the MQTT WebSocket listener (listeningEvent).


✨ Features

Authentication

  • 🔐 Login via username/password (with optional 2FA TOTP) or session cookies
  • 🍪 Session reuse — no re-login on every run

Messaging

  • 📥 Receive messages from both users and group threads
  • 🔒 E2EE listener for 1-on-1 Messenger chats (Secret Conversations / Labyrinth) via Go bridge
  • 📤 Send text, attachments, stickers, user mentions
  • 🔍 Search messages and threads
  • ↩️ Reactions, unsend, message-request handling
  • 📡 Real-time event listener

Threads & Groups

  • 👥 Create groups, add admins, change name / emoji / nickname
  • 📊 Polls, full thread metadata

Facebook actions (_features._facebook)

  • 📝 Create posts, edit bio, profile registration
  • 👤 User search, profile info, notification management
  • 🚫 Block/unblock, Marketplace and Professional mode

📦 Installation

Default install (group chats only)

pip install fbchat-v2

This pulls in requests, paho-mqtt, attrs, and pyotp. The MQTT-based group-message listener (listeningEvent) works out of the box.

Optional: enable E2EE for 1-on-1 chats

The E2EE listener requires a separate Go binary (fbchat-bridge-e2ee) that PyPI cannot ship. Build it once from source:

# 1) Install Go ≥ 1.24 from https://go.dev/dl/
# 2) Clone the bridge from the upstream repo
git clone https://github.com/MinhHuyDev/fbchat-v2
cd fbchat-v2/bridge-e2ee
git clone https://github.com/mautrix/meta.git ./meta
go mod tidy

# Windows
go build -ldflags="-s -w" -o fbchat-bridge-e2ee.exe .

# Linux / macOS
go build -ldflags="-s -w" -o fbchat-bridge-e2ee .

Then point the library at the binary via an environment variable:

# Windows (PowerShell)
$env:FBCHAT_E2EE_BIN = "C:\path\to\fbchat-bridge-e2ee.exe"

# Linux / macOS
export FBCHAT_E2EE_BIN=/path/to/fbchat-bridge-e2ee

The [e2ee] extra is reserved for a future automatic-download helper:

pip install "fbchat-v2[e2ee]"   # currently a no-op placeholder

Roadmap: v2.2.0 plans to publish prebuilt bridge binaries on GitHub Releases and auto-download them on first use.


🚀 Quick Start

Group chat listener (no Go required)

import threading
from fbchat_v2 import dataGetHome, listeningEvent

# 1) Load your Facebook session (cookies dict / string)
dataFB = dataGetHome(cookies=YOUR_COOKIES)

# 2) Start the MQTT listener
listener = listeningEvent(dataFB)
listener.get_last_seq_id()
threading.Thread(target=listener.connect_mqtt, daemon=True).start()

# listener.bodyResults is updated in real time with each new event

1-on-1 E2EE listener (requires Go bridge)

import threading
from fbchat_v2 import dataGetHome, listeningE2EEEvent

dataFB = dataGetHome(cookies=YOUR_COOKIES)

listener = listeningE2EEEvent(dataFB)
listener.get_last_seq_id()
threading.Thread(target=listener.connect_mqtt, daemon=True).start()

# listener.bodyResults uses the SAME schema as listeningEvent —
# you can swap the import without changing your event handler.

Demo — receiving decrypted 1-on-1 E2EE messages

Demo: fbchat-v2 receiving 1-on-1 Messenger chats via the E2EE bridge


📂 Package Layout

Installed Python package (importable as fbchat_v2):

fbchat_v2/
├── __init__.py                 # Re-exports: dataGetHome, listeningEvent, listeningE2EEEvent
├── py.typed                    # PEP 561 marker
├── _core/                      # Session, login, low-level helpers
│   ├── _facebookLogin.py
│   ├── _session.py
│   └── _utils.py
├── _features/
│   ├── _facebook/              # Posts, bio, search, notifications, blocking, marketplace, …
│   │   ├── _blocking.py
│   │   ├── _changeBio.py
│   │   ├── _createPost.py
│   │   ├── _get_user_info.py
│   │   ├── _marketplace.py
│   │   ├── _notification.py
│   │   ├── _professional.py
│   │   ├── _registerOnProfile.py
│   │   └── _search.py
│   └── _thread/                # Group/thread admin operations
│       ├── _addAdmin.py
│       ├── _all_thread_data.py
│       ├── _changeEmoji.py
│       ├── _changeNameThread.py
│       └── _changeNickname.py
└── _messaging/
    ├── _attachments.py
    ├── _listening.py           # MQTT — group messages
    ├── _listening_e2ee.py      # Go bridge — 1-on-1 E2EE messages
    ├── _message_requests.py
    ├── _reactions.py
    ├── _send.py
    └── _unsend.py

Public API

The top-level fbchat_v2 namespace re-exports the most common entry points:

Symbol Source Purpose
dataGetHome fbchat_v2._core._session Build the session object from cookies / login
listeningEvent fbchat_v2._messaging._listening MQTT listener for group messages
listeningE2EEEvent fbchat_v2._messaging._listening_e2ee E2EE listener for 1-on-1 messages
__version__ fbchat_v2 Package version string

Submodules (fbchat_v2._features._facebook._createPost, etc.) can be imported directly for fine-grained access.


🔧 System Requirements

Component Minimum Recommended Notes
Python 3.10 3.11 / 3.12 Required
Go toolchain 1.24 1.24+ Only for E2EE — to build fbchat-bridge-e2ee
Git any latest Needed for go mod tidy to fetch mautrix/meta
OS Windows / Linux / macOS
RAM 256 MB 1 GB+ Bridge process uses ~80–150 MB when active
Network Stable connection to facebook.com and edge-chat.facebook.com

Python dependencies (auto-installed by pip):

requests   >= 2.31.0   # HTTP client
paho-mqtt  >= 1.6.1    # MQTT WebSocket for listeningEvent
attrs      >= 23.2.0   # Data classes
pyotp      >= 2.9.0    # 2FA TOTP for username/password login

🏗 Architecture

flowchart LR
    A[Your bot / app] --> B[fbchat_v2._core<br/>session • login • utils]
    B --> C[fbchat_v2._features<br/>facebook • thread]
    B --> D[fbchat_v2._messaging<br/>send • listen • reactions]
    D -.spawns.-> E[fbchat-bridge-e2ee<br/><i>Go subprocess, optional</i>]
    C --> F[(Facebook<br/>internal endpoints)]
    D --> F
    E --> F

Three clear layers:

Layer Path Responsibility
Core fbchat_v2._core Session management, login, request helpers, low-level utilities
Features fbchat_v2._features Facebook & thread business logic (posts, groups, profile, …)
Messaging fbchat_v2._messaging Send / receive / react / listen / unsend

Full request flow diagrams live in FLOWCHART.md.


🗺 Roadmap

  • E2EE decryption for 1-on-1 Messenger chats (v2.1.0 — Go bridge)
  • Native async / await API
  • Prebuilt bridge binaries published on GitHub Releases (auto-download)
  • Full type hints across the public API
  • Pluggable storage backend for sessions
  • Integration test suite & CI

Have an idea? Open an issue.


🤝 Contributing

Contributions are welcome! See CONTRIBUTING guide and CODE_OF_CONDUCT.md.

  1. Fork the repo and create a feature branch (feat/<name>).
  2. Follow the existing 3-layer architecture (_core_features / _messaging).
  3. Use Conventional Commitsfeat:, fix:, docs:, refactor:, …
  4. Open a PR with a clear description and reproduction steps for bug fixes.
  5. Never commit secrets — config.json, cookies, tokens, .venv, etc.

🌟 Acknowledgements

After 4 years of development, this project would not exist without its community.

Community contributors

tomdev112 · syrex1013 · Kheir Eddine · 陶世玉 · Jihadi John · Bắc Trịnh · Quang Trần · Minh Trần Ngọc · Victor Knutsenberger · Hoàng Lân · Kareem Adel Abomandor · @lluevy · @phuncnheo · @minhphatnw · @khanh235a · @chapesh1 · @klongg13 · @seafibrahem · @agent1047 · @stefekdziura

Upstream open-source projects powering v2.1.0 (E2EE)

AI assistants

  • Claude Opus 4.7 (Anthropic) — code review, documentation, refactoring
  • Codex 5.3 (OpenAI) — boilerplate and RPC prototyping

If you have contributed and are missing from this list, please open an issue or PR.


📜 License

Distributed under the MIT License. See LICENSE for details.


Made with ❤️ by MinhHuyDev · Telegram

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fbchat_v2-2.1.1.tar.gz (82.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fbchat_v2-2.1.1-py3-none-any.whl (80.4 kB view details)

Uploaded Python 3

File details

Details for the file fbchat_v2-2.1.1.tar.gz.

File metadata

  • Download URL: fbchat_v2-2.1.1.tar.gz
  • Upload date:
  • Size: 82.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for fbchat_v2-2.1.1.tar.gz
Algorithm Hash digest
SHA256 bfa75227a562c82ba031e8bd5ff7a5b82ed221d9bb1c40c37eac5b7449294e00
MD5 2523052b51fe5325f6a30c96d1b4e590
BLAKE2b-256 381e8a09e952053577be0da25182c2f0b91d35cc6a2915a007813a140f758aec

See more details on using hashes here.

File details

Details for the file fbchat_v2-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: fbchat_v2-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 80.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for fbchat_v2-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2aae57f651c582744d845d986302887e3db17911798750fb6578241cd427075
MD5 236c9e0c7937f57dea449bb9f24f518b
BLAKE2b-256 b54d3e5ffb81b5a4124ae3f09d84a7de28c9217b10f56d64abe1621e235cff6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page