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.

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.3.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.3-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

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

Hashes for whatsloon-2.0.3.tar.gz
Algorithm Hash digest
SHA256 79926cb851dd0b1fb8e257ed7b9bab0ebc4ca72e613b4ce11512bc1c1bbb7634
MD5 1f21d722ffc1d36adec3469a8bcd95bf
BLAKE2b-256 f0691f7435b8d42dbf488108122eb8eb35e6fb0b5f44988bba7b2376ddabb64d

See more details on using hashes here.

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

Hashes for whatsloon-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c31278f6664e2f304ae7a8c8b04338486cb7ee645b4e0058630eccc931572743
MD5 2d8c73577e6ab636c6e6767b176079c3
BLAKE2b-256 c2ce1a0328f264f78acfc2f036dc22e25647f48d9d480ef55412584fa7b13dbc

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