38146-vm/core/wablas.py
2026-02-04 06:00:35 +00:00

45 lines
1.5 KiB
Python

import httpx
from .models import WablasConfiguration
def send_whatsapp_message(phone_number, message):
"""
Sends a WhatsApp message using the Wablas API.
"""
try:
settings = WablasConfiguration.objects.first()
if not settings or not settings.api_token:
print("Wablas settings not configured.")
return False
# Wablas API usually takes: phone, message
# And Authorization header.
headers = {
"Authorization": settings.api_token,
}
# Depending on the specific Wablas endpoint version, payload might differ.
# Common v2/v3 payload:
payload = {
"phone": phone_number,
"message": message,
}
# If secret is used, sometimes it's passed in body
if settings.secret_key:
payload["secret"] = settings.secret_key
# Using a timeout to prevent hanging
response = httpx.post(settings.api_url, data=payload, headers=headers, timeout=10)
if response.status_code >= 200 and response.status_code < 300:
print(f"WhatsApp message sent to {phone_number}: {response.json()}")
return True
else:
print(f"Failed to send WhatsApp message. Status: {response.status_code}, Body: {response.text}")
return False
except Exception as e:
print(f"Error sending WhatsApp message: {e}")
return False