Python package for WAHA (WhatsApp HTTP API)
Project description
WAHA - Python
A comprehensive Python package for the WAHA (WhatsApp HTTP API) service. This package provides a clean, type-safe, and easy-to-use interface for interacting with WhatsApp through the WAHA API.
๐ Features
- ๐ Full API Coverage: Complete implementation of all WAHA API endpoints
- โก Async & Sync Support: Both synchronous and asynchronous operation modes
- ๐ก๏ธ Type Safety: Full typing with Pydantic models for request/response validation
- ๐ฏ Organized Namespaces: Intuitive organization by functionality (auth, sessions, messages, etc.)
- ๐ Rich Documentation: Comprehensive docstrings and examples
- ๐งช Well Tested: Extensive test coverage with pytest
- ๐ Modern Python: Support for Python 3.8+ with modern async/await patterns
๐ Quick Start
Installation
pip install waha
Basic Usage
from waha import WahaClient
# Initialize the client
client = WahaClient(
base_url="http://localhost:3000",
api_key="your-api-key" # Optional, can use WAHA_API_KEY env var
)
# Start a session
session = client.sessions.start("default")
# Send a text message
message = client.messages.send_text(
session="default",
chat_id="1234567890@c.us",
text="Hello from WAHA! ๐"
)
print(f"Message sent: {message.id}")
Async Usage
import asyncio
from waha import AsyncWahaClient
async def main():
async with AsyncWahaClient(
base_url="http://localhost:3000",
api_key="your-api-key"
) as client:
# Start a session
session = await client.sessions.start("default")
# Send a message
message = await client.messages.send_text(
session="default",
chat_id="1234567890@c.us",
text="Hello from async WAHA! ๐"
)
print(f"Message sent: {message.id}")
asyncio.run(main())
๐ Documentation
Core Components
The SDK is organized into several namespaces, each handling a specific area of functionality:
๐ Authentication (client.auth)
Handle QR code authentication and phone verification:
# Get QR code for authentication
qr_code = client.auth.get_qr_code("default", format="raw")
print(f"QR Code: {qr_code.qr}")
# Request authentication code
client.auth.request_code("default", phone_number="+1234567890")
๐ฅ๏ธ Sessions (client.sessions)
Manage WhatsApp sessions:
# Create and start a session
session = client.sessions.create("my-session", start=True)
# List all sessions
sessions = client.sessions.list()
# Get session info
info = client.sessions.get("default")
# Stop a session
client.sessions.stop("default")
# Get authenticated account info
me = client.sessions.get_me("default")
print(f"Logged in as: {me.pushName} ({me.id})")
๐ค Messages (client.messages)
Send various types of messages:
# Send text message
client.messages.send_text(
chat_id="1234567890@c.us",
text="Hello! ๐",
reply_to="message_id_to_reply_to" # Optional
)
# Send image
client.messages.send_image(
chat_id="1234567890@c.us",
file="https://example.com/image.jpg",
caption="Check this out!"
)
# Send document
client.messages.send_file(
chat_id="1234567890@c.us",
file="https://example.com/document.pdf",
caption="Important document"
)
# Send location
client.messages.send_location(
chat_id="1234567890@c.us",
latitude=40.7128,
longitude=-74.0060,
title="New York City"
)
# Send contact
from waha.types import ContactVcard
contact = ContactVcard(
fullName="John Doe",
phoneNumber="+1234567890",
email="john@example.com"
)
client.messages.send_contact_vcard(
chat_id="1234567890@c.us",
contacts=[contact]
)
# Send poll
client.messages.send_poll(
chat_id="1234567890@c.us",
poll_text="What's your favorite color?",
options=["Red", "Green", "Blue", "Yellow"]
)
# Send buttons
buttons = [
{"id": "btn1", "title": "Option 1"},
{"id": "btn2", "title": "Option 2"},
]
client.messages.send_buttons(
chat_id="1234567890@c.us",
text="Choose an option:",
buttons=buttons
)
# React to a message
client.messages.react(
message_id="some_message_id",
reaction="๐"
)
# Forward a message
client.messages.forward_message(
message_id="source_message_id",
chat_id="target_chat_id"
)
๐ฌ Chats (client.chats)
Manage chats and retrieve messages:
# List chats
chats = client.chats.list(limit=50)
# Get messages from a chat
messages = client.chats.get_messages(
chat_id="1234567890@c.us",
limit=100
)
๐ค Contacts (client.contacts)
Manage contacts:
# Check if a number exists on WhatsApp
result = client.contacts.check_exists("+1234567890")
if result.numberExists:
print(f"Number exists! Chat ID: {result.chatId}")
๐ Profile (client.profile)
Manage your WhatsApp profile:
# Get profile info
profile = client.profile.get()
print(f"Name: {profile.name}, Status: {profile.status}")
# Update profile name
client.profile.set_name("New Name")
# Update profile status
client.profile.set_status("Available")
# Set profile picture
client.profile.set_picture("https://example.com/avatar.jpg")
๐ฅ Groups (client.groups)
Manage WhatsApp groups:
# Create a group
group = client.groups.create(
name="My Group",
participants=["1234567890@c.us", "0987654321@c.us"]
)
# Add participants
client.groups.add_participants(
group_id="group_id@g.us",
participants=["newuser@c.us"]
)
๐ง Configuration
Environment Variables
You can configure the SDK using environment variables:
export WAHA_URL="http://localhost:3000"
export WAHA_API_KEY="your-api-key"
Client Configuration
client = WahaClient(
base_url="http://localhost:3000",
api_key="your-api-key",
timeout=30.0, # Request timeout in seconds
headers={"Custom-Header": "value"} # Additional headers
)
๐ File Handling
The SDK supports multiple ways to handle files:
Using URLs
client.messages.send_image(
chat_id="1234567890@c.us",
file="https://example.com/image.jpg"
)
Using Base64
from waha.types import Base64File
file = Base64File(
mimetype="image/jpeg",
filename="image.jpg",
data="base64_encoded_data_here"
)
client.messages.send_image(
chat_id="1234567890@c.us",
file=file
)
Using URLFile Objects
from waha.types import URLFile
file = URLFile(
url="https://example.com/image.jpg",
filename="custom_name.jpg"
)
client.messages.send_image(
chat_id="1234567890@c.us",
file=file
)
๐จ Error Handling
The SDK provides specific exception types for different error scenarios:
from waha.exceptions import (
WahaException,
WahaAPIError,
WahaTimeoutError,
WahaAuthenticationError,
WahaSessionError
)
try:
message = client.messages.send_text(
chat_id="invalid_chat_id",
text="Hello"
)
except WahaAuthenticationError:
print("Authentication failed - check your API key")
except WahaAPIError as e:
print(f"API error: {e.message} (Status: {e.status_code})")
except WahaTimeoutError:
print("Request timed out")
except WahaException as e:
print(f"General WAHA error: {e}")
๐งช Testing
Run the test suite:
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=waha_sdk
# Run specific test file
pytest tests/test_client.py
๐ Development
Setting up for development:
# Clone the repository
git clone https://github.com/devlikeapro/waha-python.git
cd waha-python
# Install in development mode
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
# Run tests
pytest
# Format code
black waha tests examples
# Type checking
mypy waha
Project Structure
waha-python/
โโโ waha/
โ โโโ __init__.py
โ โโโ client.py # Main client classes
โ โโโ exceptions.py # Custom exceptions
โ โโโ types.py # Pydantic models
โ โโโ http_client.py # HTTP client implementation
โ โโโ namespaces/ # API namespaces
โ โโโ auth.py
โ โโโ sessions.py
โ โโโ messages.py
โ โโโ ...
โโโ tests/ # Test suite
โโโ examples/ # Usage examples
โโโ pyproject.toml # Package configuration
โโโ README.md
๐ Requirements
- Python 3.8+
- httpx >= 0.24.0
- pydantic >= 2.0.0
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Ensure all tests pass (
pytest) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Links
- WAHA Documentation: https://waha.devlike.pro
- WAHA GitHub: https://github.com/devlikeapro/waha
- PyPI Package: https://pypi.org/project/waha/
- Issues: https://github.com/devlikeapro/waha-python/issues
๐ Support
If you encounter any issues or have questions:
- Check the documentation
- Search existing issues
- Create a new issue with detailed information about your problem
๐ฏ Roadmap
- Add webhook handling utilities
- Implement retry logic with exponential backoff
- Add rate limiting support
- Create CLI tool for common operations
- Add more comprehensive examples
- Support for WhatsApp Business API features
๐ Acknowledgments
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
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 waha-1.0.0.tar.gz.
File metadata
- Download URL: waha-1.0.0.tar.gz
- Upload date:
- Size: 27.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d4bffa43a2dc5d2b26359d382dfa3796965963eb2e082b6f8faa9744b946b48
|
|
| MD5 |
65309622d9215b8837f5c853983e228e
|
|
| BLAKE2b-256 |
ae29591e155794dffe8ae84fff555509a82033fa5653b54e5fd3f6815a4cdc99
|
File details
Details for the file waha-1.0.0-py3-none-any.whl.
File metadata
- Download URL: waha-1.0.0-py3-none-any.whl
- Upload date:
- Size: 26.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37762f8b4f0766c08eaf83da55937f28b77dafc4327ed44e4b2c83c9eede5f35
|
|
| MD5 |
1f322a2dfb958c8a8c79133080fb1ec5
|
|
| BLAKE2b-256 |
e76d849afbbde4e006a7c7e6cab3d140879636d2110130546b96d71c80f42b51
|