Skip to main content
Supported python versions: 3.10, 3.11 and 3.12 License: BSD 3-Clause

An Unofficial powerful and efficient library to interact with Facebook’s Messenger, using just your facebook account cookies This is an asyncio fork of the fbchat library.

This is not an official API, Facebook has that over here for chat bots. This library differs by using a normal Facebook account instead.

aiofbchat currently support:

  • Fetching all messages, threads and images in threads.

  • Searching for messages and threads.

  • Creating groups, setting the group emoji, changing nicknames, creating polls, etc.

  • Listening for, an reacting to messages and other events in real-time.

  • Type hints, and it has a modern codebase (e.g. only Python 3.9 and upwards).

  • async/await

Essentially, everything you need to make an amazing Facebook bot!

Documentation

This is not Documented yet.

Usage

A basic example of how you can use it.

import asyncio
import aiofbchat
import json
from aiofbchat._events import Connect, Disconnect
from aiofbchat._events._delta_class import MessageEvent


# use cookie exteniom in browser and get your facebook account cookies
# and paste in a file and pass the coookie file path to this get_cookie fuction

def get_cookie(path)-> dict:
    with open(path, "r") as f:
        data = json.load(f)
    cookies = {}
    for cookie in data:
      cookies[cookie["key"]] = cookie["value"]
    return cookies


# Listen for new events and when that event is a new received message, reply to the author of the message
async def listen(listener, session: aiofbchat.Session):
    async for event in listener.listen():
        if isinstance(event, Connect):
            print("AioFbchat is connected")

        if isinstance(event, MessageEvent):
            # If you're not the author, echo
            if event.author.id != session.user.id:
                # reply_to_id replies to the given message id
                await event.thread.send_message("Hello!", reply_to_id=event.message.id)

async def main():
    # creating session from cookies
    session = await aiofbchat.Session.from_cookies(get_cookie("path to json"))
    # pass the cookies to client
    # you can use client to fetch other facebook users data
    client = aiofbchat.Client(session=session)

    # creatie a listener instance to listen to upcoming events (e.g. messages)
    # set chat_on to false if you dont want to show active status
    listener = aiofbchat.Listener(session=session, chat_on=True, foreground=True)

    # creating async tasm to run
    listen_task = asyncio.create_task(listen(listener, session))

    client.sequence_id_callback = listener.set_sequence_id

    # Call the fetch_threads API once to get the latest sequence ID
    await client.fetch_threads(limit=1).__anext__()

    # Let the listener run, otherwise the script will stop
    try:
        await listen_task
    except KeyboardInterrupt:
        await session._session.close()
    finally:
        await session._session.close()


# run the main fuction and done
asyncio.run(main())

Metadata

Release files for aiofbchat 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiofbchat 0.1.0
File Size Uploaded Provenance
aiofbchat-0.1.0.tar.gz 115.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for aiofbchat 0.1.0
File Interpreter ABI Platform
aiofbchat-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 178.9 kB

Release files / aiofbchat-0.1.0.tar.gz

Download URL aiofbchat-0.1.0.tar.gz
Size 115.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8b1e78a8a2649cbd8909ad91ce55bb2356702961b27a16704de2b88a3acdde80
BLAKE2b-256 checksum
How to use checksums
b0f838c4e70b0106eafea16560c43895e0767b1be98eb533569af7ad9a557306
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.8 Linux/6.1.57-android14-11-gcf2c2dc91b9e-ab11782407

Release files / aiofbchat-0.1.0-py3-none-any.whl

Download URL aiofbchat-0.1.0-py3-none-any.whl
Size 63.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a989294b60f6dc7a9347c81aa37e11947206c93d526eb9c63e9286eaf15cb664
BLAKE2b-256 checksum
How to use checksums
a96ba5d630923d264133c78ec40f9dece6f956c94be020940be10a7c90fbd32a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.8 Linux/6.1.57-android14-11-gcf2c2dc91b9e-ab11782407

Security for aiofbchat 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page