Skip to main content

An Unofficial facebook messenger chat api.

Project description

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())

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

aiofbchat-0.1.0.tar.gz (115.4 kB view details)

Uploaded Source

Built Distribution

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

aiofbchat-0.1.0-py3-none-any.whl (63.5 kB view details)

Uploaded Python 3

File details

Details for the file aiofbchat-0.1.0.tar.gz.

File metadata

  • Download URL: aiofbchat-0.1.0.tar.gz
  • Upload date:
  • Size: 115.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/6.1.57-android14-11-gcf2c2dc91b9e-ab11782407

File hashes

Hashes for aiofbchat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8b1e78a8a2649cbd8909ad91ce55bb2356702961b27a16704de2b88a3acdde80
MD5 8f165e5e283453265af25eade1f22f1d
BLAKE2b-256 b0f838c4e70b0106eafea16560c43895e0767b1be98eb533569af7ad9a557306

See more details on using hashes here.

File details

Details for the file aiofbchat-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aiofbchat-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 63.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/6.1.57-android14-11-gcf2c2dc91b9e-ab11782407

File hashes

Hashes for aiofbchat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a989294b60f6dc7a9347c81aa37e11947206c93d526eb9c63e9286eaf15cb664
MD5 0772197f0c357311195d6a41089bf1d4
BLAKE2b-256 a96ba5d630923d264133c78ec40f9dece6f956c94be020940be10a7c90fbd32a

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