messenger-api-python
Python Wrapper to various APIs from Facebook Messenger Platform.
Features
Send API (v12.0)
- Send text messages
- Send attachments from a remote file (image, audio, video, file)
- Send attachments from a local file (image, audio, video, file)
- Send templates (generic messages)
- Send quick replies
- Send buttons
Profile API (v12.0)
- Set welcome screen
- Set persistent menu
Attachment Upload API (v12.0)
- Upload attachments from a remote file (image, audio, video, file)
- Upload attachments from a local file (image, audio video, file)
Reusable components
Various components used when sending messages in Facebook Messenger are wrapped into Python objects to make them reusable and easy to use.
- Elements: used to contains various Element objects
- Element: a card-like component that holds various other components
- Buttons: used to contains various Button objects
- Button: button used in various other components, can also be used alone
- QuickReplies: used to contains various QuickReply objects
- QuickReply: used when sending messages accompanied with quick replies
- PersistentMenu: used when setting up persistent menu
Prerequisite
- Python 3.7+ installed
- You'll need to setup a Facebook App, Facebook Page, get the Page Access Token and link the App to the Page.
How to install
From GitHub
- Clone this repository :
git clone https://github.com/krishna2206/messenger-api-python.git
- Navigate to the cloned repository folder and build the package :
python -m build
- Navigate to dist folder and install the package, <package_name> may vary so I don't explicitly provide the name here :
pip install <package_name>.whl
From Pypi
Package from Pypi.org may not be the latest one, if you want the latest version of this package, install it from the GitHub repository (see above)
pip install messenger-api-python
Usage
Send API
from messengerapi import SendApi
send_api = SendApi(<page_access_token>)
send_api.send_text_message(<message>, <recipient_id>)
Note: From Facebook regarding User IDs
These ids are page-scoped. These ids differ from those returned from Facebook Login apps which are app-scoped. You must use ids retrieved from a Messenger integration for this page in order to function properly.
If
app_secretis initialized, an app_secret_proof will be generated and send with every request. Appsecret Proofs helps further secure your client access tokens. You can find out more on the Facebook Docs
Sending a generic template message:
Generic Template Messages allows you to add cool elements like images, text all in a single bubble.
from messengerapi import SendApi
from messengerapi.components import Elements, Element, Buttons, Button, POSTBACK
send_api = SendApi(<page_access_token>)
elements = Elements()
buttons = Buttons()
button = Button(button_type=POSTBACK, title="My button")
buttons.add_button(button.get_content())
element = Element(title="My element", subtitle="The element's subtitle, image_url=<image_url>, buttons=buttons)
elements.add_element(element.get_content())
send_api.send_generic_message(elements.get_content() , recipient_id , image_aspect_ratio="horizontal")
Sending remote (from URL) image/audio/video/file:
from messengerapi import SendApi
send_api = SendApi(<page_access_token>)
# To send an image
send_api.send_image_attachment(<image_url> , <recipient_id>)
# To send an audio
send_api.send_audio_attachment(<audio_url> , <recipient_id>)
# To send a video
send_api.send_video_attachment(<video_url> , <recipient_id>)
# To send a file
send_api.send_file_attachment(<file_url> , <recipient_id>)
Sending local image/audio/video/file:
from messengerapi import SendApi
send_api = SendApi(<page_access_token>)
# To send an image
send_api.send_local_image(<image_location> , <recipient_id>)
# To send an audio
send_api.send_local_audio(<audio_location> , <recipient_id>)
# To send a video
send_api.send_local_video(<video_location> , <recipient_id>)
# To send a file
send_api.send_local_file(<file_location> , <recipient_id>)
To do
- Securing requests
- Unit tests
Metadata
Release files for messenger-api-python 2022.4.22
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| messenger-api-python-2022.4.22.tar.gz | 8.9 kB | Without provenance | Details for messenger-api-python-2022.4.22.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| messenger_api_python-2022.4.22-py3-none-any.whl | Python 3 | none | any | Details for messenger_api_python-2022.4.22-py3-none-any.whl |
Total release size: 19.8 kB
Release files / messenger-api-python-2022.4.22.tar.gz
| Download URL | messenger-api-python-2022.4.22.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a95fe5cbe64009e226ac702eb750406c73f03c1f6a3e6728140091c454dc3f8
|
|
BLAKE2b-256 checksum How to use checksums |
637687e7fd8fabe87ebc5fcbfa7d37eb0a90a2e5dc1ca173a04150b46ad19ede
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.2
|
Release files / messenger_api_python-2022.4.22-py3-none-any.whl
| Download URL | messenger_api_python-2022.4.22-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
02174a0dc0b169783a7419cb5b9e02e35f1dbff09e7aa7cc6fa7c7ed6d914097
|
|
BLAKE2b-256 checksum How to use checksums |
ac743f4e81e70e07fb4be48d2cbfc01be06b53ff4dc2ab75d6af6e897e8a219d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.2
|
Security for messenger-api-python 2022.4.22
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.