Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -124,8 +124,17 @@ def fetch_latest_incoming_message(client, conversation_sid):
|
|
124 |
try:
|
125 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list()
|
126 |
for msg in reversed(messages):
|
127 |
-
if
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
except TwilioRestException:
|
130 |
return None
|
131 |
|
@@ -135,12 +144,17 @@ def send_twilio_message(client, conversation_sid, body):
|
|
135 |
def get_latest_whatsapp_conversation_sid(client):
|
136 |
try:
|
137 |
conversations = client.conversations.v1.conversations.list(limit=20)
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
139 |
messages = convo.messages.list(limit=1)
|
140 |
-
if messages and any(m.author.startswith("whatsapp:") for m in messages):
|
141 |
return convo.sid
|
142 |
except Exception as e:
|
143 |
-
print("Error fetching
|
144 |
return None
|
145 |
|
146 |
# ---------------- Knowledge Base Setup ----------------
|
@@ -181,7 +195,7 @@ def process_messages_loop():
|
|
181 |
continue
|
182 |
|
183 |
message = fetch_latest_incoming_message(twilio_client, conversation_sid)
|
184 |
-
if message and message["sid"] not in seen_sids
|
185 |
seen_sids.add(message["sid"])
|
186 |
question = message["body"]
|
187 |
chunks = retrieve_chunks(question, index, embed_model, text_chunks)
|
@@ -196,4 +210,4 @@ st.title("ToyShop WhatsApp Assistant (Groq + Twilio)")
|
|
196 |
if st.button("Start WhatsApp Bot"):
|
197 |
thread = threading.Thread(target=process_messages_loop)
|
198 |
thread.start()
|
199 |
-
st.success("WhatsApp assistant started and monitoring for new messages.")
|
|
|
124 |
try:
|
125 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list()
|
126 |
for msg in reversed(messages):
|
127 |
+
if (
|
128 |
+
msg.author.startswith("whatsapp:") and
|
129 |
+
msg.date_created and
|
130 |
+
msg.date_created > APP_START_TIME
|
131 |
+
):
|
132 |
+
return {
|
133 |
+
"sid": msg.sid,
|
134 |
+
"body": msg.body,
|
135 |
+
"author": msg.author,
|
136 |
+
"timestamp": msg.date_created,
|
137 |
+
}
|
138 |
except TwilioRestException:
|
139 |
return None
|
140 |
|
|
|
144 |
def get_latest_whatsapp_conversation_sid(client):
|
145 |
try:
|
146 |
conversations = client.conversations.v1.conversations.list(limit=20)
|
147 |
+
# Filter conversations created after app start time
|
148 |
+
filtered = [
|
149 |
+
c for c in conversations
|
150 |
+
if c.date_created and c.date_created > APP_START_TIME
|
151 |
+
]
|
152 |
+
for convo in sorted(filtered, key=lambda c: c.date_created, reverse=True):
|
153 |
messages = convo.messages.list(limit=1)
|
154 |
+
if messages and any(m.author.startswith("whatsapp:") and m.date_created > APP_START_TIME for m in messages):
|
155 |
return convo.sid
|
156 |
except Exception as e:
|
157 |
+
print("Error fetching valid conversation SID:", e)
|
158 |
return None
|
159 |
|
160 |
# ---------------- Knowledge Base Setup ----------------
|
|
|
195 |
continue
|
196 |
|
197 |
message = fetch_latest_incoming_message(twilio_client, conversation_sid)
|
198 |
+
if message and message["sid"] not in seen_sids:
|
199 |
seen_sids.add(message["sid"])
|
200 |
question = message["body"]
|
201 |
chunks = retrieve_chunks(question, index, embed_model, text_chunks)
|
|
|
210 |
if st.button("Start WhatsApp Bot"):
|
211 |
thread = threading.Thread(target=process_messages_loop)
|
212 |
thread.start()
|
213 |
+
st.success("WhatsApp assistant started and monitoring for new messages.")
|