| | 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 配置") |
| |
|
| | |
| | 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() |