Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -888,59 +888,38 @@ 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 |
-
# Handle
|
899 |
-
if media_type in ["image/jpeg", "image/png"] and media_path:
|
900 |
-
# If media_path is a URL, send as WhatsApp API expects
|
901 |
-
if isinstance(media_path, str) and (media_path.startswith("http://") or media_path.startswith("https://")):
|
902 |
-
payload = {
|
903 |
-
"messaging_product": "whatsapp",
|
904 |
-
"recipient_type": "individual",
|
905 |
-
"to": phone_number,
|
906 |
-
"type": "image",
|
907 |
-
"image": {
|
908 |
-
"link": media_path,
|
909 |
-
"caption": message[:1024] # WhatsApp caption limit
|
910 |
-
}
|
911 |
-
}
|
912 |
-
try:
|
913 |
-
response = httpx.post(
|
914 |
-
url,
|
915 |
-
json=payload,
|
916 |
-
timeout=15
|
917 |
-
)
|
918 |
-
response.raise_for_status()
|
919 |
-
logger.info(f"[WhatsJet] WhatsApp image+caption message sent successfully to {phone_number}")
|
920 |
-
return True
|
921 |
-
except Exception as e:
|
922 |
-
logger.error(f"[WhatsJet] Exception sending WhatsApp image+caption: {e}")
|
923 |
-
return False
|
924 |
-
else:
|
925 |
-
# If media_path is a local file, fallback to text only and log warning
|
926 |
-
logger.warning(f"[WhatsJet] Local file for image+caption not supported for WhatsApp API. Sending text only to {phone_number}.")
|
927 |
-
if message.strip():
|
928 |
-
return send_whatsjet_message(phone_number, message)
|
929 |
-
return False
|
930 |
-
|
931 |
-
# Handle other media messages (existing logic)
|
932 |
if media_type and media_path:
|
933 |
try:
|
934 |
-
|
935 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
936 |
media_b64 = base64.b64encode(media_content).decode('utf-8')
|
|
|
937 |
payload = {
|
938 |
"phone_number": phone_number,
|
939 |
"message_body": message,
|
940 |
'media_type': media_type,
|
941 |
'media_content': media_b64,
|
942 |
-
'media_filename': filename or os.path.basename(media_path)
|
943 |
}
|
|
|
944 |
try:
|
945 |
response = httpx.post(
|
946 |
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"""
|
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 |
+
# Handle media messages (including images with captions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
else:
|
908 |
+
# Read from local file
|
909 |
+
with open(media_path, 'rb') as f:
|
910 |
+
media_content = f.read()
|
911 |
+
|
912 |
+
# Encode to base64
|
913 |
media_b64 = base64.b64encode(media_content).decode('utf-8')
|
914 |
+
|
915 |
payload = {
|
916 |
"phone_number": phone_number,
|
917 |
"message_body": message,
|
918 |
'media_type': media_type,
|
919 |
'media_content': media_b64,
|
920 |
+
'media_filename': filename or os.path.basename(media_path) if not media_path.startswith('http') else filename or 'image.jpg'
|
921 |
}
|
922 |
+
|
923 |
try:
|
924 |
response = httpx.post(
|
925 |
url,
|