DeepLearning101 commited on
Commit
86289b0
·
verified ·
1 Parent(s): bf30f7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -1,17 +1,14 @@
1
  import gradio as gr
2
  import os
3
  import pandas as pd
4
- import smtplib
5
- from email.mime.text import MIMEText
6
- from email.mime.multipart import MIMEMultipart
7
  from supabase import create_client, Client
8
 
9
  # --- 設定 ---
10
  SUPABASE_URL = os.getenv("SUPABASE_URL")
11
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
12
- MAIL_USERNAME = os.getenv("MAIL_USERNAME") # Gmail 帳號
13
- MAIL_PASSWORD = os.getenv("MAIL_PASSWORD") # Gmail 應用程式密碼
14
- PUBLIC_SPACE_URL = "https://deeplearning101-ciecietaipei.hf.space"
15
 
16
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
17
 
@@ -25,8 +22,11 @@ def get_bookings():
25
  return df[cols]
26
 
27
  def send_confirmation_email(booking_id):
28
- """改用 Gmail SMTP 發送確認信"""
29
  try:
 
 
 
30
  # 1. 抓取資料
31
  res = supabase.table("bookings").select("*").eq("id", booking_id).execute()
32
  if not res.data: return "❌ 找不到該訂單"
@@ -39,11 +39,6 @@ def send_confirmation_email(booking_id):
39
  confirm_link = f"{PUBLIC_SPACE_URL}/?id={booking_id}&action=confirm"
40
 
41
  # 3. 準備信件內容 (HTML)
42
- msg = MIMEMultipart()
43
- msg['From'] = f"Cié Cié Taipei <{MAIL_USERNAME}>"
44
- msg['To'] = email
45
- msg['Subject'] = f"[{booking['date']}] Cié Cié Taipei 訂位確認"
46
-
47
  html_content = f"""
48
  <div style="padding: 20px; background: #111; color: #d4af37; font-family: sans-serif; border-radius: 10px;">
49
  <h2 style="border-bottom: 1px solid #d4af37; padding-bottom: 10px;">訂位保留確認</h2>
@@ -60,17 +55,23 @@ def send_confirmation_email(booking_id):
60
  <p style="color: #666; font-size: 12px;">若需取消,請直接回覆此信或致電 02-2709-3446。</p>
61
  </div>
62
  """
63
- msg.attach(MIMEText(html_content, 'html'))
64
-
65
- # 4. 透過 Gmail 發送
66
- # Gmail SMTP Server: smtp.gmail.com, Port: 465 (SSL)
67
- with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
68
- server.login(MAIL_USERNAME, MAIL_PASSWORD)
69
- server.send_message(msg)
70
 
71
- # 5. 更新狀態
72
- supabase.table("bookings").update({"status": "已發確認信"}).eq("id", booking_id).execute()
73
- return f" 已透過 Gmail 發送確認信給 {booking['name']} ({email})"
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  except Exception as e:
76
  return f"❌ 發信失敗: {str(e)}"
 
1
  import gradio as gr
2
  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)
14
 
 
22
  return df[cols]
23
 
24
  def send_confirmation_email(booking_id):
25
+ """透過 Google Apps Script 中繼站發信"""
26
  try:
27
+ if not GAS_MAIL_URL:
28
+ return "❌ 錯誤:未設定 GAS_MAIL_URL Secret"
29
+
30
  # 1. 抓取資料
31
  res = supabase.table("bookings").select("*").eq("id", booking_id).execute()
32
  if not res.data: return "❌ 找不到該訂單"
 
39
  confirm_link = f"{PUBLIC_SPACE_URL}/?id={booking_id}&action=confirm"
40
 
41
  # 3. 準備信件內容 (HTML)
 
 
 
 
 
42
  html_content = f"""
43
  <div style="padding: 20px; background: #111; color: #d4af37; font-family: sans-serif; border-radius: 10px;">
44
  <h2 style="border-bottom: 1px solid #d4af37; padding-bottom: 10px;">訂位保留確認</h2>
 
55
  <p style="color: #666; font-size: 12px;">若需取消,請直接回覆此信或致電 02-2709-3446。</p>
56
  </div>
57
  """
 
 
 
 
 
 
 
58
 
59
+ # 4. 呼叫 GAS 發信 (這走的是 Port 443,HF 不會擋)
60
+ payload = {
61
+ "to": email,
62
+ "subject": f"[{booking['date']}] Cié Cié Taipei 訂位確認",
63
+ "htmlBody": html_content,
64
+ "name": "Cié Cié Taipei"
65
+ }
66
+
67
+ response = requests.post(GAS_MAIL_URL, json=payload)
68
+
69
+ if response.status_code == 200:
70
+ # 5. 更新狀態
71
+ supabase.table("bookings").update({"status": "已發確認信"}).eq("id", booking_id).execute()
72
+ return f"✅ 已透過 GAS 發送確認信給 {booking['name']} ({email})"
73
+ else:
74
+ return f"❌ GAS 回傳錯誤: {response.text}"
75
 
76
  except Exception as e:
77
  return f"❌ 發信失敗: {str(e)}"