File size: 2,766 Bytes
a43bc9b
1b813f3
a43bc9b
5048dcb
 
1b813f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43bc9b
5048dcb
1b813f3
 
 
 
 
5048dcb
 
1b813f3
a43bc9b
1b813f3
 
 
 
 
 
 
 
 
 
 
 
 
 
a43bc9b
1b813f3
 
 
5048dcb
1b813f3
b58e86b
1b813f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import requests
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException

st.set_page_config(page_title="WhatsApp Chat", layout="wide")

st.title("πŸ“± Two-Way WhatsApp Chat via Twilio Conversations")

# Twilio credentials
account_sid = st.secrets.get("TWILIO_ACCOUNT_SID") or st.text_input("Twilio Account SID", "")
auth_token = st.secrets.get("TWILIO_AUTH_TOKEN") or st.text_input("Twilio Auth Token", type="password")

# Conversation SID and Author (e.g., 'system' or phone number)
conversation_sid = st.text_input("Conversation SID", "CH0401ce390b374780b3eaded677d882b0")
author = st.text_input("Author Name (e.g., system or whatsapp:+92333xxxxxxx)", "system")

# Message to send
msg_to_send = st.text_input("Type your message here")

def send_message(account_sid, auth_token, conversation_sid, author, body):
    try:
        client = Client(account_sid, auth_token)
        message = client.conversations \
                        .v1 \
                        .conversations(conversation_sid) \
                        .messages \
                        .create(author=author, body=body)
        return message.sid
    except TwilioRestException as e:
        return f"Twilio Error: {str(e)}"
    except Exception as e:
        return f"Error: {str(e)}"

def fetch_messages(account_sid, auth_token, conversation_sid):
    try:
        client = Client(account_sid, auth_token)
        messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=50)
        return messages
    except Exception as e:
        return f"Error fetching messages: {e}"

# Send message button
if st.button("πŸ“€ Send Message"):
    if not all([account_sid, auth_token, conversation_sid, author, msg_to_send]):
        st.error("Please fill in all fields.")
    else:
        sid = send_message(account_sid, auth_token, conversation_sid, author, msg_to_send)
        if sid.startswith("IM"):
            st.success(f"Message sent! SID: {sid}")
        else:
            st.error(sid)

st.markdown("---")
st.subheader("πŸ•“ Chat History")

# Display message history
if st.button("πŸ”„ Refresh Chat"):
    if not all([account_sid, auth_token, conversation_sid]):
        st.warning("Provide Twilio credentials and Conversation SID first.")
    else:
        messages = fetch_messages(account_sid, auth_token, conversation_sid)
        if isinstance(messages, str):
            st.error(messages)
        else:
            for msg in reversed(messages):  # Show newest at bottom
                sender = msg.author
                body = msg.body or "[No content]"
                timestamp = msg.date_created.strftime("%Y-%m-%d %H:%M:%S")
                st.markdown(f"**{sender}** [{timestamp}]: {body}")