twilio_streamlit_robertclass / streamlit_app.py
jerrychen428's picture
Create streamlit_app.py
04eb0f6 verified
import streamlit as st
from twilio.rest import Client
import os
def send_sms(account_sid, auth_token, from_number, to_number, message_body):
"""發送SMS訊息的函數"""
try:
client = Client(account_sid, auth_token)
message = client.messages.create(
from_=from_number,
to=to_number,
body=message_body
)
return True, message.sid
except Exception as e:
return False, str(e)
def main():
st.title("📱 SMS 發送器")
st.write("使用 Twilio 發送 SMS 訊息")
# 側邊欄配置
st.sidebar.header("Twilio 配置")
# Twilio 憑證輸入(建議使用環境變量或secrets)
account_sid = st.sidebar.text_input(
"Account SID",
value="ACd7f840e0ea441d49885b4ab7a8828c46",
type="password"
)
auth_token = st.sidebar.text_input(
"Auth Token",
value="b3f7af530024eb7bf68e3576f5fe3ad7",
type="password"
)
from_number = st.sidebar.text_input(
"發送號碼 (Twilio號碼)",
value="+17623355728"
)
# 主要內容區域
st.header("發送訊息")
# 接收號碼輸入
to_number = st.text_input(
"接收號碼",
value="+886919017732",
placeholder="例如: +886912345678"
)
# 訊息內容輸入
message_body = st.text_area(
"訊息內容",
placeholder="請輸入您要發送的訊息...",
height=100
)
# 發送按鈕
col1, col2, col3 = st.columns([1, 1, 1])
with col2:
if st.button("📤 發送訊息", type="primary"):
# 驗證輸入
if not all([account_sid, auth_token, from_number, to_number, message_body]):
st.error("請填寫所有必要欄位!")
elif not to_number.startswith('+'):
st.error("接收號碼必須包含國家代碼,例如: +886912345678")
else:
# 發送訊息
with st.spinner("正在發送訊息..."):
success, result = send_sms(
account_sid,
auth_token,
from_number,
to_number,
message_body
)
if success:
st.success(f"✅ 訊息發送成功!\n訊息ID: {result}")
# 清空訊息內容
st.rerun()
else:
st.error(f"❌ 發送失敗: {result}")
# 使用說明
st.markdown("---")
st.subheader("📋 使用說明")
st.markdown("""
1. **Twilio 配置**: 在側邊欄輸入您的 Twilio Account SID 和 Auth Token
2. **發送號碼**: 使用您的 Twilio 虛擬號碼
3. **接收號碼**: 輸入要發送到的手機號碼(需包含國家代碼,如 +886)
4. **訊息內容**: 輸入要發送的訊息內容
5. 點擊「發送訊息」按鈕即可發送
⚠️ **安全提醒**:
- 請不要在生產環境中硬編碼敏感資訊
- 建議使用 Streamlit secrets 或環境變量來存儲 API 憑證
""")
# 可選:顯示發送歷史(需要額外實現)
with st.expander("💡 進階功能建議"):
st.markdown("""
- 新增發送歷史記錄
- 支援批量發送
- 訊息模板功能
- 發送狀態追蹤
- 字數統計和費用估算
""")
if __name__ == "__main__":
main()