masadonline commited on
Commit
71f4ee8
·
verified ·
1 Parent(s): 7dc3b90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
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
- messages = client.conversations.conversations(convo_sid).messages.list()
106
- except TwilioRestException as e:
107
- if e.status == 404:
108
- print(f"Conversation {convo_sid} not found, skipping...")
109
- continue
110
- #messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
111
- for msg in reversed(messages):
112
- if msg.author.startswith("whatsapp:"):
113
- return {
114
- "sid": msg.sid,
115
- "body": msg.body,
116
- "author": msg.author,
117
- "timestamp": msg.date_created,
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):