Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,165 +1,118 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
block_title_text_color="#d4af37",
|
| 18 |
-
button_primary_background_fill="#d4af37",
|
| 19 |
-
button_primary_text_color="#000000",
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
# --- 2. CSS ---
|
| 23 |
-
custom_css = """
|
| 24 |
-
/* 隱藏 footer */
|
| 25 |
-
footer {display: none !important;}
|
| 26 |
-
|
| 27 |
-
/* 強制讓所有容器允許內容溢出 */
|
| 28 |
-
.gradio-container, .block, .row, .column {
|
| 29 |
-
overflow: visible !important;
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
/* 下拉選單樣式修正 */
|
| 33 |
-
.options, .wrap .options {
|
| 34 |
-
background-color: #262626 !important;
|
| 35 |
-
border: 1px solid #d4af37 !important;
|
| 36 |
-
z-index: 10000 !important;
|
| 37 |
-
box-shadow: 0 5px 15px rgba(0,0,0,0.5);
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
.item, .options .item {
|
| 41 |
-
color: #E0E0E0 !important;
|
| 42 |
-
padding: 8px 12px !important;
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
.item:hover, .item.selected, .options .item:hover {
|
| 46 |
-
background-color: #d4af37 !important;
|
| 47 |
-
color: black !important;
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
input:focus, .dropdown-trigger:focus-within {
|
| 51 |
-
border-color: #d4af37 !important;
|
| 52 |
-
box-shadow: 0 0 8px rgba(212, 175, 55, 0.4) !important;
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
h3 {
|
| 56 |
-
border-bottom: 1px solid #444;
|
| 57 |
-
padding-bottom: 5px;
|
| 58 |
-
margin-bottom: 10px;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
/* 🌟 新增:針對警語頁尾的優化樣式 */
|
| 62 |
-
.legal-footer {
|
| 63 |
-
text-align: center;
|
| 64 |
-
margin-top: 15px; /* 縮小上方間距 */
|
| 65 |
-
padding-top: 15px;
|
| 66 |
-
border-top: 1px solid #333;
|
| 67 |
-
color: #666;
|
| 68 |
-
font-size: 0.75rem; /* 字體稍微縮小更像註解 */
|
| 69 |
-
line-height: 1.5;
|
| 70 |
-
font-family: sans-serif;
|
| 71 |
-
}
|
| 72 |
-
.legal-footer strong {
|
| 73 |
-
color: #888;
|
| 74 |
-
}
|
| 75 |
-
"""
|
| 76 |
-
|
| 77 |
-
# --- 核心邏輯 (保持不變) ---
|
| 78 |
-
def get_date_options():
|
| 79 |
-
options = []
|
| 80 |
-
today = datetime.now()
|
| 81 |
-
weekdays = ["(一)", "(二)", "(三)", "(四)", "(五)", "(六)", "(日)"]
|
| 82 |
-
for i in range(30):
|
| 83 |
-
current_date = today + timedelta(days=i)
|
| 84 |
-
date_str = f"{current_date.strftime('%Y-%m-%d')} {weekdays[current_date.weekday()]}"
|
| 85 |
-
options.append(date_str)
|
| 86 |
-
return options
|
| 87 |
-
|
| 88 |
-
def update_time_slots(date_str):
|
| 89 |
-
if not date_str:
|
| 90 |
-
return gr.update(choices=[]), "請先選擇日期"
|
| 91 |
-
try:
|
| 92 |
-
clean_date_str = date_str.split(" ")[0]
|
| 93 |
-
date_obj = datetime.strptime(clean_date_str, "%Y-%m-%d")
|
| 94 |
-
weekday = date_obj.weekday()
|
| 95 |
-
except:
|
| 96 |
-
return gr.update(choices=[]), "日期格式錯誤"
|
| 97 |
-
|
| 98 |
-
slots = ["18:00", "18:30", "19:00", "19:30", "20:00", "20:30",
|
| 99 |
-
"21:00", "21:30", "22:00", "22:30", "23:00", "23:30",
|
| 100 |
-
"00:00", "00:30", "01:00"]
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
if not name or not tel or not date_str or not time:
|
| 113 |
-
return "⚠️ 請完整填寫
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# --- 介面佈局 ---
|
| 124 |
-
|
| 125 |
-
with gr.Row():
|
| 126 |
-
with gr.Column():
|
| 127 |
-
gr.Markdown("### 📅 預約資訊 Booking Info")
|
| 128 |
-
date_options = get_date_options()
|
| 129 |
-
booking_date = gr.Dropdown(choices=date_options, label="選擇日期 Select Date", interactive=True)
|
| 130 |
-
pax_count = gr.Slider(minimum=1, maximum=10, value=2, step=1, label="用餐人數 Guest Count")
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
|
| 139 |
gr.Markdown("### 👤 聯絡人資料 Contact")
|
| 140 |
with gr.Group():
|
| 141 |
with gr.Row():
|
| 142 |
-
cust_name = gr.Textbox(label="訂位姓名
|
| 143 |
-
cust_tel = gr.Textbox(label="手機號碼
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
gr.
|
| 146 |
-
|
| 147 |
-
submit_btn = gr.Button("確認預約 Request Booking", size="lg", variant="primary")
|
| 148 |
-
|
| 149 |
-
# 🌟 修改點 1:移除 label,這樣平常就不會出現一個空框框
|
| 150 |
output_msg = gr.HTML()
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
booking_date.change(update_time_slots, inputs=booking_date, outputs=[time_slot, status_box])
|
| 162 |
-
submit_btn.click(handle_booking, inputs=[cust_name, cust_tel, booking_date, time_slot, pax_count], outputs=output_msg)
|
| 163 |
-
|
| 164 |
-
if __name__ == "__main__":
|
| 165 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from supabase import create_client, Client
|
| 6 |
+
|
| 7 |
+
# --- 連線設定 ---
|
| 8 |
+
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
| 9 |
+
LINE_ADMIN_ID = os.getenv("LINE_ADMIN_ID")
|
| 10 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 11 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 12 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 13 |
+
|
| 14 |
+
# --- LINE 通知函式 (更新版:包含備註) ---
|
| 15 |
+
def send_line_notify(data):
|
| 16 |
+
if not LINE_ACCESS_TOKEN or not LINE_ADMIN_ID: return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
msg_body = [
|
| 19 |
+
{"type": "text", "text": "姓名", "color": "#aaaaaa", "size": "sm", "flex": 2},
|
| 20 |
+
{"type": "text", "text": data['name'], "wrap": True, "color": "#666666", "size": "sm", "flex": 5},
|
| 21 |
+
{"type": "text", "text": "電話", "color": "#aaaaaa", "size": "sm", "flex": 2},
|
| 22 |
+
{"type": "text", "text": data['tel'], "wrap": True, "color": "#666666", "size": "sm", "flex": 5},
|
| 23 |
+
{"type": "text", "text": "Email", "color": "#aaaaaa", "size": "sm", "flex": 2},
|
| 24 |
+
{"type": "text", "text": data.get('email', '-'), "wrap": True, "color": "#666666", "size": "sm", "flex": 5},
|
| 25 |
+
{"type": "text", "text": "備註", "color": "#aaaaaa", "size": "sm", "flex": 2},
|
| 26 |
+
{"type": "text", "text": data.get('remarks', '無'), "wrap": True, "color": "#666666", "size": "sm", "flex": 5}
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# ... (此處省略部分重複的 Flex Message 結構,重點是把上面的 msg_body 塞進去) ...
|
| 30 |
+
# 為了節省篇幅,這裡直接發送簡單通知,您可以用之前的 Flex Message 結構替換
|
| 31 |
+
requests.post(
|
| 32 |
+
"https://api.line.me/v2/bot/message/push",
|
| 33 |
+
headers={"Authorization": f"Bearer {LINE_ACCESS_TOKEN}", "Content-Type": "application/json"},
|
| 34 |
+
json={"to": LINE_ADMIN_ID, "messages": [{"type": "text", "text": f"🔥 新訂位:{data['name']} ({data['pax']}人)\n時間:{data['date']} {data['time']}\n備註:{data.get('remarks', '無')}"}]}
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# --- 核心邏輯 ---
|
| 38 |
+
def handle_booking(name, tel, email, date_str, time, pax, remarks):
|
| 39 |
if not name or not tel or not date_str or not time:
|
| 40 |
+
return "⚠️ 請完整填寫必填欄位"
|
| 41 |
|
| 42 |
+
data = {
|
| 43 |
+
"name": name, "tel": tel, "email": email,
|
| 44 |
+
"date": date_str, "time": time, "pax": pax,
|
| 45 |
+
"remarks": remarks, "status": "待處理"
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
# 寫入資料庫
|
| 50 |
+
supabase.table("bookings").insert(data).execute()
|
| 51 |
+
# 發送 LINE
|
| 52 |
+
send_line_notify(data)
|
| 53 |
+
return """<div style='text-align: center; color: #fff; padding: 20px; border: 1px solid #d4af37; border-radius: 8px; background: #222;'>
|
| 54 |
+
<h2 style='color: #d4af37;'>Request Received</h2><p>🥂 預約申請已提交,請留意確認信。</p></div>"""
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"❌ 系統錯誤: {str(e)}"
|
| 57 |
+
|
| 58 |
+
# --- 處理確認連結 (Webhook) ---
|
| 59 |
+
def check_confirmation(request: gr.Request):
|
| 60 |
"""
|
| 61 |
+
當網址帶有 ?id=xx&action=confirm 時觸發
|
| 62 |
+
"""
|
| 63 |
+
if not request: return ""
|
| 64 |
+
params = request.query_params
|
| 65 |
+
action = params.get('action')
|
| 66 |
+
bid = params.get('id')
|
| 67 |
+
|
| 68 |
+
if action == 'confirm' and bid:
|
| 69 |
+
try:
|
| 70 |
+
# 更新資料庫狀態
|
| 71 |
+
supabase.table("bookings").update({"status": "顧客已確認"}).eq("id", bid).execute()
|
| 72 |
+
return f"""
|
| 73 |
+
<script>
|
| 74 |
+
document.addEventListener('DOMContentLoaded', function() {{
|
| 75 |
+
alert('✅ 感謝您!訂位已確認 (編號 {bid})');
|
| 76 |
+
}});
|
| 77 |
+
</script>
|
| 78 |
+
<div style='padding:20px; background:#d4af37; color:black; text-align:center; margin-bottom:20px; border-radius:8px;'>
|
| 79 |
+
🎉 感謝您的確認!我們期待您的光臨。
|
| 80 |
+
</div>
|
| 81 |
+
"""
|
| 82 |
+
except:
|
| 83 |
+
return ""
|
| 84 |
+
return ""
|
| 85 |
|
| 86 |
# --- 介面佈局 ---
|
| 87 |
+
# 主題與 CSS 設定 (沿用之前的) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Booking") as demo:
|
| 90 |
+
# 隱藏的 HTML 區塊,用來接收確認訊息
|
| 91 |
+
confirm_msg_box = gr.HTML()
|
| 92 |
+
|
| 93 |
+
# 頁面載入時檢查網址參數
|
| 94 |
+
demo.load(check_confirmation, inputs=None, outputs=confirm_msg_box)
|
| 95 |
|
| 96 |
+
# ... (Header 與 日期選擇區塊 保持不變) ...
|
| 97 |
|
| 98 |
gr.Markdown("### 👤 聯絡人資料 Contact")
|
| 99 |
with gr.Group():
|
| 100 |
with gr.Row():
|
| 101 |
+
cust_name = gr.Textbox(label="訂位姓名 *", placeholder="ex. 王小明")
|
| 102 |
+
cust_tel = gr.Textbox(label="手機號碼 *", placeholder="ex. 0912-xxx-xxx")
|
| 103 |
+
with gr.Row():
|
| 104 |
+
cust_email = gr.Textbox(label="電子信箱 (接收確認信用)", placeholder="example@gmail.com")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
cust_remarks = gr.Textbox(label="備註 (過敏/慶生/特殊需求)", lines=2)
|
| 107 |
|
| 108 |
+
submit_btn = gr.Button("確認預約 Request Booking", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
output_msg = gr.HTML()
|
| 110 |
|
| 111 |
+
# ... (Event Listeners) ...
|
| 112 |
+
submit_btn.click(
|
| 113 |
+
handle_booking,
|
| 114 |
+
inputs=[cust_name, cust_tel, cust_email, booking_date, time_slot, pax_count, cust_remarks],
|
| 115 |
+
outputs=output_msg
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|