Skip to main content

A user-friendly Python wrapper for the WhatsApp Cloud API. Easily integrate WhatsApp messaging into your applications and enhance user engagement with streamlined workflows.

Reason this release was yanked:

Will have to install 'httpx' manually

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.

PyPI Downloads PyPI Downloads PyPI Downloads

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 optionally pytest-asyncio for async tests)
    • Install with: pip install -r requirements.txt (if provided) or pip install pytest pytest-asyncio
  • Running All Tests:

    pytest tests/
    
  • Running a Specific Test File:

    pytest tests/test_text.py
    
  • Test Coverage: To check coverage (if pytest-cov is installed):

    pytest --cov=whatsloon tests/
    
  • Contributing Tests:

    • Add new tests in the tests/ directory, following the pattern test_<module>.py.
    • Each test function should include a docstring explaining its purpose and expected input/output.
    • Edge cases and error handling are strongly encouraged.

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 pytest before submitting.
    • Submit a pull request with a clear description of your changes.
  • Code Style:

    • We recommend using black for formatting and flake8 for linting.
    • All public APIs should have type hints and Google-style docstrings.

Thank you for helping improve whatsloon!

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

whatsloon-2.0.1.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whatsloon-2.0.1-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

File details

Details for the file whatsloon-2.0.1.tar.gz.

File metadata

  • Download URL: whatsloon-2.0.1.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

Hashes for whatsloon-2.0.1.tar.gz
Algorithm Hash digest
SHA256 2f0dc5b92b1963b493c8657f4730614963e10a5585e3b5fb47d1d556cda48d41
MD5 c91911f2991578e6bad20a5861946297
BLAKE2b-256 ace2b42fbf71ae7b4f7deeeb318fb3134db28f251c0627d106d84a8482c32ce7

See more details on using hashes here.

File details

Details for the file whatsloon-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: whatsloon-2.0.1-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

Hashes for whatsloon-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df9b6964f14e161d44d2d2cdd79b5f633d3e18fb48b32b831bfc1697ae21c8da
MD5 01fb14b350d8f9ebacb0a7716fa5008c
BLAKE2b-256 2afcd7278af96178a971633f879e420135006528c79248146bca76c923c5d50a

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