DeepLearning101 commited on
Commit
913a9b3
·
verified ·
1 Parent(s): 8354a5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -19
app.py CHANGED
@@ -3,11 +3,16 @@ import os
3
  import pandas as pd
4
  import requests # 👈 改用 requests 發送 HTTP 請求
5
  from supabase import create_client, Client
 
 
 
 
6
 
7
  # --- 設定 ---
8
  SUPABASE_URL = os.getenv("SUPABASE_URL")
9
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
10
  GAS_MAIL_URL = os.getenv("GAS_MAIL_URL") # 👈 讀取 GAS 網址
 
11
  PUBLIC_SPACE_URL = "https://deeplearning101-ciecietaipei.hf.space"
12
 
13
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
@@ -20,13 +25,14 @@ def get_bookings():
20
  response = supabase.table("bookings").select("*").order("created_at", desc=True).execute()
21
  if not response.data: return pd.DataFrame()
22
  df = pd.DataFrame(response.data)
 
23
  cols = ['id', 'date', 'time', 'name', 'tel', 'email', 'pax', 'remarks', 'status']
24
  for c in cols:
25
  if c not in df.columns: df[c] = ""
26
  return df[cols]
27
 
28
  def send_confirmation_email(booking_id):
29
- """透過 Google Apps Script 中繼站發信"""
30
  try:
31
  if not GAS_MAIL_URL:
32
  return "❌ 錯誤:未設定 GAS_MAIL_URL Secret"
@@ -39,31 +45,51 @@ def send_confirmation_email(booking_id):
39
  email = booking.get('email')
40
  if not email or "@" not in email: return "❌ 客人未填寫有效 Email"
41
 
42
- # 2. 產生連結
43
  confirm_link = f"{PUBLIC_SPACE_URL}/?id={booking_id}&action=confirm"
 
44
 
45
- # 3. 準備信件內容 (HTML)
46
  html_content = f"""
47
- <div style="padding: 20px; background: #111; color: #d4af37; font-family: sans-serif; border-radius: 10px;">
48
- <h2 style="border-bottom: 1px solid #d4af37; padding-bottom: 10px;">訂位保留確認</h2>
49
- <p>{booking['name']} 您好,我們已為您保留座位:</p>
50
- <ul style="color: #eee;">
51
- <li>日期:{booking['date']}</li>
52
- <li>時間:{booking['time']}</li>
53
- <li>人數:{booking['pax']} 位</li>
54
- </ul>
55
- <p>請點擊下方按鈕確認出席:</p>
56
- <a href="{confirm_link}" style="background: #d4af37; color: black; padding: 12px 24px; text-decoration: none; border-radius: 5px; font-weight: bold; display: inline-block; margin-top: 10px;">✅ 我會出席 (Confirm)</a>
57
- <br><br>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  <hr style="border: 0; border-top: 1px solid #333;">
59
- <p style="color: #666; font-size: 12px;">若需取消,請直接回覆此信或致電 02-2709-3446。</p>
 
 
 
60
  </div>
61
  """
62
 
63
- # 4. 呼叫 GAS 發信 (這走的是 Port 443,HF 不會擋)
64
  payload = {
65
  "to": email,
66
- "subject": f"[{booking['date']}] Cié Cié Taipei 訂位確認",
67
  "htmlBody": html_content,
68
  "name": "Cié Cié Taipei"
69
  }
@@ -96,8 +122,7 @@ with gr.Blocks(title="Admin") as demo:
96
  action_btn.click(send_confirmation_email, inputs=id_input, outputs=log_output)
97
 
98
  if __name__ == "__main__":
99
- # 這裡做了修改:加入了 auth 參數
100
- # 如果 Secrets 沒設定,預設帳密為 admin / 123456 (強烈建議去設定 Secrets!)
101
  user = ADMIN_USER if ADMIN_USER else "admin"
102
  pwd = ADMIN_PASSWORD if ADMIN_PASSWORD else "123456"
103
 
 
3
  import pandas as pd
4
  import requests # 👈 改用 requests 發送 HTTP 請求
5
  from supabase import create_client, Client
6
+ from datetime import datetime, timedelta, timezone
7
+
8
+ # 設定台北時區 (雖然這個檔案目前沒用到,但保留著好習慣)
9
+ TAIPEI_TZ = timezone(timedelta(hours=8))
10
 
11
  # --- 設定 ---
12
  SUPABASE_URL = os.getenv("SUPABASE_URL")
13
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
14
  GAS_MAIL_URL = os.getenv("GAS_MAIL_URL") # 👈 讀取 GAS 網址
