Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ import requests
|
|
15 |
from io import StringIO
|
16 |
from pdfminer.high_level import extract_text_to_fp
|
17 |
from pdfminer.layout import LAParams
|
18 |
-
|
19 |
import datetime
|
20 |
|
21 |
APP_START_TIME = datetime.datetime.now(datetime.timezone.utc)
|
@@ -102,20 +102,23 @@ def generate_answer_with_groq(question, context):
|
|
102 |
# --- Twilio Functions ---
|
103 |
def fetch_latest_incoming_message(client, conversation_sid):
|
104 |
try:
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
}
|
|
|
|
|
|
|
119 |
return None
|
120 |
|
121 |
def send_twilio_message(client, conversation_sid, body):
|
|
|
15 |
from io import StringIO
|
16 |
from pdfminer.high_level import extract_text_to_fp
|
17 |
from pdfminer.layout import LAParams
|
18 |
+
from twilio.base.exceptions import TwilioRestException # Add this at the top
|
19 |
import datetime
|
20 |
|
21 |
APP_START_TIME = datetime.datetime.now(datetime.timezone.utc)
|
|
|
102 |
# --- Twilio Functions ---
|
103 |
def fetch_latest_incoming_message(client, conversation_sid):
|
104 |
try:
|
105 |
+
messages = client.conversations.conversations(conversation_sid).messages.list()
|
106 |
+
for msg in reversed(messages):
|
107 |
+
if msg.author.startswith("whatsapp:"):
|
108 |
+
return {
|
109 |
+
"sid": msg.sid,
|
110 |
+
"body": msg.body,
|
111 |
+
"author": msg.author,
|
112 |
+
"timestamp": msg.date_created,
|
113 |
+
}
|
114 |
+
except TwilioRestException as e:
|
115 |
+
if e.status == 404:
|
116 |
+
print(f"Conversation {conversation_sid} not found, skipping...")
|
117 |
+
else:
|
118 |
+
print(f"Twilio error fetching messages for {conversation_sid}:", e)
|
119 |
+
except Exception as e:
|
120 |
+
print(f"Unexpected error in fetch_latest_incoming_message for {conversation_sid}:", e)
|
121 |
+
|
122 |
return None
|
123 |
|
124 |
def send_twilio_message(client, conversation_sid, body):
|