template
This commit is contained in:
parent
a109df2deb
commit
8901d99dd2
11 changed files with 380 additions and 13 deletions
|
|
@ -104,6 +104,30 @@ class WhatsAppGraphClient:
|
|||
payload["context"] = {"message_id": reply_to}
|
||||
return await self._send_message(payload)
|
||||
|
||||
async def upload_media(self, filename: str, content_type: str, data: bytes) -> str:
|
||||
"""Upload a file for a template header. Returns a media id that one send can use."""
|
||||
if not self.configured:
|
||||
raise WhatsAppGraphError("WhatsApp Graph API is not configured", status_code=503)
|
||||
url = self._graph_url(f"{self.phone_number_id}/media")
|
||||
async with httpx.AsyncClient(timeout=120.0) as client:
|
||||
response = await client.post(
|
||||
url,
|
||||
data={"messaging_product": "whatsapp", "type": content_type},
|
||||
files={"file": (filename or "header", data, content_type)},
|
||||
headers={"Authorization": f"Bearer {self.access_token}"},
|
||||
)
|
||||
try:
|
||||
body = response.json()
|
||||
except ValueError:
|
||||
body = {}
|
||||
if response.status_code >= 400:
|
||||
detail = body.get("error", {}).get("message") if isinstance(body, dict) else response.text
|
||||
raise WhatsAppGraphError(detail or "Meta did not accept the header file", status_code=response.status_code)
|
||||
media_id = str(body.get("id") or "") if isinstance(body, dict) else ""
|
||||
if not media_id:
|
||||
raise WhatsAppGraphError("Meta did not return a media id for the header file", status_code=502)
|
||||
return media_id
|
||||
|
||||
async def send_template(
|
||||
self,
|
||||
to: str,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue