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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -37
app.py CHANGED
@@ -143,46 +143,50 @@ def start_conversation_monitor(client, index, embed_model, text_chunks):
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...")
167
- while True:
168
- try:
169
- conversations = client.conversations.v1.conversations.list(limit=20)
170
- for convo in conversations:
171
- convo_full = client.conversations.v1.conversations(convo.sid).fetch()
172
- if convo.sid not in processed_convos and convo_full.date_created > APP_START_TIME:
173
- participants = client.conversations.v1.conversations(convo.sid).participants.list()
174
- for p in participants:
175
- address = p.messaging_binding.get("address", "") if p.messaging_binding else ""
176
- if address.startswith("whatsapp:"):
177
- print(f"πŸ†• New WhatsApp convo found: {convo.sid}")
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
-
185
- threading.Thread(target=poll_new_conversations, daemon=True).start()
 
186
 
187
  # --- Streamlit UI ---
188
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
 
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...")
170
+ while True:
171
+ try:
172
+ conversations = client.conversations.v1.conversations.list(limit=20)
173
+ for convo in conversations:
174
+ convo_full = client.conversations.v1.conversations(convo.sid).fetch()
175
+ if convo.sid not in processed_convos and convo_full.date_created > APP_START_TIME:
176
+ participants = client.conversations.v1.conversations(convo.sid).participants.list()
177
+ for p in participants:
178
+ address = p.messaging_binding.get("address", "") if p.messaging_binding else ""
179
+ if address.startswith("whatsapp:"):
180
+ print(f"πŸ†• New WhatsApp convo found: {convo.sid}")
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 ---
192
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")