mirror of
http://88.130.71.182:3000/BlitTech/contexta_be.git
synced 2026-06-12 23:23:21 +00:00
updates Mar6
This commit is contained in:
36
app/services/whatsapp_service.py
Normal file
36
app/services/whatsapp_service.py
Normal file
@@ -0,0 +1,36 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user