Skip to content

Commit b7e7e89

Browse files
committed
Update khoj message handler to use the new response format for images
1 parent a927d1a commit b7e7e89

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/flint/routers/api.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,26 @@ async def response_to_user_whatsapp(message: str, from_number: str, body, intro_
233233

234234
if chat_response.get("response"):
235235
chat_response_text = chat_response["response"]
236-
data = make_whatsapp_payload(chat_response_text, from_number)
237-
response = whatsapp_cloud_api_session.post(url, json=data)
238-
response.raise_for_status()
239-
elif chat_response.get("image"):
240-
media_url = chat_response["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()
236+
if chat_response_text.get("image"):
237+
media_url = chat_response_text["image"]
238+
if media_url:
239+
# Write the file to a tmp directory
240+
filepath = f"/tmp/{int(time.time() * 1000)}.png"
241+
response = requests.get(media_url)
242+
response.raise_for_status()
246243

247-
with open(filepath, "wb") as f:
248-
f.write(response.content)
244+
with open(filepath, "wb") as f:
245+
f.write(response.content)
249246

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)
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()
251+
os.remove(filepath)
252+
else:
253+
data = make_whatsapp_payload(chat_response_text, from_number)
254+
response = whatsapp_cloud_api_session.post(url, json=data)
255+
response.raise_for_status()
255256
elif chat_response.get("detail"):
256257
chat_response_text = chat_response["detail"]
257258
data = make_whatsapp_payload(chat_response_text, from_number)

src/flint/routers/dev.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ async def chat_dev(
2525
) -> Response:
2626
chat_response = send_message_to_khoj_chat(body, phone_number)
2727

28-
if chat_response.get("image"):
29-
encoded_img = chat_response["image"]
30-
if encoded_img:
31-
# Write the file to a tmp directory
32-
filepath = f"/tmp/{int(time.time() * 1000)}.png"
33-
with open(filepath, "wb") as f:
34-
f.write(base64.b64decode(encoded_img))
35-
chat_response_text = f"Image saved to {filepath}"
36-
elif chat_response.get("response"):
37-
chat_response_text = chat_response["response"]
28+
if chat_response.get("response"):
29+
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:
39+
chat_response_text = chat_response["response"]
3840
elif chat_response.get("detail"):
3941
chat_response_text = chat_response["detail"]
4042

0 commit comments

Comments
 (0)