Skip to main content

Biblioteca Python para integração com a API Oficial do WhatsApp (Cloud API).

Project description

WhatsApp Cloud API Client (Python)

Biblioteca Python para integracao com a API Oficial do WhatsApp (Cloud API), com foco em simplicidade de uso em apps.

Instalacao

pip install whatsapp-cloud-api-client

Para desenvolvimento local:

pip install -e ".[dev]"

Uso rapido (sync)

from whatsapp_cloud_api import WhatsAppClient

client = WhatsAppClient(
    access_token="SEU_TOKEN",
    phone_number_id="SEU_PHONE_NUMBER_ID",
)

resp = client.send_text(
    to="5511999999999",
    body="Ola! Mensagem enviada via Cloud API.",
)
print(resp.model_dump())

Uso rapido (async)

import asyncio
from whatsapp_cloud_api import AsyncWhatsAppClient


async def main() -> None:
    async with AsyncWhatsAppClient(
        access_token="SEU_TOKEN",
        phone_number_id="SEU_PHONE_NUMBER_ID",
    ) as client:
        resp = await client.send_text(
            to="5511999999999",
            body="Mensagem async",
        )
        print(resp.model_dump())


asyncio.run(main())

Modelos tipados (Pydantic)

Os metodos retornam modelos Pydantic:

  • SendMessageResponse
  • MediaUploadResponse
  • MediaInfoResponse
  • MarkAsReadResponse

Retry, backoff e rate limit

O cliente possui retry configuravel para erros transientes (429, 500, 502, 503, 504).

Por padrao, retry roda apenas para GET para evitar duplicidade em envio de mensagem (POST).

from whatsapp_cloud_api import WhatsAppClient

client = WhatsAppClient(
    access_token="SEU_TOKEN",
    phone_number_id="SEU_PHONE_NUMBER_ID",
    max_retries=3,
    backoff_factor=0.5,
    max_backoff=8.0,
    retry_methods={"GET", "POST"},  # habilite POST se quiser retry em envio
)

Se a API retornar Retry-After, esse valor sera respeitado.

Funcionalidades implementadas

  • Envio de mensagem de texto
  • Envio de mensagem template
  • Envio de midia por media_id ou link (imagem, documento, video, audio, sticker)
  • Upload de midia
  • Marcar mensagem como lida
  • Busca de informacoes de midia
  • Validacao de assinatura de webhook (X-Hub-Signature-256)
  • Cliente sincrono e assincrono

Exemplo com Flask (webhook)

from flask import Flask, request, jsonify
from whatsapp_cloud_api import verify_webhook_signature, verify_webhook_challenge

app = Flask(__name__)
APP_SECRET = "SEU_APP_SECRET"
VERIFY_TOKEN = "SEU_VERIFY_TOKEN"


@app.get("/webhook")
def webhook_verify():
    ok, challenge = verify_webhook_challenge(
        mode=request.args.get("hub.mode"),
        token=request.args.get("hub.verify_token"),
        challenge=request.args.get("hub.challenge"),
        verify_token=VERIFY_TOKEN,
    )
    if not ok:
        return "forbidden", 403
    return challenge, 200


@app.post("/webhook")
def webhook_receive():
    if not verify_webhook_signature(
        app_secret=APP_SECRET,
        raw_body=request.get_data(),
        x_hub_signature_256=request.headers.get("X-Hub-Signature-256", ""),
    ):
        return "invalid signature", 401

    data = request.get_json(silent=True) or {}
    return jsonify({"ok": True, "received": bool(data)}), 200

Rodar testes

pytest

CI e publicacao

  • CI: .github/workflows/ci.yml
  • Criacao de tag/release: .github/workflows/release.yml
  • Publicacao PyPI: .github/workflows/publish.yml

Fluxo recomendado:

  1. Execute o workflow Create Release e informe a tag (ex: v0.2.0).
  2. O release publicado dispara Publish to PyPI.
  3. Configure Trusted Publisher no PyPI para este repositorio (OIDC), sem token manual.

Opcional: se preferir token, adapte o workflow para usar PYPI_API_TOKEN.

Licenca

MIT

Documentacao completa

  • Guia de uso: docs/GUIA_DE_USO.md

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

whatsapp_cloud_api_client-0.2.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

whatsapp_cloud_api_client-0.2.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file whatsapp_cloud_api_client-0.2.1.tar.gz.

File metadata

File hashes

Hashes for whatsapp_cloud_api_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 30e11c50f51f2bdd9aadc04791ed31657cad744cc4c4eb6a6a1070b35635f71e
MD5 245c3e71a86d99dccc4be6dc66b89f70
BLAKE2b-256 225ae6aee5409faaecfeb86139a4edb30f0291517eb74b852d2bdd2981bbd2b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for whatsapp_cloud_api_client-0.2.1.tar.gz:

Publisher: publish.yml on cfartes/api_whatsapp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file whatsapp_cloud_api_client-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for whatsapp_cloud_api_client-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 feb152cc079ca0bfb26754afd0a16863b31cf527878066d4af2c432abec9a275
MD5 75b5a6194e1db92b8fd2f2f8a8b4a2eb
BLAKE2b-256 b930dd8f77428b4b4eab7ce4fe6ff87db02b4b7504e2a1cd2e52e77c16a44ec2

See more details on using hashes here.

Provenance

The following attestation bundles were made for whatsapp_cloud_api_client-0.2.1-py3-none-any.whl:

Publisher: publish.yml on cfartes/api_whatsapp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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