masadonline commited on
Commit
0b29458
Β·
verified Β·
1 Parent(s): 6c85feb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
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
- messages = client.conversations.v1.conversations(convo_sid).messages.list(limit=50)
151
- for msg in reversed(messages):
152
- if msg.date_created > last_processed and msg.author != "system":
153
- text = msg.body.strip()
154
- print(f"πŸ“© Received message: {text}")
155
- last_processed_timestamp[convo_sid] = msg.date_created
156
-
157
- # Query response
158
- result = query_index(text, index, embed_model, text_chunks)
159
-
160
- # Send reply
161
- client.conversations.v1.conversations(convo_sid).messages.create(
162
- author="system", body=result
163
- )
164
  except Exception as e:
165
- print("❌ Error in poll_conversation:", e)
166
- time.sleep(3)
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 new conversations:", e)
185
  time.sleep(5)
186
 
187
- # βœ… Start background thread to monitor new conversations
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 ---