@@ -234,25 +234,26 @@ async def response_to_user_whatsapp(message: str, from_number: str, body, intro_
234
234
chat_response = send_message_to_khoj_chat (user_message , from_number )
235
235
236
236
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 :
256
257
data = make_whatsapp_payload (chat_response_text , from_number )
257
258
response = whatsapp_cloud_api_session .post (url , json = data )
258
259
response .raise_for_status ()
0 commit comments