Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,70 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from twilio.rest import Client
|
3 |
from twilio.base.exceptions import TwilioRestException
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
Sends a WhatsApp message using Twilio.
|
22 |
-
|
23 |
-
Args:
|
24 |
-
account_sid (str): Your Twilio Account SID.
|
25 |
-
auth_token (str): Your Twilio Auth Token.
|
26 |
-
from_whatsapp (str): The WhatsApp number to send the message from (in the format 'whatsapp:+number').
|
27 |
-
to_whatsapp (str): The WhatsApp number to send the message to (in the format 'whatsapp:+number').
|
28 |
-
content_sid (str): The SID of the Twilio Content template.
|
29 |
-
content_variables (str): A JSON string of the variables to populate the template.
|
30 |
-
|
31 |
-
Returns:
|
32 |
-
str: The SID of the sent message, or an error message.
|
33 |
-
"""
|
34 |
try:
|
35 |
client = Client(account_sid, auth_token)
|
36 |
-
message = client.
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
)
|
42 |
return message.sid
|
43 |
except TwilioRestException as e:
|
44 |
-
return str(e)
|
45 |
except Exception as e:
|
46 |
-
return f"
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
else:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
st.success(f"Message sent successfully! SID: {message_sid}")
|
59 |
else:
|
60 |
-
st.error(
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
from twilio.rest import Client
|
4 |
from twilio.base.exceptions import TwilioRestException
|
5 |
|
6 |
+
st.set_page_config(page_title="WhatsApp Chat", layout="wide")
|
7 |
+
|
8 |
+
st.title("π± Two-Way WhatsApp Chat via Twilio Conversations")
|
9 |
+
|
10 |
+
# Twilio credentials
|
11 |
+
account_sid = st.secrets.get("TWILIO_ACCOUNT_SID") or st.text_input("Twilio Account SID", "")
|
12 |
+
auth_token = st.secrets.get("TWILIO_AUTH_TOKEN") or st.text_input("Twilio Auth Token", type="password")
|
13 |
+
|
14 |
+
# Conversation SID and Author (e.g., 'system' or phone number)
|
15 |
+
conversation_sid = st.text_input("Conversation SID", "CH0401ce390b374780b3eaded677d882b0")
|
16 |
+
author = st.text_input("Author Name (e.g., system or whatsapp:+92333xxxxxxx)", "system")
|
17 |
+
|
18 |
+
# Message to send
|
19 |
+
msg_to_send = st.text_input("Type your message here")
|
20 |
+
|
21 |
+
def send_message(account_sid, auth_token, conversation_sid, author, body):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
try:
|
23 |
client = Client(account_sid, auth_token)
|
24 |
+
message = client.conversations \
|
25 |
+
.v1 \
|
26 |
+
.conversations(conversation_sid) \
|
27 |
+
.messages \
|
28 |
+
.create(author=author, body=body)
|
|
|
29 |
return message.sid
|
30 |
except TwilioRestException as e:
|
31 |
+
return f"Twilio Error: {str(e)}"
|
32 |
except Exception as e:
|
33 |
+
return f"Error: {str(e)}"
|
34 |
+
|
35 |
+
def fetch_messages(account_sid, auth_token, conversation_sid):
|
36 |
+
try:
|
37 |
+
client = Client(account_sid, auth_token)
|
38 |
+
messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=50)
|
39 |
+
return messages
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error fetching messages: {e}"
|
42 |
+
|
43 |
+
# Send message button
|
44 |
+
if st.button("π€ Send Message"):
|
45 |
+
if not all([account_sid, auth_token, conversation_sid, author, msg_to_send]):
|
46 |
+
st.error("Please fill in all fields.")
|
47 |
else:
|
48 |
+
sid = send_message(account_sid, auth_token, conversation_sid, author, msg_to_send)
|
49 |
+
if sid.startswith("IM"):
|
50 |
+
st.success(f"Message sent! SID: {sid}")
|
|
|
51 |
else:
|
52 |
+
st.error(sid)
|
53 |
|
54 |
+
st.markdown("---")
|
55 |
+
st.subheader("π Chat History")
|
56 |
+
|
57 |
+
# Display message history
|
58 |
+
if st.button("π Refresh Chat"):
|
59 |
+
if not all([account_sid, auth_token, conversation_sid]):
|
60 |
+
st.warning("Provide Twilio credentials and Conversation SID first.")
|
61 |
+
else:
|
62 |
+
messages = fetch_messages(account_sid, auth_token, conversation_sid)
|
63 |
+
if isinstance(messages, str):
|
64 |
+
st.error(messages)
|
65 |
+
else:
|
66 |
+
for msg in reversed(messages): # Show newest at bottom
|
67 |
+
sender = msg.author
|
68 |
+
body = msg.body or "[No content]"
|
69 |
+
timestamp = msg.date_created.strftime("%Y-%m-%d %H:%M:%S")
|
70 |
+
st.markdown(f"**{sender}** [{timestamp}]: {body}")
|