masadonline commited on
Commit
fb6f517
Β·
verified Β·
1 Parent(s): 0ee59bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -100,6 +100,22 @@ def generate_answer_with_groq(question, context, retries=3, delay=2):
100
  except Exception as e:
101
  return f"⚠️ Groq API Error: {e}"
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  # Twilio message fetch and send
104
  def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
105
  client = Client(account_sid, auth_token)
@@ -134,7 +150,17 @@ if not all([account_sid, auth_token, GROQ_API_KEY]):
134
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
135
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
136
 
137
- conversation_sid = st.text_input("Enter Conversation SID", value="")
 
 
 
 
 
 
 
 
 
 
138
 
139
  enable_autorefresh = st.checkbox("πŸ”„ Enable Auto-Refresh", value=True)
140
  interval_seconds = st.selectbox("Refresh Interval (seconds)", options=[5, 10, 15, 30, 60], index=1)
 
100
  except Exception as e:
101
  return f"⚠️ Groq API Error: {e}"
102
 
103
+ # Twilio Automatically fetches and uses the latest active WhatsApp conversation SID
104
+ def get_latest_whatsapp_conversation_sid(account_sid, auth_token):
105
+ try:
106
+ client = Client(account_sid, auth_token)
107
+ conversations = client.conversations.v1.conversations.list(limit=20)
108
+ for convo in conversations:
109
+ messages = client.conversations.v1.conversations(convo.sid).messages.list(limit=10)
110
+ for msg in messages:
111
+ if msg.author.startswith("whatsapp:"):
112
+ return convo.sid
113
+ return None
114
+ except Exception as e:
115
+ st.error(f"❌ Error fetching conversation SID: {e}")
116
+ return None
117
+
118
+
119
  # Twilio message fetch and send
120
  def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
121
  client = Client(account_sid, auth_token)
 
150
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
151
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
152
 
153
+ #conversation_sid = st.text_input("Enter Conversation SID", value="")
154
+ auto_detect = st.checkbox("πŸ“‘ Auto-detect Conversation SID", value=True)
155
+
156
+ if auto_detect:
157
+ conversation_sid = get_latest_whatsapp_conversation_sid(account_sid, auth_token)
158
+ if conversation_sid:
159
+ st.success(f"βœ… Using detected Conversation SID: `{conversation_sid}`")
160
+ else:
161
+ st.warning("⚠️ No active WhatsApp conversation found.")
162
+ else:
163
+ conversation_sid = st.text_input("Enter Conversation SID manually", value="")
164
 
165
  enable_autorefresh = st.checkbox("πŸ”„ Enable Auto-Refresh", value=True)
166
  interval_seconds = st.selectbox("Refresh Interval (seconds)", options=[5, 10, 15, 30, 60], index=1)