Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -143,27 +143,24 @@ def start_conversation_monitor(client, index, embed_model, text_chunks):
|
|
143 |
last_processed_timestamp = {}
|
144 |
|
145 |
def poll_conversation(convo_sid):
|
146 |
-
print(f"π§΅ Polling conversation: {convo_sid}")
|
147 |
-
last_processed = last_processed_timestamp.get(convo_sid, APP_START_TIME)
|
148 |
while True:
|
149 |
try:
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
)
|
164 |
except Exception as e:
|
165 |
-
print("β Error in
|
166 |
-
|
167 |
|
168 |
def poll_new_conversations():
|
169 |
print("β‘οΈ Monitoring for new WhatsApp conversations...")
|
@@ -181,11 +178,12 @@ def start_conversation_monitor(client, index, embed_model, text_chunks):
|
|
181 |
processed_convos.add(convo.sid)
|
182 |
threading.Thread(target=poll_conversation, args=(convo.sid,), daemon=True).start()
|
183 |
except Exception as e:
|
184 |
-
print("β Error polling
|
185 |
time.sleep(5)
|
186 |
|
187 |
-
# β
|
188 |
-
threading.Thread(target=poll_new_conversations, daemon=True).start()
|
|
|
189 |
|
190 |
|
191 |
# --- Streamlit UI ---
|
|
|
143 |
last_processed_timestamp = {}
|
144 |
|
145 |
def poll_conversation(convo_sid):
|
|
|
|
|
146 |
while True:
|
147 |
try:
|
148 |
+
latest_msg = fetch_latest_incoming_message(client, convo_sid)
|
149 |
+
if latest_msg:
|
150 |
+
msg_time = latest_msg["timestamp"]
|
151 |
+
if convo_sid not in last_processed_timestamp or msg_time > last_processed_timestamp[convo_sid]:
|
152 |
+
last_processed_timestamp[convo_sid] = msg_time
|
153 |
+
question = latest_msg["body"]
|
154 |
+
sender = latest_msg["author"]
|
155 |
+
print(f"\nπ₯ New message from {sender} in {convo_sid}: {question}")
|
156 |
+
context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
|
157 |
+
answer = generate_answer_with_groq(question, context)
|
158 |
+
send_twilio_message(client, convo_sid, answer)
|
159 |
+
print(f"π€ Replied to {sender}: {answer}")
|
160 |
+
time.sleep(3)
|
|
|
161 |
except Exception as e:
|
162 |
+
print(f"β Error in convo {convo_sid} polling:", e)
|
163 |
+
time.sleep(5)
|
164 |
|
165 |
def poll_new_conversations():
|
166 |
print("β‘οΈ Monitoring for new WhatsApp conversations...")
|
|
|
178 |
processed_convos.add(convo.sid)
|
179 |
threading.Thread(target=poll_conversation, args=(convo.sid,), daemon=True).start()
|
180 |
except Exception as e:
|
181 |
+
print("β Error polling conversations:", e)
|
182 |
time.sleep(5)
|
183 |
|
184 |
+
# β
Launch conversation polling monitor
|
185 |
+
threading.Thread(target=poll_new_conversations, daemon=True).start()
|
186 |
+
|
187 |
|
188 |
|
189 |
# --- Streamlit UI ---
|