Skip to content

Commit e766f58

Browse files
committed
Convert image from webp to png before writing it to the file system and then sending it in the whatsapp response
1 parent b7e7e89 commit e766f58

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/flint/routers/api.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from requests import Session
66
import time
77
import uuid
8+
from io import BytesIO
9+
from PIL import Image
810

911
# External Packages
1012
from fastapi import APIRouter, status, Request, BackgroundTasks
@@ -241,13 +243,14 @@ async def response_to_user_whatsapp(message: str, from_number: str, body, intro_
241243
response = requests.get(media_url)
242244
response.raise_for_status()
243245

244-
with open(filepath, "wb") as f:
245-
f.write(response.content)
246+
# The incoming image is a link to a webp image. We need to convert it to a png image.
247+
image = Image.open(BytesIO(response.content))
248+
image.save(filepath, "PNG")
246249

247-
media_id = upload_media_to_whatsapp(filepath, "image/png", phone_number_id)
248-
data = make_whatsapp_image_payload(media_id, from_number)
249-
response = whatsapp_cloud_api_session.post(url, json=data)
250-
response.raise_for_status()
250+
media_id = upload_media_to_whatsapp(filepath, "image/png", phone_number_id)
251+
data = make_whatsapp_image_payload(media_id, from_number)
252+
response = whatsapp_cloud_api_session.post(url, json=data)
253+
response.raise_for_status()
251254
os.remove(filepath)
252255
else:
253256
data = make_whatsapp_payload(chat_response_text, from_number)

0 commit comments

Comments
 (0)