Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -888,33 +888,47 @@ def split_message_for_whatsapp(message: str, max_length: int = 1000) -> list:
|
|
888 |
return [message[i:i+max_length] for i in range(0, len(message), max_length)]
|
889 |
|
890 |
def send_whatsjet_message(phone_number: str, message: str, media_type: str = None, media_path: str = None, filename: str = None) -> bool:
|
891 |
-
"""Send a message using WhatsJet API with optional media attachment"""
|
892 |
if not all([WHATSJET_API_URL, WHATSJET_VENDOR_UID, WHATSJET_API_TOKEN]):
|
893 |
logger.error("[WhatsJet] Missing environment variables.")
|
894 |
return False
|
895 |
|
896 |
url = f"{WHATSJET_API_URL}/{WHATSJET_VENDOR_UID}/contact/send-message?token={WHATSJET_API_TOKEN}"
|
897 |
|
898 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
899 |
if media_type and media_path:
|
900 |
try:
|
901 |
# For public URLs, download the content first
|
902 |
if isinstance(media_path, str) and (media_path.startswith("http://") or media_path.startswith("https://")):
|
903 |
-
# Download from URL
|
904 |
response = requests.get(media_path, stream=True, timeout=15)
|
905 |
response.raise_for_status()
|
906 |
media_content = response.content
|
907 |
-
# Log the first 20 bytes and content type
|
908 |
logger.info(f"[WhatsJet][DEBUG] Downloaded image content-type: {response.headers.get('Content-Type')}")
|
909 |
logger.info(f"[WhatsJet][DEBUG] First 20 bytes: {media_content[:20]}")
|
910 |
else:
|
911 |
-
# Read from local file
|
912 |
with open(media_path, 'rb') as f:
|
913 |
media_content = f.read()
|
914 |
-
|
915 |
-
# Encode to base64
|
916 |
media_b64 = base64.b64encode(media_content).decode('utf-8')
|
917 |
-
|
918 |
payload = {
|
919 |
"phone_number": phone_number,
|
920 |
"message_body": message,
|
@@ -922,7 +936,6 @@ def send_whatsjet_message(phone_number: str, message: str, media_type: str = Non
|
|
922 |
'media_content': media_b64,
|
923 |
'media_filename': filename or os.path.basename(media_path) if not media_path.startswith('http') else filename or 'image.jpg'
|
924 |
}
|
925 |
-
|
926 |
try:
|
927 |
response = httpx.post(
|
928 |
url,
|
|
|
888 |
return [message[i:i+max_length] for i in range(0, len(message), max_length)]
|
889 |
|
890 |
def send_whatsjet_message(phone_number: str, message: str, media_type: str = None, media_path: str = None, filename: str = None) -> bool:
|
891 |
+
"""Send a message using WhatsJet API with optional media attachment (now supports header_image for images)"""
|
892 |
if not all([WHATSJET_API_URL, WHATSJET_VENDOR_UID, WHATSJET_API_TOKEN]):
|
893 |
logger.error("[WhatsJet] Missing environment variables.")
|
894 |
return False
|
895 |
|
896 |
url = f"{WHATSJET_API_URL}/{WHATSJET_VENDOR_UID}/contact/send-message?token={WHATSJET_API_TOKEN}"
|
897 |
|
898 |
+
# Use WhatsJet's header_image for images with captions
|
899 |
+
if media_type in ["image/jpeg", "image/png"] and media_path and (media_path.startswith("http://") or media_path.startswith("https://")):
|
900 |
+
payload = {
|
901 |
+
"phone_number": phone_number,
|
902 |
+
"header_image": media_path,
|
903 |
+
"message_body": message
|
904 |
+
}
|
905 |
+
try:
|
906 |
+
response = httpx.post(
|
907 |
+
url,
|
908 |
+
json=payload,
|
909 |
+
timeout=15
|
910 |
+
)
|
911 |
+
response.raise_for_status()
|
912 |
+
logger.info(f"[WhatsJet] header_image message sent successfully to {phone_number}")
|
913 |
+
return True
|
914 |
+
except Exception as e:
|
915 |
+
logger.error(f"[WhatsJet] Exception sending header_image message: {e}")
|
916 |
+
return False
|
917 |
+
|
918 |
+
# Handle other media messages (existing logic)
|
919 |
if media_type and media_path:
|
920 |
try:
|
921 |
# For public URLs, download the content first
|
922 |
if isinstance(media_path, str) and (media_path.startswith("http://") or media_path.startswith("https://")):
|
|
|
923 |
response = requests.get(media_path, stream=True, timeout=15)
|
924 |
response.raise_for_status()
|
925 |
media_content = response.content
|
|
|
926 |
logger.info(f"[WhatsJet][DEBUG] Downloaded image content-type: {response.headers.get('Content-Type')}")
|
927 |
logger.info(f"[WhatsJet][DEBUG] First 20 bytes: {media_content[:20]}")
|
928 |
else:
|
|
|
929 |
with open(media_path, 'rb') as f:
|
930 |
media_content = f.read()
|
|
|
|
|
931 |
media_b64 = base64.b64encode(media_content).decode('utf-8')
|
|
|
932 |
payload = {
|
933 |
"phone_number": phone_number,
|
934 |
"message_body": message,
|
|
|
936 |
'media_content': media_b64,
|
937 |
'media_filename': filename or os.path.basename(media_path) if not media_path.startswith('http') else filename or 'image.jpg'
|
938 |
}
|
|
|
939 |
try:
|
940 |
response = httpx.post(
|
941 |
url,
|