kanha-upadhyay commited on
Commit
d104988
·
1 Parent(s): c54772b

Add message polling for new responses from the assistant

Browse files
Files changed (1) hide show
  1. app.py +18 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import logging
2
  import os
3
  import sqlite3
4
  import threading
 
5
  import urllib.parse
6
  from http.server import BaseHTTPRequestHandler, HTTPServer
7
 
@@ -131,6 +132,23 @@ if prompt := st.chat_input("Type your message..."):
131
  client.messages.create(
132
  body=prompt, from_=TWILIO_PHONE_NUMBER, to=KORA_PHONE_NUMBER
133
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  except sqlite3.Error as e:
135
  st.error(f"Error saving message to database: {e}")
136
  except Exception as e:
 
2
  import os
3
  import sqlite3
4
  import threading
5
+ import time
6
  import urllib.parse
7
  from http.server import BaseHTTPRequestHandler, HTTPServer
8
 
 
132
  client.messages.create(
133
  body=prompt, from_=TWILIO_PHONE_NUMBER, to=KORA_PHONE_NUMBER
134
  )
135
+
136
+ # Periodically check for new messages from the assistant
137
+ def check_for_new_messages():
138
+ while True:
139
+ try:
140
+ with db_lock:
141
+ latest_message = c.execute(
142
+ "SELECT sender, text FROM messages ORDER BY timestamp DESC LIMIT 1"
143
+ ).fetchone()
144
+ if latest_message and latest_message[0] == "assistant":
145
+ st.chat_message("assistant").write(latest_message[1])
146
+ break
147
+ except sqlite3.Error as e:
148
+ logging.error(f"Error checking for new messages: {e}")
149
+ time.sleep(5)
150
+
151
+ check_for_new_messages()
152
  except sqlite3.Error as e:
153
  st.error(f"Error saving message to database: {e}")
154
  except Exception as e: