import httpx import hashlib import hmac import logging logger = logging.getLogger(__name__) _META_API = "https://graph.facebook.com/v19.0" async def send_message(phone_number_id: str, to: str, text: str, access_token: str) -> bool: try: async with httpx.AsyncClient(timeout=10) as client: r = await client.post( f"{_META_API}/{phone_number_id}/messages", headers={"Authorization": f"Bearer {access_token}"}, json={ "messaging_product": "whatsapp", "to": to, "type": "text", "text": {"body": text}, }, ) return r.status_code == 200 except Exception as e: logger.error(f"WhatsApp send error: {e}") return False def verify_signature(payload: bytes, signature: str, app_secret: str) -> bool: expected = "sha256=" + hmac.new( app_secret.encode("utf-8"), payload, hashlib.sha256, ).hexdigest() return hmac.compare_digest(expected, signature)