Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from twilio.rest import Client
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Set up Streamlit app
|
7 |
+
st.title("Twilio Conversation Viewer")
|
8 |
+
|
9 |
+
# Get Twilio credentials from Streamlit secrets
|
10 |
+
account_sid = st.secrets["TWILIO_SID"]
|
11 |
+
auth_token = st.secrets["TWILIO_TOKEN"]
|
12 |
+
conversation_sid = st.secrets["TWILIO_CONVERSATION_SID"]
|
13 |
+
|
14 |
+
client = Client(account_sid, auth_token)
|
15 |
+
|
16 |
+
def fetch_messages(conversation_sid):
|
17 |
+
"""Fetches messages from the Twilio conversation."""
|
18 |
+
try:
|
19 |
+
messages = client.conversations.v1.conversations(conversation_sid).messages.list()
|
20 |
+
return list(messages)
|
21 |
+
except Exception as e:
|
22 |
+
st.error(f"Error fetching messages: {e}")
|
23 |
+
return []
|
24 |
+
|
25 |
+
def display_messages(messages):
|
26 |
+
"""Displays messages in the Streamlit app."""
|
27 |
+
for message in messages:
|
28 |
+
if message.author == 'system':
|
29 |
+
st.markdown(f'<div style="background-color:#f0f2f6;padding:10px;border-radius:5px;margin-bottom:5px;"><strong>System:</strong> {message.body}</div>', unsafe_allow_html=True)
|
30 |
+
else:
|
31 |
+
st.markdown(f'<div style="background-color:#e1f5fe;padding:10px;border-radius:5px;margin-bottom:5px;"><strong>{message.author}:</strong> {message.body}</div>', unsafe_allow_html=True)
|
32 |
+
|
33 |
+
def send_message(conversation_sid, author, body):
|
34 |
+
"""Sends a new message to the Twilio conversation."""
|
35 |
+
try:
|
36 |
+
message = client.conversations.v1.conversations(conversation_sid).messages.create(author=author, body=body)
|
37 |
+
st.success(f"Message sent (SID: {message.sid})")
|
38 |
+
except Exception as e:
|
39 |
+
st.error(f"Error sending message: {e}")
|
40 |
+
|
41 |
+
# Sidebar for configuration and actions
|
42 |
+
with st.sidebar:
|
43 |
+
st.header("Conversation Actions")
|
44 |
+
new_message = st.text_input("Enter new message:")
|
45 |
+
message_author = st.text_input("Your identifier (e.g., user1):", "user")
|
46 |
+
if st.button("Send Message"):
|
47 |
+
if new_message:
|
48 |
+
send_message(conversation_sid, message_author, new_message)
|
49 |
+
# Give a short delay to see the updated messages
|
50 |
+
time.sleep(1)
|
51 |
+
st.rerun() # Refresh the app to show the new message
|
52 |
+
|
53 |
+
st.header("Real-time Updates")
|
54 |
+
refresh_rate = st.slider("Refresh interval (seconds):", 1, 10, 3)
|
55 |
+
|
56 |
+
# Main area to display messages
|
57 |
+
st.subheader("Conversation History")
|
58 |
+
|
59 |
+
while True:
|
60 |
+
messages = fetch_messages(conversation_sid)
|
61 |
+
if messages:
|
62 |
+
display_messages(messages)
|
63 |
+
else:
|
64 |
+
st.info("No messages in this conversation yet.")
|
65 |
+
time.sleep(refresh_rate)
|
66 |
+
st.empty() # Clear the previous messages for the next update
|