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.

🚀 Getting Started

  1. Create an App on Meta Developer Account and connect it to your Page.
  2. Add a callback URL to your app. eg: https://example.com/callback to communicate with your server.
  3. Get the Facebook Page Access Token from your created app.
  4. Send a GET request to your callback URL to verify the connection. eg: https://example.com/callback?hub.verify_token=VERIFY_TOKEN.
  5. All set! You can now use the SDK.

📝 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.asend_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.asend_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.asend_local_attachment(
        recipient_id="USER_PSID", file_path="./test.png"
    )
    print(response)

👨 Get User Name (Only for verified app by Meta)

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

🗒️ Get Conversation History (Optional Limit upto 9)

async def aget_chat_history_limited():
    response = await client.aget_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.asend_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)

Note: USER_PSID means page scoped user id, which you can get from the response of the webhook callback set for your page.

📚 Requirements

  • Python 3.9+

🛠️ TODO

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

📃 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.27.tar.gz (7.5 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.27-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fb_gapi-0.1.27.tar.gz
  • Upload date:
  • Size: 7.5 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.27.tar.gz
Algorithm Hash digest
SHA256 87708d9cab88abfef3e7bc7bb6e548dca7526a94d072985a3329b288c8d8716f
MD5 9e6d7018aadcc69bf59ec4d57b647359
BLAKE2b-256 793801061d36b1934866dcd7e86b8ebe2f17422792a7b2e9f19dcb2f82a3ec4f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fb_gapi-0.1.27-py3-none-any.whl
  • Upload date:
  • Size: 7.2 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.27-py3-none-any.whl
Algorithm Hash digest
SHA256 7fb929f2c4d851245cd3acb2e7739ab48230969d0522df2250c403f28c61a794
MD5 7c79f239c2fc24b14059b2c661ef3c36
BLAKE2b-256 b1c802d838ce084ff2d4a0f9997718c9acc383c1a420b2deac3fc4d1297e6150

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