masadonline commited on
Commit
3e432a3
Β·
verified Β·
1 Parent(s): a7618f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -32
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"poll_convo before while: {convo_sid}")
215
  while True:
216
- print(f"poll_convo Twilio SID: {convo_sid}")
217
- latest_msg = fetch_latest_incoming_message(client, convo_sid)
218
- if latest_msg:
219
- msg_time = latest_msg["timestamp"]
220
- print(f"msg_time: {msg_time}")
221
- print(f"last_processed_timestamp: {last_processed_timestamp}")
222
- print(f"last_processed_timestamp[convo_sid]: {last_processed_timestamp[convo_sid]}")
223
- if convo_sid not in last_processed_timestamp or msg_time > last_processed_timestamp[convo_sid]:
224
- last_processed_timestamp[convo_sid] = msg_time
225
- question = latest_msg["body"]
226
- sender = latest_msg["author"]
227
- print(f"πŸ“© New message from {sender}: {question}")
228
- context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
229
- answer = generate_answer_with_groq(question, context)
230
- send_twilio_message(client, convo_sid, answer)
 
231
  time.sleep(120)
232
 
233
- conversations = client.conversations.v1.conversations.list(limit=1)
234
-
235
- for convo in conversations:
236
- print(f"convo.sid: {convo.sid}")
237
- print(f"convo.date_created: {convo.date_created}")
238
- print(f"APP_START_TIME: {APP_START_TIME}")
239
- print(f"processed_convos: {processed_convos}")
240
-
241
- # Filter out conversations created before app startup
242
- if convo.date_created > APP_START_TIME:
243
- processed_convos.add(convo.sid)
244
- print(f"πŸ” Monitoring conversation SID: {convo.sid}, Created at: {convo.date_created}")
245
- threading.Thread(target=poll_convo, args=(convo.sid,), daemon=True).start()
 
 
 
 
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 conversations.")
 
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.")