Skip to main content

A lightweight Python Sync / Async SDK for sending messages via Facebook Messenger

Project description

Facebook Messenger Graph API Python Async SDK ( v23.0 )

A lightweight Python SDK for sending messages (text and image attachments) using the Facebook Graph API along with the Facebook Messenger Send API.

Installation

pip install -U fb_gapi

🚀 Features

  • Send Text Messages: Easily send text messages to users.
  • Send Remote Attachments: Send remote attachments by URL.
  • Send Local Attachments: Send local attachments by file path.
  • Get Conversation History: Fetch the latest conversation history.
  • Error Handling: The SDK will raise a MessengerAPIError when the Facebook API responds with an error.

🚀 Usage

📦 Import the client

from fb_gapi import MessengerClient

🔒 Initialize with your Page Access Token

client = MessengerClient(access_token="YOUR_PAGE_ACCESS_TOKEN")

✉️ Sending a Text Message

async def asend_text():
    response = await client.send_text(
        recipient_id="USER_PSID", message_text="Hello from Python!"
    )
    print(response)

🖼️ Sending an Attachment By URL

async def asend_remote_attachment():
    response = await client.send_remote_attachment(
        recipient_id="USER_PSID",
        image_url="https://example.com/image.jpg",
    )
    print(response)

🖼️ Sending a Local Attachment

async def asend_local_attachment():
    response = await client.send_local_attachment(
        recipient_id="USER_PSID", file_path="./test.png"
    )
    print(response)

Get User Name

async def aget_user_name():
    response = await client.get_user_name(user_id="USER_PSID")
    print(response)

Get Conversation History (Optional Limit)

async def aget_chat_history_limited():
    response = await client.get_chat_history(recipient_id="USER_PSID", limit=5)
    print(response)

⚠️ Error Handling

This SDK will raise a MessengerAPIError when the Facebook API responds with an error.

Example:

async def aexample_error_handling():
    try:
        await client.send_text("invalid_user_id", "Hi!")
    except MessengerAPIError as e:
        print(f"GAPI Error: {e}")

Error Output Example:

MessengerAPIError (HTTP 400): [OAuthException] Invalid OAuth access token. (code 190)

📄 Requirements

  • Python 3.6+

🛠️ TODO

  • Improve conversation history limit.
  • Add support for templates.
  • Add support for quick replies.
  • Add support for actions.
  • Add support for custom buttons.

📃 License

MIT License. Use freely and contribute!

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

fb_gapi-0.1.24.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

fb_gapi-0.1.24-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file fb_gapi-0.1.24.tar.gz.

File metadata

  • Download URL: fb_gapi-0.1.24.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fb_gapi-0.1.24.tar.gz
Algorithm Hash digest
SHA256 ead48f63e7747fe67d060b884bbcb739302777a7f00fd2e231108fad20656a59
MD5 038a78c32e3edb7a27e4c31dc39d3d23
BLAKE2b-256 fdc555d5bf8d9f21bd7cfb7aedcb28ef0b5606cc445c851f46b642b290a0a282

See more details on using hashes here.

File details

Details for the file fb_gapi-0.1.24-py3-none-any.whl.

File metadata

  • Download URL: fb_gapi-0.1.24-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fb_gapi-0.1.24-py3-none-any.whl
Algorithm Hash digest
SHA256 b96caf4a6755c955a8fbfbfb6867e580cb8df4749181bfc4c46d0269da57d457
MD5 24219281f1ad8a503ddaf39c606094e5
BLAKE2b-256 9ad1475cebdf0ba838403905714bfb3fce519292168e5eae21c47c09e48bc401

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