Skip to main content

A lightweight Python SDK for sending messages via Facebook Messenger

Project description

Facebook Messenger Graph API Python SDK

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

🚀 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

response = client.send_text(recipient_id="USER_PSID", message_text="Hello, user!")
print(response)

🖼️ Sending an Attachment By URL

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

🖼️ Sending a Local Attachment

file_path = "/path/to/image.jpg"
response = client.send_local_attachment(recipient_id="USER_PSID", file_path=file_path)
print(response)

Get Conversation History (Optional Limit)

response = 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:

from fb_gapi import MessengerAPIError

try:
    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.13.tar.gz (5.1 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.13-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fb_gapi-0.1.13.tar.gz
Algorithm Hash digest
SHA256 9c44b3c81a552b991104028057105862bda00a815f082ce851da5e781be80b41
MD5 1567cc031b82269718bf1601e32be0e8
BLAKE2b-256 fc77719676f1e5d8dd0fbad39f65fcf9c2ffdb0a6e6e3f736f760be42313a700

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for fb_gapi-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 13234b8f0f31d2fc2e37053e2b3481ed150672dfadadae4e5184618c3a2bf1e8
MD5 d3af3f1fbc08abb128f7e8dafed0222e
BLAKE2b-256 05a69854f7c1cae84425895d5297d74c099ebca00f7760c2cbea63256d7fdc3d

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