masadonline commited on
Commit
2aa4c5e
Β·
verified Β·
1 Parent(s): b2d8bee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -105,8 +105,14 @@ def fetch_latest_incoming_message(client, conversation_sid):
105
  messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
106
  for msg in reversed(messages):
107
  if msg.author.startswith("whatsapp:"):
108
- return msg.body, msg.author, msg.index
109
- return None, None, None
 
 
 
 
 
 
110
 
111
  def send_twilio_message(client, conversation_sid, body):
112
  return client.conversations.v1.conversations(conversation_sid).messages.create(
@@ -138,20 +144,25 @@ def start_conversation_monitor(client, index, embed_model, text_chunks):
138
  monitored_sids = set()
139
 
140
  def poll_conversation(convo_sid):
141
- while True:
142
- try:
143
- question, sender, msg_index = fetch_latest_incoming_message(client, convo_sid)
144
- if question and (convo_sid not in last_msg_index or msg_index > last_msg_index[convo_sid]):
145
- last_msg_index[convo_sid] = msg_index
 
 
 
 
 
146
  print(f"\nπŸ“₯ New message from {sender} in {convo_sid}: {question}")
147
  context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
148
  answer = generate_answer_with_groq(question, context)
149
  send_twilio_message(client, convo_sid, answer)
150
  print(f"πŸ“€ Replied to {sender}: {answer}")
151
- time.sleep(3)
152
- except Exception as e:
153
- print(f"❌ Error in convo {convo_sid} polling:", e)
154
- time.sleep(5)
155
 
156
  def monitor_all_conversations():
157
  while True:
 
105
  messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
106
  for msg in reversed(messages):
107
  if msg.author.startswith("whatsapp:"):
108
+ return {
109
+ "sid": msg.sid,
110
+ "body": msg.body,
111
+ "author": msg.author,
112
+ "timestamp": msg.date_created,
113
+ }
114
+ return None
115
+
116
 
117
  def send_twilio_message(client, conversation_sid, body):
118
  return client.conversations.v1.conversations(conversation_sid).messages.create(
 
144
  monitored_sids = set()
145
 
146
  def poll_conversation(convo_sid):
147
+ last_processed_timestamp = None
148
+ while True:
149
+ try:
150
+ latest_msg = fetch_latest_incoming_message(client, convo_sid)
151
+ if latest_msg:
152
+ msg_time = latest_msg["timestamp"]
153
+ if last_processed_timestamp is None or msg_time > last_processed_timestamp:
154
+ last_processed_timestamp = msg_time
155
+ question = latest_msg["body"]
156
+ sender = latest_msg["author"]
157
  print(f"\nπŸ“₯ New message from {sender} in {convo_sid}: {question}")
158
  context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
159
  answer = generate_answer_with_groq(question, context)
160
  send_twilio_message(client, convo_sid, answer)
161
  print(f"πŸ“€ Replied to {sender}: {answer}")
162
+ time.sleep(3)
163
+ except Exception as e:
164
+ print(f"❌ Error in convo {convo_sid} polling:", e)
165
+ time.sleep(5)
166
 
167
  def monitor_all_conversations():
168
  while True: