A user-friendly Python wrapper for the WhatsApp Cloud API. Easily integrate WhatsApp messaging into your applications and enhance user engagement with streamlined workflows.
Project description
whatsloon
A Python wrapper facilitating seamless integration with the WhatsApp Cloud API. Streamline your messaging workflows and enhance user engagement with this efficient toolkit.
Overview
whatsloon is a robust Python SDK for the WhatsApp Cloud API. It provides a comprehensive set of mixin classes and utilities to send all supported WhatsApp message types, including text, media, interactive, template, location, reaction, sticker, and more. The package is designed for both synchronous and asynchronous workflows, with strong validation and error handling.
Features
- Send Text, Image, Video, Audio, and Document messages
- Send Interactive messages: Lists, Reply Buttons, Flows, CTA buttons
- Send Location and Location Request messages
- Send Contact and Address messages
- Send Template messages (with components)
- Send Stickers and Reactions
- Send Typing Indicators
- Mark messages as Read
- Robust validation for API limits (e.g., button/section/row limits)
- Both synchronous and asynchronous support (requests and httpx)
- Clear error handling and logging
Key Components
- Mixin Classes: Each message type (text, image, video, audio, document, list, flow, reply buttons, template, sticker, reaction, location, location request, contact, address, typing indicator, read receipt) is implemented as a robust, reusable mixin class.
- WhatsAppBaseClient: Easily compose your own client by combining mixins for only the features you need.
- Validation: All payload builders validate API limits and required fields.
- Async & Sync: All senders support both synchronous (requests) and asynchronous (httpx) usage.
Installation
pip install whatsloon
Usage Examples
1. Using WhatsAppCloudAPIClient (All Features, Easiest)
from whatsloon import WhatsAppCloudAPIClient
client = WhatsAppCloudAPIClient(
access_token="YOUR_API_KEY",
mobile_number_id="MOBILE_NUMBER_ID",
recipient_country_code="91",
recipient_mobile_number="9876543210",
)
# Synchronous usage
result = client.send_text_message("Hello, world!", preview_url=True)
print(result)
# Asynchronous usage
import asyncio
async def main():
result = await client.async_send_text_message("Hello async!", preview_url=True)
print(result)
asyncio.run(main())
# Send an image (sync)
result = client.send_image_message(media_id="MEDIA_ID", caption="A photo")
print(result)
# Send an image (async)
async def main_img():
result = await client.async_send_image_message(media_id="MEDIA_ID", caption="Async photo")
print(result)
asyncio.run(main_img())
2. Composing Your Own Client (Selected Features Only)
# Import mixins directly from the package root
from whatsloon import WhatsAppBaseClient, TextSender, ImageSender
class MyWhatsAppClient(WhatsAppBaseClient, TextSender, ImageSender):
pass
client = MyWhatsAppClient(
access_token="YOUR_API_KEY",
mobile_number_id="MOBILE_NUMBER_ID",
recipient_country_code="91",
recipient_mobile_number="9876543210",
)
# Use only the features you mix in
result = client.send_text_message("Hello, world!", preview_url=True)
print(result)
3. Without Mixins (Direct Usage)
from whatsloon import WhatsAppBaseClient
class SimpleClient(WhatsAppBaseClient):
pass
client = SimpleClient(
access_token="YOUR_API_KEY",
mobile_number_id="MOBILE_NUMBER_ID",
recipient_country_code="91",
recipient_mobile_number="9876543210",
)
# You can use the generic send_message method for custom payloads:
payload = {
"type": "text",
"text": {"body": "Hello from custom payload!"}
}
result = client.send_message(payload)
print(result)
# Async version
import asyncio
async def main():
result = await client.async_send_message(payload)
print(result)
asyncio.run(main())
Testing
whatsloon includes comprehensive tests for all mixins, message types, and integration scenarios.
-
Test Dependencies:
- Requires
pytest(and optionallypytest-asynciofor async tests) - Install with:
pip install -r requirements.txt(if provided) orpip install pytest pytest-asyncio
- Requires
-
Running All Tests:
pytest tests/ -
Running a Specific Test File:
pytest tests/test_text.py -
Test Coverage: To check coverage (if
pytest-covis installed):pytest --cov=whatsloon tests/
-
Contributing Tests:
- Add new tests in the
tests/directory, following the patterntest_<module>.py. - Each test function should include a docstring explaining its purpose and expected input/output.
- Edge cases and error handling are strongly encouraged.
- Add new tests in the
Contributing
Contributions are welcome! To contribute to whatsloon:
-
Bug Reports & Feature Requests:
- Please use the GitHub Issues page to report bugs or suggest features.
-
Pull Requests:
- Fork the repository and create a new branch for your feature or fix.
- Ensure your code follows the existing style and includes type hints and docstrings.
- Add or update tests as appropriate.
- Run all tests locally with
pytestbefore submitting. - Submit a pull request with a clear description of your changes.
-
Code Style:
- We recommend using
blackfor formatting andflake8for linting. - All public APIs should have type hints and Google-style docstrings.
- We recommend using
Thank you for helping improve whatsloon!
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 whatsloon-2.0.3.tar.gz.
File metadata
- Download URL: whatsloon-2.0.3.tar.gz
- Upload date:
- Size: 30.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79926cb851dd0b1fb8e257ed7b9bab0ebc4ca72e613b4ce11512bc1c1bbb7634
|
|
| MD5 |
1f21d722ffc1d36adec3469a8bcd95bf
|
|
| BLAKE2b-256 |
f0691f7435b8d42dbf488108122eb8eb35e6fb0b5f44988bba7b2376ddabb64d
|
File details
Details for the file whatsloon-2.0.3-py3-none-any.whl.
File metadata
- Download URL: whatsloon-2.0.3-py3-none-any.whl
- Upload date:
- Size: 38.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c31278f6664e2f304ae7a8c8b04338486cb7ee645b4e0058630eccc931572743
|
|
| MD5 |
2d8c73577e6ab636c6e6767b176079c3
|
|
| BLAKE2b-256 |
c2ce1a0328f264f78acfc2f036dc22e25647f48d9d480ef55412584fa7b13dbc
|