Skip to content

Commit 13bff8e

Browse files
committed
When processing the response, handle it differently if it's just a text response
1 parent 1f8943c commit 13bff8e

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/flint/routers/api.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,25 +234,26 @@ async def response_to_user_whatsapp(message: str, from_number: str, body, intro_
234234
chat_response = send_message_to_khoj_chat(user_message, from_number)
235235

236236
if chat_response.get("response"):
237-
chat_response_text = chat_response["response"]
238-
if chat_response_text.get("image"):
239-
media_url = chat_response_text["image"]
240-
if media_url:
241-
# Write the file to a tmp directory
242-
filepath = f"/tmp/{int(time.time() * 1000)}.png"
243-
response = requests.get(media_url)
244-
response.raise_for_status()
245-
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")
249-
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()
254-
os.remove(filepath)
255-
else:
237+
chat_response_text = chat_response.get("response")
238+
try:
239+
if chat_response_text.get("image"):
240+
media_url = chat_response_text["image"]
241+
if media_url:
242+
# Write the file to a tmp directory
243+
filepath = f"/tmp/{int(time.time() * 1000)}.png"
244+
response = requests.get(media_url)
245+
response.raise_for_status()
246+
247+
# The incoming image is a link to a webp image. We need to convert it to a png image.
248+
image = Image.open(BytesIO(response.content))
249+
image.save(filepath, "PNG")
250+
251+
media_id = upload_media_to_whatsapp(filepath, "image/png", phone_number_id)
252+
data = make_whatsapp_image_payload(media_id, from_number)
253+
response = whatsapp_cloud_api_session.post(url, json=data)
254+
response.raise_for_status()
255+
os.remove(filepath)
256+
except AttributeError:
256257
data = make_whatsapp_payload(chat_response_text, from_number)
257258
response = whatsapp_cloud_api_session.post(url, json=data)
258259
response.raise_for_status()

src/flint/routers/dev.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ async def chat_dev(
2727

2828
if chat_response.get("response"):
2929
response = chat_response.get("response")
30-
if response.get("image"):
31-
encoded_img = response["image"]
32-
if encoded_img:
33-
# Write the file to a tmp directory
34-
filepath = f"/tmp/{int(time.time() * 1000)}.png"
35-
with open(filepath, "wb") as f:
36-
f.write(base64.b64decode(encoded_img))
37-
chat_response_text = f"Image saved to {filepath}"
38-
else:
30+
# Try parsing to see if the response is a JSON object
31+
try:
32+
if response.get("image"):
33+
encoded_img = response["image"]
34+
if encoded_img:
35+
# Write the file to a tmp directory
36+
filepath = f"/tmp/{int(time.time() * 1000)}.png"
37+
with open(filepath, "wb") as f:
38+
f.write(base64.b64decode(encoded_img))
39+
chat_response_text = f"Image saved to {filepath}"
40+
except AttributeError:
3941
chat_response_text = chat_response["response"]
4042
elif chat_response.get("detail"):
4143
chat_response_text = chat_response["detail"]

0 commit comments

Comments
 (0)