Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -206,43 +206,47 @@ def setup_knowledge_base():
|
|
206 |
|
207 |
# ---------------- Monitor Twilio Conversations ----------------
|
208 |
def start_conversation_monitor(client, index, embed_model, text_chunks):
|
209 |
-
#print(f"start_conversation_monitor: {client}")
|
210 |
processed_convos = set()
|
211 |
last_processed_timestamp = {}
|
212 |
|
213 |
def poll_convo(convo_sid):
|
214 |
-
print(f"
|
215 |
while True:
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
|
|
231 |
time.sleep(120)
|
232 |
|
233 |
-
conversations
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
|
|
|
|
246 |
|
247 |
|
248 |
# ---------------- Main Entry ----------------
|
@@ -261,6 +265,5 @@ if __name__ == "__main__":
|
|
261 |
st.error("β Twilio credentials not set.")
|
262 |
else:
|
263 |
client = Client(account_sid, auth_token)
|
264 |
-
#print(f"client: {client}")
|
265 |
start_conversation_monitor(client, index, model, chunks)
|
266 |
-
st.info("β
Bot is now monitoring Twilio
|
|
|
206 |
|
207 |
# ---------------- Monitor Twilio Conversations ----------------
|
208 |
def start_conversation_monitor(client, index, embed_model, text_chunks):
|
|
|
209 |
processed_convos = set()
|
210 |
last_processed_timestamp = {}
|
211 |
|
212 |
def poll_convo(convo_sid):
|
213 |
+
print(f"π§΅ Started polling for SID: {convo_sid}")
|
214 |
while True:
|
215 |
+
try:
|
216 |
+
latest_msg = fetch_latest_incoming_message(client, convo_sid)
|
217 |
+
if latest_msg:
|
218 |
+
msg_time = latest_msg["timestamp"]
|
219 |
+
prev_time = last_processed_timestamp.get(convo_sid)
|
220 |
+
|
221 |
+
if prev_time is None or msg_time > prev_time:
|
222 |
+
last_processed_timestamp[convo_sid] = msg_time
|
223 |
+
question = latest_msg["body"]
|
224 |
+
sender = latest_msg["author"]
|
225 |
+
print(f"π© New message from {sender}: {question}")
|
226 |
+
context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
|
227 |
+
answer = generate_answer_with_groq(question, context)
|
228 |
+
send_twilio_message(client, convo_sid, answer)
|
229 |
+
except Exception as e:
|
230 |
+
print(f"β οΈ Error in poll_convo: {e}")
|
231 |
time.sleep(120)
|
232 |
|
233 |
+
# Get all conversations and find the most recent one after APP_START_TIME
|
234 |
+
conversations = client.conversations.v1.conversations.list(limit=20)
|
235 |
+
sorted_convos = sorted(
|
236 |
+
[c for c in conversations if c.date_created > APP_START_TIME],
|
237 |
+
key=lambda c: c.date_created,
|
238 |
+
reverse=True
|
239 |
+
)
|
240 |
+
|
241 |
+
if not sorted_convos:
|
242 |
+
print("β No new Twilio conversations found after app startup.")
|
243 |
+
return
|
244 |
+
|
245 |
+
latest_convo = sorted_convos[0]
|
246 |
+
if latest_convo.sid not in processed_convos:
|
247 |
+
processed_convos.add(latest_convo.sid)
|
248 |
+
print(f"β
Monitoring latest conversation SID: {latest_convo.sid}, Created: {latest_convo.date_created}")
|
249 |
+
threading.Thread(target=poll_convo, args=(latest_convo.sid,), daemon=True).start()
|
250 |
|
251 |
|
252 |
# ---------------- Main Entry ----------------
|
|
|
265 |
st.error("β Twilio credentials not set.")
|
266 |
else:
|
267 |
client = Client(account_sid, auth_token)
|
|
|
268 |
start_conversation_monitor(client, index, model, chunks)
|
269 |
+
st.info("β
Bot is now monitoring the latest Twilio conversation.")
|