Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -301,62 +301,53 @@ def initialize_twilio_client(acc_sid, auth_tkn):
|
|
301 |
st.error(f"Failed to initialize Twilio client: {e}")
|
302 |
return None
|
303 |
|
304 |
-
def get_new_whatsapp_messages(twilio_client,
|
305 |
-
processed_message_sids, bot_whatsapp_identity_val):
|
306 |
if not twilio_client:
|
307 |
st.warning("Twilio client not initialized.")
|
308 |
return []
|
309 |
-
if not conversation_service_sid_val:
|
310 |
-
st.warning("Twilio Conversation Service SID not provided.")
|
311 |
-
return []
|
312 |
if not bot_whatsapp_identity_val:
|
313 |
st.warning("Twilio Bot WhatsApp Identity not provided.")
|
314 |
return []
|
315 |
|
316 |
new_messages_to_process = []
|
317 |
try:
|
318 |
-
|
319 |
-
|
320 |
-
.conversations \
|
321 |
-
.list(limit=50)
|
322 |
|
323 |
for conv in conversations:
|
324 |
if conv.date_updated and conv.date_updated > bot_start_time_utc:
|
325 |
messages = twilio_client.conversations.v1 \
|
326 |
-
.services(conversation_service_sid_val) \
|
327 |
.conversations(conv.sid) \
|
328 |
.messages \
|
329 |
-
.list(order='desc', limit=10)
|
330 |
|
331 |
for msg in messages:
|
332 |
if msg.sid in processed_message_sids:
|
333 |
-
continue
|
334 |
|
|
|
335 |
if msg.author and msg.author.lower() != bot_whatsapp_identity_val.lower() and \
|
336 |
-
msg.date_created and msg.date_created > bot_start_time_utc
|
|
|
337 |
new_messages_to_process.append({
|
338 |
"conversation_sid": conv.sid, "message_sid": msg.sid,
|
339 |
"author_identity": msg.author, "message_body": msg.body,
|
340 |
"timestamp_utc": msg.date_created
|
341 |
})
|
342 |
-
break
|
343 |
except Exception as e:
|
344 |
st.error(f"Error fetching Twilio messages: {e}")
|
345 |
return sorted(new_messages_to_process, key=lambda m: m['timestamp_utc'])
|
346 |
|
347 |
-
def send_whatsapp_message(twilio_client,
|
348 |
if not twilio_client:
|
349 |
st.error("Twilio client not initialized for sending message.")
|
350 |
return False
|
351 |
-
if not conversation_service_sid_val:
|
352 |
-
st.error("Twilio Conversation Service SID not provided for sending message.")
|
353 |
-
return False
|
354 |
if not bot_identity_val:
|
355 |
st.error("Bot identity not provided for sending message.")
|
356 |
return False
|
357 |
try:
|
358 |
twilio_client.conversations.v1 \
|
359 |
-
.services(conversation_service_sid_val) \
|
360 |
.conversations(conversation_sid) \
|
361 |
.messages \
|
362 |
.create(author=bot_identity_val, body=message_body)
|
@@ -657,18 +648,17 @@ if st.session_state.get("bot_started") and st.session_state.get("rag_pipeline_re
|
|
657 |
if (current_time - st.session_state.last_twilio_poll_time) > polling_interval_to_use:
|
658 |
st.session_state.last_twilio_poll_time = current_time
|
659 |
|
660 |
-
if not st.session_state.get("twilio_client") or
|
661 |
-
|
662 |
-
not twilio_bot_whatsapp_identity_to_use or \
|
663 |
-
not st.session_state.get("bot_start_time_utc"):
|
664 |
-
st.warning("Twilio client/config missing for polling. Ensure bot is started and SIDs are set.")
|
665 |
else:
|
666 |
with st.spinner(f"Checking WhatsApp messages (last poll: {datetime.fromtimestamp(st.session_state.last_twilio_poll_time).strftime('%H:%M:%S')})..."):
|
667 |
-
new_messages = get_new_whatsapp_messages(
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
|
|
|
|
672 |
if new_messages:
|
673 |
st.info(f"Found {len(new_messages)} new WhatsApp message(s) to process.")
|
674 |
for msg_data in new_messages:
|
@@ -757,8 +747,12 @@ if st.session_state.get("bot_started") and st.session_state.get("rag_pipeline_re
|
|
757 |
order_status=wa_order_status
|
758 |
)
|
759 |
|
760 |
-
if send_whatsapp_message(
|
761 |
-
|
|
|
|
|
|
|
|
|
762 |
st.session_state.processed_message_sids.add(msg_sid)
|
763 |
st.success(f"Successfully responded to WhatsApp message SID {msg_sid} from {author_id}.")
|
764 |
else:
|
|
|
301 |
st.error(f"Failed to initialize Twilio client: {e}")
|
302 |
return None
|
303 |
|
304 |
+
def get_new_whatsapp_messages(twilio_client, bot_start_time_utc, processed_message_sids, bot_whatsapp_identity_val):
|
|
|
305 |
if not twilio_client:
|
306 |
st.warning("Twilio client not initialized.")
|
307 |
return []
|
|
|
|
|
|
|
308 |
if not bot_whatsapp_identity_val:
|
309 |
st.warning("Twilio Bot WhatsApp Identity not provided.")
|
310 |
return []
|
311 |
|
312 |
new_messages_to_process = []
|
313 |
try:
|
314 |
+
# Get all conversations (not limited to a specific service)
|
315 |
+
conversations = twilio_client.conversations.v1.conversations.list(limit=50)
|
|
|
|
|
316 |
|
317 |
for conv in conversations:
|
318 |
if conv.date_updated and conv.date_updated > bot_start_time_utc:
|
319 |
messages = twilio_client.conversations.v1 \
|
|
|
320 |
.conversations(conv.sid) \
|
321 |
.messages \
|
322 |
+
.list(order='desc', limit=10)
|
323 |
|
324 |
for msg in messages:
|
325 |
if msg.sid in processed_message_sids:
|
326 |
+
continue
|
327 |
|
328 |
+
# Check if message is from WhatsApp and not from the bot
|
329 |
if msg.author and msg.author.lower() != bot_whatsapp_identity_val.lower() and \
|
330 |
+
msg.date_created and msg.date_created > bot_start_time_utc and \
|
331 |
+
msg.author.startswith('whatsapp:'):
|
332 |
new_messages_to_process.append({
|
333 |
"conversation_sid": conv.sid, "message_sid": msg.sid,
|
334 |
"author_identity": msg.author, "message_body": msg.body,
|
335 |
"timestamp_utc": msg.date_created
|
336 |
})
|
337 |
+
break
|
338 |
except Exception as e:
|
339 |
st.error(f"Error fetching Twilio messages: {e}")
|
340 |
return sorted(new_messages_to_process, key=lambda m: m['timestamp_utc'])
|
341 |
|
342 |
+
def send_whatsapp_message(twilio_client, conversation_sid, message_body, bot_identity_val):
|
343 |
if not twilio_client:
|
344 |
st.error("Twilio client not initialized for sending message.")
|
345 |
return False
|
|
|
|
|
|
|
346 |
if not bot_identity_val:
|
347 |
st.error("Bot identity not provided for sending message.")
|
348 |
return False
|
349 |
try:
|
350 |
twilio_client.conversations.v1 \
|
|
|
351 |
.conversations(conversation_sid) \
|
352 |
.messages \
|
353 |
.create(author=bot_identity_val, body=message_body)
|
|
|
648 |
if (current_time - st.session_state.last_twilio_poll_time) > polling_interval_to_use:
|
649 |
st.session_state.last_twilio_poll_time = current_time
|
650 |
|
651 |
+
if not st.session_state.get("twilio_client") or not twilio_bot_whatsapp_identity_to_use or not st.session_state.get("bot_start_time_utc"):
|
652 |
+
st.warning("Twilio client/config missing for polling. Ensure bot is started and WhatsApp identity is set.")
|
|
|
|
|
|
|
653 |
else:
|
654 |
with st.spinner(f"Checking WhatsApp messages (last poll: {datetime.fromtimestamp(st.session_state.last_twilio_poll_time).strftime('%H:%M:%S')})..."):
|
655 |
+
new_messages = get_new_whatsapp_messages(
|
656 |
+
st.session_state.twilio_client,
|
657 |
+
st.session_state.bot_start_time_utc,
|
658 |
+
st.session_state.processed_message_sids,
|
659 |
+
twilio_bot_whatsapp_identity_to_use
|
660 |
+
)
|
661 |
+
|
662 |
if new_messages:
|
663 |
st.info(f"Found {len(new_messages)} new WhatsApp message(s) to process.")
|
664 |
for msg_data in new_messages:
|
|
|
747 |
order_status=wa_order_status
|
748 |
)
|
749 |
|
750 |
+
if send_whatsapp_message(
|
751 |
+
st.session_state.twilio_client,
|
752 |
+
conv_sid,
|
753 |
+
response_whatsapp,
|
754 |
+
twilio_bot_whatsapp_identity_to_use
|
755 |
+
):
|
756 |
st.session_state.processed_message_sids.add(msg_sid)
|
757 |
st.success(f"Successfully responded to WhatsApp message SID {msg_sid} from {author_id}.")
|
758 |
else:
|