Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -941,12 +941,25 @@ def send_whatsjet_message(phone_number: str, message: str, media_type: str = Non
|
|
941 |
}
|
942 |
|
943 |
logger.info(f"[WhatsJet] Attempting multipart upload for {phone_number}")
|
|
|
|
|
|
|
944 |
response = httpx.post(
|
945 |
url,
|
946 |
data=data,
|
947 |
files=files,
|
948 |
timeout=30
|
949 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
950 |
response.raise_for_status()
|
951 |
logger.info(f"[WhatsJet] Media image sent successfully via multipart upload to {phone_number}")
|
952 |
|
@@ -982,11 +995,28 @@ def send_whatsjet_message(phone_number: str, message: str, media_type: str = Non
|
|
982 |
}
|
983 |
|
984 |
logger.info(f"[WhatsJet] Attempting base64 upload for {phone_number}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
985 |
response = httpx.post(
|
986 |
url,
|
987 |
json=payload,
|
988 |
timeout=30
|
989 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
990 |
response.raise_for_status()
|
991 |
logger.info(f"[WhatsJet] Media image sent successfully via base64 to {phone_number}")
|
992 |
|
@@ -3949,6 +3979,110 @@ async def debug_whatsjet():
|
|
3949 |
except Exception as e:
|
3950 |
return {"error": str(e), "timestamp": datetime.now().isoformat()}
|
3951 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3952 |
if __name__ == "__main__":
|
3953 |
# Launch FastAPI app
|
3954 |
import uvicorn
|
|
|
941 |
}
|
942 |
|
943 |
logger.info(f"[WhatsJet] Attempting multipart upload for {phone_number}")
|
944 |
+
logger.info(f"[WhatsJet] Multipart payload - data: {data}")
|
945 |
+
logger.info(f"[WhatsJet] Multipart payload - files: {list(files.keys())}")
|
946 |
+
|
947 |
response = httpx.post(
|
948 |
url,
|
949 |
data=data,
|
950 |
files=files,
|
951 |
timeout=30
|
952 |
)
|
953 |
+
|
954 |
+
# Log response details for debugging
|
955 |
+
logger.info(f"[WhatsJet] Multipart response status: {response.status_code}")
|
956 |
+
logger.info(f"[WhatsJet] Multipart response headers: {dict(response.headers)}")
|
957 |
+
try:
|
958 |
+
response_text = response.text
|
959 |
+
logger.info(f"[WhatsJet] Multipart response body: {response_text[:500]}...")
|
960 |
+
except:
|
961 |
+
logger.info(f"[WhatsJet] Multipart response body: Unable to read")
|
962 |
+
|
963 |
response.raise_for_status()
|
964 |
logger.info(f"[WhatsJet] Media image sent successfully via multipart upload to {phone_number}")
|
965 |
|
|
|
995 |
}
|
996 |
|
997 |
logger.info(f"[WhatsJet] Attempting base64 upload for {phone_number}")
|
998 |
+
logger.info(f"[WhatsJet] Base64 payload keys: {list(payload.keys())}")
|
999 |
+
logger.info(f"[WhatsJet] Base64 payload - phone_number: {payload['phone_number']}")
|
1000 |
+
logger.info(f"[WhatsJet] Base64 payload - media_type: {payload['media_type']}")
|
1001 |
+
logger.info(f"[WhatsJet] Base64 payload - media_filename: {payload['media_filename']}")
|
1002 |
+
logger.info(f"[WhatsJet] Base64 payload - message_body: '{payload['message_body']}'")
|
1003 |
+
logger.info(f"[WhatsJet] Base64 payload - media_content length: {len(payload['media_content'])} chars")
|
1004 |
+
|
1005 |
response = httpx.post(
|
1006 |
url,
|
1007 |
json=payload,
|
1008 |
timeout=30
|
1009 |
)
|
1010 |
+
|
1011 |
+
# Log response details for debugging
|
1012 |
+
logger.info(f"[WhatsJet] Base64 response status: {response.status_code}")
|
1013 |
+
logger.info(f"[WhatsJet] Base64 response headers: {dict(response.headers)}")
|
1014 |
+
try:
|
1015 |
+
response_text = response.text
|
1016 |
+
logger.info(f"[WhatsJet] Base64 response body: {response_text[:500]}...")
|
1017 |
+
except:
|
1018 |
+
logger.info(f"[WhatsJet] Base64 response body: Unable to read")
|
1019 |
+
|
1020 |
response.raise_for_status()
|
1021 |
logger.info(f"[WhatsJet] Media image sent successfully via base64 to {phone_number}")
|
1022 |
|
|
|
3979 |
except Exception as e:
|
3980 |
return {"error": str(e), "timestamp": datetime.now().isoformat()}
|
3981 |
|
3982 |
+
@app.get("/test-whatsjet-payloads")
|
3983 |
+
async def test_whatsjet_payloads(phone: str):
|
3984 |
+
"""
|
3985 |
+
Test different WhatsJet API payload formats to identify the correct one for image sending.
|
3986 |
+
"""
|
3987 |
+
if not all([WHATSJET_API_URL, WHATSJET_VENDOR_UID, WHATSJET_API_TOKEN]):
|
3988 |
+
return {"error": "WhatsJet not configured"}
|
3989 |
+
|
3990 |
+
url = f"{WHATSJET_API_URL}/{WHATSJET_VENDOR_UID}/contact/send-message?token={WHATSJET_API_TOKEN}"
|
3991 |
+
test_image_url = "https://www.w3schools.com/w3images/lights.jpg"
|
3992 |
+
|
3993 |
+
# Download test image
|
3994 |
+
try:
|
3995 |
+
response = requests.get(test_image_url, timeout=10)
|
3996 |
+
response.raise_for_status()
|
3997 |
+
image_content = response.content
|
3998 |
+
image_b64 = base64.b64encode(image_content).decode('utf-8')
|
3999 |
+
except Exception as e:
|
4000 |
+
return {"error": f"Failed to download test image: {e}"}
|
4001 |
+
|
4002 |
+
results = {}
|
4003 |
+
|
4004 |
+
# Test different payload formats
|
4005 |
+
test_payloads = [
|
4006 |
+
{
|
4007 |
+
"name": "Format 1: Standard with media_content",
|
4008 |
+
"payload": {
|
4009 |
+
"phone_number": phone,
|
4010 |
+
"media_type": "image/jpeg",
|
4011 |
+
"media_content": image_b64,
|
4012 |
+
"media_filename": "test.jpg",
|
4013 |
+
"message_body": ""
|
4014 |
+
}
|
4015 |
+
},
|
4016 |
+
{
|
4017 |
+
"name": "Format 2: With media_url instead of media_content",
|
4018 |
+
"payload": {
|
4019 |
+
"phone_number": phone,
|
4020 |
+
"media_type": "image/jpeg",
|
4021 |
+
"media_url": test_image_url,
|
4022 |
+
"media_filename": "test.jpg",
|
4023 |
+
"message_body": ""
|
4024 |
+
}
|
4025 |
+
},
|
4026 |
+
{
|
4027 |
+
"name": "Format 3: With file field",
|
4028 |
+
"payload": {
|
4029 |
+
"phone_number": phone,
|
4030 |
+
"file": image_b64,
|
4031 |
+
"file_type": "image/jpeg",
|
4032 |
+
"filename": "test.jpg",
|
4033 |
+
"message_body": ""
|
4034 |
+
}
|
4035 |
+
},
|
4036 |
+
{
|
4037 |
+
"name": "Format 4: With attachment field",
|
4038 |
+
"payload": {
|
4039 |
+
"phone_number": phone,
|
4040 |
+
"attachment": image_b64,
|
4041 |
+
"attachment_type": "image/jpeg",
|
4042 |
+
"attachment_name": "test.jpg",
|
4043 |
+
"message_body": ""
|
4044 |
+
}
|
4045 |
+
},
|
4046 |
+
{
|
4047 |
+
"name": "Format 5: With image field",
|
4048 |
+
"payload": {
|
4049 |
+
"phone_number": phone,
|
4050 |
+
"image": image_b64,
|
4051 |
+
"image_type": "image/jpeg",
|
4052 |
+
"image_name": "test.jpg",
|
4053 |
+
"message_body": ""
|
4054 |
+
}
|
4055 |
+
}
|
4056 |
+
]
|
4057 |
+
|
4058 |
+
for test in test_payloads:
|
4059 |
+
try:
|
4060 |
+
logger.info(f"[WhatsJet] Testing payload format: {test['name']}")
|
4061 |
+
response = httpx.post(url, json=test['payload'], timeout=30)
|
4062 |
+
|
4063 |
+
results[test['name']] = {
|
4064 |
+
"status_code": response.status_code,
|
4065 |
+
"success": response.status_code == 200,
|
4066 |
+
"response_body": response.text[:200] if response.text else "No response body"
|
4067 |
+
}
|
4068 |
+
|
4069 |
+
logger.info(f"[WhatsJet] {test['name']} - Status: {response.status_code}")
|
4070 |
+
|
4071 |
+
except Exception as e:
|
4072 |
+
results[test['name']] = {
|
4073 |
+
"status_code": "Error",
|
4074 |
+
"success": False,
|
4075 |
+
"error": str(e)
|
4076 |
+
}
|
4077 |
+
logger.error(f"[WhatsJet] {test['name']} - Error: {e}")
|
4078 |
+
|
4079 |
+
return {
|
4080 |
+
"timestamp": datetime.now().isoformat(),
|
4081 |
+
"phone": phone,
|
4082 |
+
"test_image_url": test_image_url,
|
4083 |
+
"results": results
|
4084 |
+
}
|
4085 |
+
|
4086 |
if __name__ == "__main__":
|
4087 |
# Launch FastAPI app
|
4088 |
import uvicorn
|