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
MessengerAPIErrorwhen 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fb_gapi-0.1.14.tar.gz.
File metadata
- Download URL: fb_gapi-0.1.14.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
451c9f45de7de761a52e0cd8a228c5619cadb76b955d98197e32008f14894536
|
|
| MD5 |
b91809ba945a947cd7a11173d6a7e408
|
|
| BLAKE2b-256 |
77906d3e09c046bb1946c791f2d44207cf407af6196449bc5da07265c7e1caae
|
File details
Details for the file fb_gapi-0.1.14-py3-none-any.whl.
File metadata
- Download URL: fb_gapi-0.1.14-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36a6541187adf51d54b45cf6bf9f8626351ddda097b5717dbee6db4c9c4d106d
|
|
| MD5 |
5a4f1c70c0085a78980b95a69cb241b4
|
|
| BLAKE2b-256 |
41c6ea3e8c2777c190e62ce748eb82c6711751aa651108c6542790c52e004d39
|