Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
161 |
except Exception as e:
|
162 |
-
print(
|
163 |
-
|
164 |
-
|
165 |
-
def poll_new_conversations():
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
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")
|