Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,67 +4,119 @@ 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 |
|
| 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 |
def handle_booking(name, tel, email, date_str, time, pax, remarks):
|
| 48 |
if not name or not tel or not date_str or not time:
|
| 49 |
-
return "⚠️ 請完整填寫必填欄位"
|
| 50 |
|
| 51 |
data = {
|
| 52 |
-
"name": name,
|
| 53 |
-
"
|
| 54 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
# 寫入資料庫
|
| 59 |
supabase.table("bookings").insert(data).execute()
|
| 60 |
-
|
|
|
|
| 61 |
send_line_notify(data)
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
|
|
|
| 65 |
return f"❌ 系統錯誤: {str(e)}"
|
| 66 |
|
| 67 |
-
# ---
|
| 68 |
def check_confirmation(request: gr.Request):
|
| 69 |
"""
|
| 70 |
當網址帶有 ?id=xx&action=confirm 時觸發
|
|
@@ -78,50 +130,122 @@ def check_confirmation(request: gr.Request):
|
|
| 78 |
try:
|
| 79 |
# 更新資料庫狀態
|
| 80 |
supabase.table("bookings").update({"status": "顧客已確認"}).eq("id", bid).execute()
|
|
|
|
|
|
|
| 81 |
return f"""
|
| 82 |
<script>
|
| 83 |
document.addEventListener('DOMContentLoaded', function() {{
|
| 84 |
alert('✅ 感謝您!訂位已確認 (編號 {bid})');
|
| 85 |
}});
|
| 86 |
</script>
|
| 87 |
-
<div style='padding:20px; background:#d4af37; color:black; text-align:center; margin-bottom:20px; border-radius:8px;'>
|
| 88 |
-
🎉 感謝您的確認!我們期待您的光臨。
|
| 89 |
</div>
|
| 90 |
"""
|
| 91 |
-
except:
|
|
|
|
| 92 |
return ""
|
| 93 |
return ""
|
| 94 |
|
| 95 |
-
# ---
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
confirm_msg_box = gr.HTML()
|
| 101 |
|
| 102 |
-
#
|
| 103 |
demo.load(check_confirmation, inputs=None, outputs=confirm_msg_box)
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
gr.Markdown("### 👤 聯絡人資料 Contact")
|
| 108 |
with gr.Group():
|
| 109 |
with gr.Row():
|
| 110 |
-
cust_name = gr.Textbox(label="訂位姓名 *", placeholder="ex. 王小明")
|
| 111 |
-
cust_tel = gr.Textbox(label="手機號碼 *", placeholder="ex. 0912-xxx-xxx")
|
|
|
|
|
|
|
| 112 |
with gr.Row():
|
| 113 |
-
cust_email = gr.Textbox(label="電子信箱 (接收確認信用)", placeholder="example@gmail.com")
|
| 114 |
with gr.Row():
|
| 115 |
-
cust_remarks = gr.Textbox(label="備註 (過敏/慶生/特殊需求)", lines=2)
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
output_msg = gr.HTML()
|
| 119 |
|
| 120 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
submit_btn.click(
|
| 122 |
handle_booking,
|
| 123 |
inputs=[cust_name, cust_tel, cust_email, booking_date, time_slot, pax_count, cust_remarks],
|
| 124 |
outputs=output_msg
|
| 125 |
)
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
from supabase import create_client, Client
|
| 6 |
|
| 7 |
+
# --- 1. 連線設定 ---
|
| 8 |
+
# 請確認 HF Secrets 裡都有設定這些變數
|
| 9 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
| 10 |
LINE_ADMIN_ID = os.getenv("LINE_ADMIN_ID")
|
| 11 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 12 |
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 13 |
|
| 14 |
+
# 建立 Supabase 連線
|
| 15 |
+
if SUPABASE_URL and SUPABASE_KEY:
|
| 16 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 17 |
+
else:
|
| 18 |
+
print("⚠️ 警告:SUPABASE 環境變數未設定")
|
| 19 |
|
| 20 |
+
# --- 2. 輔助函式 (日期與時間) ---
|
| 21 |
+
def get_date_options():
|
| 22 |
+
options = []
|
| 23 |
+
today = datetime.now()
|
| 24 |
+
weekdays = ["(一)", "(二)", "(三)", "(四)", "(五)", "(六)", "(日)"]
|
| 25 |
+
for i in range(30):
|
| 26 |
+
current_date = today + timedelta(days=i)
|
| 27 |
+
date_str = f"{current_date.strftime('%Y-%m-%d')} {weekdays[current_date.weekday()]}"
|
| 28 |
+
options.append(date_str)
|
| 29 |
+
return options
|
| 30 |
|
| 31 |
+
def update_time_slots(date_str):
|
| 32 |
+
if not date_str:
|
| 33 |
+
return gr.update(choices=[]), "請先選擇日期"
|
| 34 |
+
try:
|
| 35 |
+
clean_date_str = date_str.split(" ")[0]
|
| 36 |
+
date_obj = datetime.strptime(clean_date_str, "%Y-%m-%d")
|
| 37 |
+
weekday = date_obj.weekday()
|
| 38 |
+
except:
|
| 39 |
+
return gr.update(choices=[]), "日期格式錯誤"
|
| 40 |
|
| 41 |
+
slots = ["18:00", "18:30", "19:00", "19:30", "20:00", "20:30",
|
| 42 |
+
"21:00", "21:30", "22:00", "22:30", "23:00", "23:30",
|
| 43 |
+
"00:00", "00:30", "01:00"]
|
| 44 |
|
| 45 |
+
if weekday == 4 or weekday == 5:
|
| 46 |
+
slots.extend(["01:30", "02:00", "02:30"])
|
| 47 |
+
status_msg = f"✨ 已選擇 {date_str} (週末營業至 03:00)"
|
| 48 |
+
else:
|
| 49 |
+
slots.extend(["01:30"])
|
| 50 |
+
status_msg = f"🌙 已選擇 {date_str} (平日營業至 02:00)"
|
| 51 |
+
|
| 52 |
+
return gr.update(choices=slots, value=slots[0] if slots else None), status_msg
|
| 53 |
+
|
| 54 |
+
# --- 3. LINE 通知函式 ---
|
| 55 |
+
def send_line_notify(data):
|
| 56 |
+
if not LINE_ACCESS_TOKEN or not LINE_ADMIN_ID:
|
| 57 |
+
return
|
| 58 |
|
| 59 |
+
# 簡單的文字通知 (您也可以換回之前的 Flex Message)
|
| 60 |
+
message = (
|
| 61 |
+
f"🔥 新訂位通知 🔥\n"
|
| 62 |
+
f"姓名:{data['name']}\n"
|
| 63 |
+
f"電話:{data['tel']}\n"
|
| 64 |
+
f"日期:{data['date']} {data['time']}\n"
|
| 65 |
+
f"人數:{data['pax']} 位\n"
|
| 66 |
+
f"Email:{data.get('email', '-')}\n"
|
| 67 |
+
f"備註:{data.get('remarks', '無')}"
|
| 68 |
)
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
requests.post(
|
| 72 |
+
"https://api.line.me/v2/bot/message/push",
|
| 73 |
+
headers={
|
| 74 |
+
"Authorization": f"Bearer {LINE_ACCESS_TOKEN}",
|
| 75 |
+
"Content-Type": "application/json"
|
| 76 |
+
},
|
| 77 |
+
json={
|
| 78 |
+
"to": LINE_ADMIN_ID,
|
| 79 |
+
"messages": [{"type": "text", "text": message}]
|
| 80 |
+
}
|
| 81 |
+
)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"LINE 發送失敗: {e}")
|
| 84 |
|
| 85 |
+
# --- 4. 核心邏輯:處理訂位 ---
|
| 86 |
def handle_booking(name, tel, email, date_str, time, pax, remarks):
|
| 87 |
if not name or not tel or not date_str or not time:
|
| 88 |
+
return "⚠️ 請完整填寫必填欄位 (姓名、電話、日期、時間)"
|
| 89 |
|
| 90 |
data = {
|
| 91 |
+
"name": name,
|
| 92 |
+
"tel": tel,
|
| 93 |
+
"email": email,
|
| 94 |
+
"date": date_str,
|
| 95 |
+
"time": time,
|
| 96 |
+
"pax": pax,
|
| 97 |
+
"remarks": remarks,
|
| 98 |
+
"status": "待處理"
|
| 99 |
}
|
| 100 |
|
| 101 |
try:
|
| 102 |
+
# 1. 寫入資料庫
|
| 103 |
supabase.table("bookings").insert(data).execute()
|
| 104 |
+
|
| 105 |
+
# 2. 發送 LINE
|
| 106 |
send_line_notify(data)
|
| 107 |
+
|
| 108 |
+
return """
|
| 109 |
+
<div style='text-align: center; color: #fff; padding: 20px; border: 1px solid #d4af37; border-radius: 8px; background: #222;'>
|
| 110 |
+
<h2 style='color: #d4af37; margin: 0;'>Request Received</h2>
|
| 111 |
+
<p style='margin: 10px 0;'>🥂 預約申請已提交</p>
|
| 112 |
+
<p style='font-size: 0.9em; color: #aaa;'>請留意您的 Email 確認信。</p>
|
| 113 |
+
</div>
|
| 114 |
+
"""
|
| 115 |
except Exception as e:
|
| 116 |
+
print(f"Error: {e}")
|
| 117 |
return f"❌ 系統錯誤: {str(e)}"
|
| 118 |
|
| 119 |
+
# --- 5. 核心邏輯:處理確認連結 (Webhook) ---
|
| 120 |
def check_confirmation(request: gr.Request):
|
| 121 |
"""
|
| 122 |
當網址帶有 ?id=xx&action=confirm 時觸發
|
|
|
|
| 130 |
try:
|
| 131 |
# 更新資料庫狀態
|
| 132 |
supabase.table("bookings").update({"status": "顧客已確認"}).eq("id", bid).execute()
|
| 133 |
+
|
| 134 |
+
# 回傳給前端顯示的確認訊息
|
| 135 |
return f"""
|
| 136 |
<script>
|
| 137 |
document.addEventListener('DOMContentLoaded', function() {{
|
| 138 |
alert('✅ 感謝您!訂位已確認 (編號 {bid})');
|
| 139 |
}});
|
| 140 |
</script>
|
| 141 |
+
<div style='padding:20px; background:#d4af37; color:black; text-align:center; margin-bottom:20px; border-radius:8px; font-weight:bold;'>
|
| 142 |
+
🎉 感謝您的確認!我們期待您的光臨。 (訂單編號: {bid})
|
| 143 |
</div>
|
| 144 |
"""
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print(f"Confirm Error: {e}")
|
| 147 |
return ""
|
| 148 |
return ""
|
| 149 |
|
| 150 |
+
# --- 6. 介面設定 (Theme & CSS) ---
|
| 151 |
+
theme = gr.themes.Soft(
|
| 152 |
+
primary_hue="amber",
|
| 153 |
+
neutral_hue="zinc",
|
| 154 |
+
font=[gr.themes.GoogleFont("Playfair Display"), "ui-sans-serif", "sans-serif"],
|
| 155 |
+
).set(
|
| 156 |
+
body_background_fill="#0F0F0F",
|
| 157 |
+
block_background_fill="#1a1a1a",
|
| 158 |
+
block_border_width="1px",
|
| 159 |
+
block_border_color="#333",
|
| 160 |
+
input_background_fill="#262626",
|
| 161 |
+
input_border_color="#444",
|
| 162 |
+
body_text_color="#E0E0E0",
|
| 163 |
+
block_title_text_color="#d4af37",
|
| 164 |
+
button_primary_background_fill="#d4af37",
|
| 165 |
+
button_primary_text_color="#000000",
|
| 166 |
+
)
|
| 167 |
|
| 168 |
+
custom_css = """
|
| 169 |
+
footer {display: none !important;}
|
| 170 |
+
.gradio-container, .block, .row, .column { overflow: visible !important; }
|
| 171 |
+
.options, .wrap .options {
|
| 172 |
+
background-color: #262626 !important;
|
| 173 |
+
border: 1px solid #d4af37 !important;
|
| 174 |
+
z-index: 10000 !important;
|
| 175 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.5);
|
| 176 |
+
}
|
| 177 |
+
.item, .options .item { color: #E0E0E0 !important; padding: 8px 12px !important; }
|
| 178 |
+
.item:hover, .item.selected, .options .item:hover { background-color: #d4af37 !important; color: black !important; }
|
| 179 |
+
input:focus, .dropdown-trigger:focus-within { border-color: #d4af37 !important; box-shadow: 0 0 8px rgba(212, 175, 55, 0.4) !important; }
|
| 180 |
+
h3 { border-bottom: 1px solid #444; padding-bottom: 5px; margin-bottom: 10px; }
|
| 181 |
+
.legal-footer {
|
| 182 |
+
text-align: center; margin-top: 15px; padding-top: 15px; border-top: 1px solid #333; color: #666; font-size: 0.75rem; line-height: 1.5; font-family: sans-serif;
|
| 183 |
+
}
|
| 184 |
+
.legal-footer strong { color: #888; }
|
| 185 |
+
"""
|
| 186 |
+
|
| 187 |
+
# --- 7. 介面佈局 (完整版) ---
|
| 188 |
+
with gr.Blocks(theme=theme, css=custom_css, title="Booking") as demo:
|
| 189 |
+
|
| 190 |
+
# [隱藏] 用來接收 URL 確認參數的區塊
|
| 191 |
confirm_msg_box = gr.HTML()
|
| 192 |
|
| 193 |
+
# 頁面載入時,執行 check_confirmation
|
| 194 |
demo.load(check_confirmation, inputs=None, outputs=confirm_msg_box)
|
| 195 |
|
| 196 |
+
with gr.Row():
|
| 197 |
+
with gr.Column():
|
| 198 |
+
gr.Markdown("### 📅 預約資訊 Booking Info")
|
| 199 |
+
|
| 200 |
+
# --- 之前漏掉的 Date & Pax ---
|
| 201 |
+
date_options = get_date_options()
|
| 202 |
+
booking_date = gr.Dropdown(choices=date_options, label="選擇日期 Select Date", interactive=True)
|
| 203 |
+
pax_count = gr.Slider(minimum=1, maximum=10, value=2, step=1, label="用餐人數 Guest Count")
|
| 204 |
+
|
| 205 |
+
with gr.Column():
|
| 206 |
+
gr.Markdown("### 🕰️ 選擇時段 Time Slot")
|
| 207 |
+
# --- 之前漏掉的 Status & Time ---
|
| 208 |
+
status_box = gr.Markdown("請先選擇日期...", visible=True)
|
| 209 |
+
time_slot = gr.Dropdown(choices=[], label="可用時段 Available Time", interactive=True)
|
| 210 |
+
|
| 211 |
+
gr.HTML("<div style='height: 10px'></div>")
|
| 212 |
|
| 213 |
gr.Markdown("### 👤 聯絡人資料 Contact")
|
| 214 |
with gr.Group():
|
| 215 |
with gr.Row():
|
| 216 |
+
cust_name = gr.Textbox(label="訂位姓名 Name *", placeholder="ex. 王小明")
|
| 217 |
+
cust_tel = gr.Textbox(label="手機號碼 Phone *", placeholder="ex. 0912-xxx-xxx")
|
| 218 |
+
|
| 219 |
+
# --- 新增的 Email 與 備註 ---
|
| 220 |
with gr.Row():
|
| 221 |
+
cust_email = gr.Textbox(label="電子信箱 Email (接收確認信用)", placeholder="example@gmail.com")
|
| 222 |
with gr.Row():
|
| 223 |
+
cust_remarks = gr.Textbox(label="備註 Remarks (過敏/慶生/特殊需求)", lines=2)
|
| 224 |
|
| 225 |
+
gr.HTML("<div style='height: 15px'></div>")
|
| 226 |
+
|
| 227 |
+
submit_btn = gr.Button("確認預約 Request Booking", size="lg", variant="primary")
|
| 228 |
output_msg = gr.HTML()
|
| 229 |
|
| 230 |
+
# 頁尾警語
|
| 231 |
+
gr.HTML("""
|
| 232 |
+
<div class="legal-footer">
|
| 233 |
+
<p style="margin-bottom: 5px;">© 2026 CIE CIE TAIPEI. All Rights Reserved.</p>
|
| 234 |
+
<p>內容涉及酒類產品訊息,請勿轉發分享給未達法定購買年齡者;未滿十八歲請勿飲酒。<br>
|
| 235 |
+
<strong>喝酒不開車,開車不喝酒。</strong></p>
|
| 236 |
+
</div>
|
| 237 |
+
""")
|
| 238 |
+
|
| 239 |
+
# --- 互動事件綁定 ---
|
| 240 |
+
# 1. 日期改變 -> 更新時段
|
| 241 |
+
booking_date.change(update_time_slots, inputs=booking_date, outputs=[time_slot, status_box])
|
| 242 |
+
|
| 243 |
+
# 2. 送出按鈕 -> 處理訂位
|
| 244 |
submit_btn.click(
|
| 245 |
handle_booking,
|
| 246 |
inputs=[cust_name, cust_tel, cust_email, booking_date, time_slot, pax_count, cust_remarks],
|
| 247 |
outputs=output_msg
|
| 248 |
)
|
| 249 |
|
| 250 |
+
if __name__ == "__main__":
|
| 251 |
+
demo.launch()
|