15
+ # ⚠️ 請務必確認這裡填的是您 Space A (前端) 的網址
16
  PUBLIC_SPACE_URL = "https://deeplearning101-ciecietaipei.hf.space"
17
 
18
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
 
25
  response = supabase.table("bookings").select("*").order("created_at", desc=True).execute()
26
  if not response.data: return pd.DataFrame()
27
  df = pd.DataFrame(response.data)
28
+ # 確保欄位順序與存在
29
  cols = ['id', 'date', 'time', 'name', 'tel', 'email', 'pax', 'remarks', 'status']
30
  for c in cols:
31
  if c not in df.columns: df[c] = ""
32
  return df[cols]
33
 
34
  def send_confirmation_email(booking_id):
35
+ """透過 Google Apps Script 中繼站發信 (包含 確認 與 取消 雙按鈕)"""
36
  try:
37
  if not GAS_MAIL_URL:
38
  return "❌ 錯誤:未設定 GAS_MAIL_URL Secret"
 
45
  email = booking.get('email')
46
  if not email or "@" not in email: return "❌ 客人未填寫有效 Email"
47
 
48
+ # 2. 產生連結 (⚠️ 修正點:補回取消連結)
49
  confirm_link = f"{PUBLIC_SPACE_URL}/?id={booking_id}&action=confirm"
50
+ cancel_link = f"{PUBLIC_SPACE_URL}/?id={booking_id}&action=cancel"
51
 
52
+ # 3. 準備信件內容 (HTML 包含雙按鈕)
53
  html_content = f"""
54
+ <div style="padding: 20px; background: #111; color: #d4af37; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; border-radius: 10px; max-width: 600px; margin: 0 auto;">
55
+ <h2 style="border-bottom: 1px solid #d4af37; padding-bottom: 15px; text-align: center; letter-spacing: 2px;">Cié Cié Taipei</h2>
56
+
57
+ <p style="font-size: 16px; margin-top: 20px;">{booking['name']} 您好,</p>
58
+ <p style="font-size: 16px; color: #ccc;">感謝您的預約,我們已為您保留座位:</p>
59
+
60
+ <div style="background: #222; padding: 15px; border-radius: 8px; margin: 20px 0; border-left: 4px solid #d4af37;">
61
+ <ul style="color: #eee; list-style: none; padding: 0; margin: 0; line-height: 1.8;">
62
+ <li>📅 <b>日期:</b> {booking['date']}</li>
63
+ <li>⏰ <b>時間:</b> {booking['time']}</li>
64
+ <li>👥 <b>人數:</b> {booking['pax']} 位</li>
65
+ <li>📝 <b>備註:</b> {booking.get('remarks') or '無'}</li>
66
+ </ul>
67
+ </div>
68
+
69
+ <p style="text-align: center; margin-top: 30px; font-weight: bold; color: #fff;">請選擇您的回覆:</p>
70
+
71
+ <div style="text-align: center; margin-top: 15px; margin-bottom: 30px;">
72
+ <a href="{confirm_link}" style="display: inline-block; background: #d4af37; color: #000; padding: 12px 30px; text-decoration: none; border-radius: 5px; font-weight: bold; margin: 0 10px;">
73
+ ✅ 確認出席
74
+ </a>
75
+
76
+ <a href="{cancel_link}" style="display: inline-block; background: transparent; color: #ff5252; padding: 11px 29px; text-decoration: none; border-radius: 5px; font-weight: bold; border: 1px solid #ff5252; margin: 0 10px;">
77
+ 🚫 取消訂位
78
+ </a>
79
+ </div>
80
+
81
  <hr style="border: 0; border-top: 1px solid #333;">
82
+ <p style="color: #666; font-size: 12px; text-align: center; margin-top: 20px;">
83
+ 此郵件為系統自動發送。<br>
84
+ 如需人工服務,請致電 02-2709-3446
85
+ </p>
86
  </div>
87
  """
88
 
89
+ # 4. 呼叫 GAS 發信
90
  payload = {
91
  "to": email,
92
+ "subject": f"[{booking['date']}] Cié Cié Taipei 訂位確認信",
93
  "htmlBody": html_content,
94
  "name": "Cié Cié Taipei"
95
  }
 
122
  action_btn.click(send_confirmation_email, inputs=id_input, outputs=log_output)
123
 
124
  if __name__ == "__main__":
125
+ # 啟動並加入帳密驗證
 
126
  user = ADMIN_USER if ADMIN_USER else "admin"
127
  pwd = ADMIN_PASSWORD if ADMIN_PASSWORD else "123456"
128