Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -105,8 +105,14 @@ def fetch_latest_incoming_message(client, conversation_sid):
|
|
105 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
|
106 |
for msg in reversed(messages):
|
107 |
if msg.author.startswith("whatsapp:"):
|
108 |
-
return
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
def send_twilio_message(client, conversation_sid, body):
|
112 |
return client.conversations.v1.conversations(conversation_sid).messages.create(
|
@@ -138,20 +144,25 @@ def start_conversation_monitor(client, index, embed_model, text_chunks):
|
|
138 |
monitored_sids = set()
|
139 |
|
140 |
def poll_conversation(convo_sid):
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
146 |
print(f"\nπ₯ New message from {sender} in {convo_sid}: {question}")
|
147 |
context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
|
148 |
answer = generate_answer_with_groq(question, context)
|
149 |
send_twilio_message(client, convo_sid, answer)
|
150 |
print(f"π€ Replied to {sender}: {answer}")
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
|
156 |
def monitor_all_conversations():
|
157 |
while True:
|
|
|
105 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
|
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 |
+
return None
|
115 |
+
|
116 |
|
117 |
def send_twilio_message(client, conversation_sid, body):
|
118 |
return client.conversations.v1.conversations(conversation_sid).messages.create(
|
|
|
144 |
monitored_sids = set()
|
145 |
|
146 |
def poll_conversation(convo_sid):
|
147 |
+
last_processed_timestamp = None
|
148 |
+
while True:
|
149 |
+
try:
|
150 |
+
latest_msg = fetch_latest_incoming_message(client, convo_sid)
|
151 |
+
if latest_msg:
|
152 |
+
msg_time = latest_msg["timestamp"]
|
153 |
+
if last_processed_timestamp is None or msg_time > last_processed_timestamp:
|
154 |
+
last_processed_timestamp = msg_time
|
155 |
+
question = latest_msg["body"]
|
156 |
+
sender = latest_msg["author"]
|
157 |
print(f"\nπ₯ New message from {sender} in {convo_sid}: {question}")
|
158 |
context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
|
159 |
answer = generate_answer_with_groq(question, context)
|
160 |
send_twilio_message(client, convo_sid, answer)
|
161 |
print(f"π€ Replied to {sender}: {answer}")
|
162 |
+
time.sleep(3)
|
163 |
+
except Exception as e:
|
164 |
+
print(f"β Error in convo {convo_sid} polling:", e)
|
165 |
+
time.sleep(5)
|
166 |
|
167 |
def monitor_all_conversations():
|
168 |
while True:
|