Skip to main content

No project description provided

Project description

django-whatsapp-api-wrapper

Um wrapper simples para enviar mensagens via WhatsApp Cloud API e expor um endpoint de webhook, pronto para integrar em qualquer projeto Django.

Instalação

python -m pip install django-whatsapp-api-wrapper

Configuração (Django)

  1. Adicione o app em INSTALLED_APPS:
INSTALLED_APPS = [
    # ...
    "django_whatsapp_api_wrapper",
]
  1. Inclua as URLs no urls.py principal:
from django.urls import path, include

urlpatterns = [
    # ...
    path("whatsapp-api-wrapper/", include("django_whatsapp_api_wrapper.urls")),
]
  1. Defina as variáveis de ambiente (ou no seu .env):
TOKEN=
PACKAGE_VERSION=0.1.1
API_VERSION=v23.0
PHONE_NUMBER_ID=
WHATSAPP_VERIFY_TOKEN=

O endpoint de webhook ficará disponível em:

  • GET/POST: /whatsapp-api-wrapper/webhook/
  • Verificação (GET): /whatsapp-api-wrapper/webhook/?hub.mode=subscribe&hub.verify_token=<TOKEN>&hub.challenge=123

Extensibilidade do Webhook

Você pode customizar o processamento do webhook no projeto hospedeiro de duas formas:

  • Via setting com handler plugável:
# settings.py
WHATSAPP_WEBHOOK_HANDLER = "meuapp.whatsapp.handle_webhook"
# meuapp/whatsapp.py
from django.http import JsonResponse

def handle_webhook(request, payload):
    # sua lógica aqui (salvar eventos, acionar tasks, etc)
    return JsonResponse({"ok": True})
  • Via signal webhook_event_received:
from django.dispatch import receiver
from django_whatsapp_api_wrapper.signals import webhook_event_received

@receiver(webhook_event_received)
def on_whatsapp_event(sender, payload, request, **kwargs):
    # sua lógica aqui
    pass

Uso Rápido (envio de mensagens)

from django_whatsapp_api_wrapper import WhatsApp

wp = WhatsApp()

# Template
language = {"code": "pt_BR"}
template = {"name": "opa", "language": language}
m = wp.build_message(to="5521980340830", type="template", data=template)
m.send()

# Texto
data = {"preview_url": False, "body": "será que só funciona?"}
m2 = wp.build_message(to="+5521994740431", type="text", data=data)
m2.send()

Notas

  • Nome do pacote no PyPI: django-whatsapp-api-wrapper
  • Nome do módulo/import: django_whatsapp_api_wrapper

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

django_whatsapp_api_wrapper-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

django_whatsapp_api_wrapper-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file django_whatsapp_api_wrapper-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_whatsapp_api_wrapper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6473bab829f75487e6eda6cf9edcdc8b15a132f3303f18bb769964fb1891ec10
MD5 7b71624f51cef3278d3ded98d6acc8fa
BLAKE2b-256 79447fa15fb2d2e20fcd5f0ee90ff36eea6c5c9041f94b1ce6ab8063fc949523

See more details on using hashes here.

File details

Details for the file django_whatsapp_api_wrapper-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_whatsapp_api_wrapper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17d367e738525600a4aa881801d755fb4773516c553268c19986dcb829604250
MD5 7b481d6f0fdd7206f851c48f361d8933
BLAKE2b-256 406f2175b6aa4ae02e0f97d7ee6c06db6966285af4e97316521e05c87dc4f94e

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