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
MessengerAPIErrorwhen the Facebook API responds with an error.
Prerequisites
- Create an app on Meta Developer Account and connect it to your Page.
- Facebook Page Access Token
- Python 3.6+
🚀 Getting Started
📦 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)
🛠️ 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
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.26.tar.gz.
File metadata
- Download URL: fb_gapi-0.1.26.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1f7c068100aa00ddcfa421f681789fe70cbf56500129565967e8f756116a827
|
|
| MD5 |
631c6bc37b57c50e2d53522eca96f90c
|
|
| BLAKE2b-256 |
09272770c2051c48384b5ba5468d348e975437d7e9264c3ad49dc17834dc4974
|
File details
Details for the file fb_gapi-0.1.26-py3-none-any.whl.
File metadata
- Download URL: fb_gapi-0.1.26-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb6d3aa3f320d83de793fe83c1d91230dc551665e0311e7b5cb29ad135058d10
|
|
| MD5 |
bb0a8298de263d51cdb0cd221d319223
|
|
| BLAKE2b-256 |
36d334bf510ff7306959b6e62f9b964989ffe29e9165fc01aef90738477aee8f
|