|
5 | 5 | from requests import Session
|
6 | 6 | import time
|
7 | 7 | import uuid
|
| 8 | +from io import BytesIO |
| 9 | +from PIL import Image |
8 | 10 |
|
9 | 11 | # External Packages
|
10 | 12 | from fastapi import APIRouter, status, Request, BackgroundTasks
|
@@ -241,13 +243,14 @@ async def response_to_user_whatsapp(message: str, from_number: str, body, intro_
|
241 | 243 | response = requests.get(media_url)
|
242 | 244 | response.raise_for_status()
|
243 | 245 |
|
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") |
246 | 249 |
|
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() |
251 | 254 | os.remove(filepath)
|
252 | 255 | else:
|
253 | 256 | data = make_whatsapp_payload(chat_response_text, from_number)
|
|
0 commit comments