Skip to main content

fca-unofficial: Unofficial Facebook Chat API for Python

Project description

Using the Bot API (Group-only)

This guide shows developers how to use your bot and the underlying python_facebookapi to send messages to a Messenger group thread and connect using appstate.json.

If your deployment is currently group-only (commands work in group chats, not in 1:1 DMs), follow these steps.


1) Prepare appstate.json

The API logs in with cookies stored in appstate.json at the repository root. This is a JSON array of objects like:

[
  { "key": "c_user", "value": "..." },
  { "key": "xs", "value": "..." },
  { "key": "datr", "value": "..." },
  { "key": "sb", "value": "..." }
]

Ways to create it:

  • If you have a cookie string, you can paste it on the first run and a.py/bot.py will write appstate.json for you.
  • Or set FB_COOKIE before running:
$env:FB_COOKIE = "c_user=...; xs=...; datr=...; sb=..."
python a.py

The bot stores the parsed cookies in appstate.json automatically.


2) Run the bot (group commands)

To run the command-driven listener (recommended):

# Optional diagnostics
$env:DM_DEBUG = "1"

python a.py

In your group thread, send:

  • /ping → bot replies pong
  • /send group:<THREAD_ID> hello group → bot sends "hello group" into the specified group thread
  • /history <THREAD_ID> [amount] → bot prints recent messages from that group

Notes:

  • Use the group: prefix to force group mode when using /send.
  • The bot ignores its own messages, and you can restrict who can trigger commands using ALLOW_USER_IDS (comma-separated user IDs).

3) Send to a group programmatically

If you want to send a message from a Python script without using chat commands, use the API like this:

from python_facebookapi import login
import json

# Load appstate.json
with open("appstate.json", "r", encoding="utf-8") as f:
    appstate = json.load(f)

# Login
api = login({"appState": appstate}, options={"autoSaveAppState": True, "logging": True})

# Send a message to a group thread
GROUP_THREAD_ID = "758202360604154"  # replace with your group thread ID
res = api.sendMessage("Hello, group!", GROUP_THREAD_ID, isSingleUser=False)
print(res)  # {"messageID": "...", "threadID": "...", "timestamp": ...}

Tips to find a group thread ID:

  • Inspect logs printed by the bot when it receives messages (threadID field).
  • Use API methods like getThreadList() to enumerate threads and pick the group.

4) Example: minimal group bot

If you prefer a tiny "bot.js-like" Python bot, use bot.py. It supports:

  • /ping, /send group:<id> <msg>, /help
  • Logging in via appstate.json or FB_COOKIE

Run it:

python bot.py

5) Troubleshooting (group-only)

  • The bot replies in group but not in DMs:

    • This is expected in a group-only deployment. DMs may require extra realtime parsing and/or fallback polling.

    • You can enable a polling fallback in bot.py with:

      $env:POLL_FALLBACK = "1"
      python bot.py
      
    • Or keep sending commands from a group thread.

  • Login errors / disconnects:

    • Refresh cookies in appstate.json (ensure c_user, xs, datr, sb).
    • Keep DM_DEBUG=1 to capture listener logs.

6) Security

  • appstate.json contains session cookies—keep it private.
  • Use a dedicated account for testing.

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

fca_unofficial-0.0.2.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

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

fca_unofficial-0.0.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file fca_unofficial-0.0.2.tar.gz.

File metadata

  • Download URL: fca_unofficial-0.0.2.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for fca_unofficial-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8c4874fe5e91382a007e242a03d766b821d4d5502234e552161732a674c776bd
MD5 7e40bc1394fa46e1c01bf5ee4e552edb
BLAKE2b-256 d5450ab8b97459c6af0639a1984e706afed255693d87c1218ab88ca95c08987e

See more details on using hashes here.

File details

Details for the file fca_unofficial-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: fca_unofficial-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for fca_unofficial-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99bd56f23173f007db87e008a5d2e499d5c9af6472a4fa4daea565b7286e26f0
MD5 c580b7332b9374eabdf4577f10be222f
BLAKE2b-256 695106b60eaf3e69c6138e062996a47f750ef37366bec596fda63bcc43191af9

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