DeepLearning101 commited on
Commit
9bf23ef
·
verified ·
1 Parent(s): 3f1ed99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -16,7 +16,8 @@ TAIPEI_TZ = timezone(timedelta(hours=8))
16
 
17
  SUPABASE_URL = os.getenv("SUPABASE_URL")
18
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
19
- GAS_MAIL_URL = os.getenv("GAS_MAIL_URL")
 
20
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
21
 
22
  # --- 新增:LINE Pay 查詢用金鑰 ---
@@ -147,26 +148,28 @@ def send_confirmation_hybrid(booking_id):
147
  <a href="{cancel_link}" style="display:inline-block; border:1px solid #555; color:#aaa; padding:11px 29px; text-decoration:none; border-radius:50px;">🚫 取消</a></div></div>"""
148
 
149
  # 執行發送
150
- if email and "@" in email and GAS_MAIL_URL:
151
  try:
152
- # 🔥 改進:加上 timeout 和狀態檢查
 
 
153
  response = requests.post(
154
- GAS_MAIL_URL,
155
  json={"to": email, "subject": mail_subject, "htmlBody": mail_html, "name": "Cié Cié Taipei"},
156
- timeout=10 # 10秒超時
157
  )
158
- # 檢查 HTTP 狀態碼
159
  if response.status_code == 200:
160
  log_msg += f"✅ Mail送出 "
161
  else:
162
  log_msg += f"⚠️ Mail異常(HTTP {response.status_code}) "
163
- print(f"❌ GAS Mail API 錯誤: {response.status_code} - {response.text}")
164
  except requests.exceptions.Timeout:
165
  log_msg += f"⏱️ Mail逾時 "
166
- print(f"❌ GAS Mail API 逾時: {GAS_MAIL_URL}")
167
  except Exception as e:
168
  log_msg += f"❌ Mail失敗 "
169
- print(f"❌ GAS Mail API 異常: {str(e)}")
170
  else: log_msg += "⚠️ 無Mail "
171
 
172
  if user_id and len(str(user_id)) > 5 and LINE_ACCESS_TOKEN:
 
16
 
17
  SUPABASE_URL = os.getenv("SUPABASE_URL")
18
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
19
+ GAS_MAIL_URL = os.getenv("GAS_MAIL_URL") # 保留舊的(向下相容)
20
+ API_BASE_URL = os.getenv("API_BASE_URL", "https://deeplearning101-api-ciecietaipei.hf.space") # 🌟 新增:API 網址
21
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
22
 
23
  # --- 新增:LINE Pay 查詢用金鑰 ---
 
148
  <a href="{cancel_link}" style="display:inline-block; border:1px solid #555; color:#aaa; padding:11px 29px; text-decoration:none; border-radius:50px;">🚫 取消</a></div></div>"""
149
 
150
  # 執行發送
151
+ if email and "@" in email:
152
  try:
153
+ # 🌟 優先使用 API 發信,如果失敗則回退到 GAS
154
+ mail_url = f"{API_BASE_URL}/api/send_email" if API_BASE_URL else GAS_MAIL_URL
155
+
156
  response = requests.post(
157
+ mail_url,
158
  json={"to": email, "subject": mail_subject, "htmlBody": mail_html, "name": "Cié Cié Taipei"},
159
+ timeout=10
160
  )
161
+
162
  if response.status_code == 200:
163
  log_msg += f"✅ Mail送出 "
164
  else:
165
  log_msg += f"⚠️ Mail異常(HTTP {response.status_code}) "
166
+ print(f"❌ Mail API 錯誤: {response.status_code} - {response.text}")
167
  except requests.exceptions.Timeout:
168
  log_msg += f"⏱️ Mail逾時 "
169
+ print(f"❌ Mail API 逾時: {mail_url}")
170
  except Exception as e:
171
  log_msg += f"❌ Mail失敗 "
172
+ print(f"❌ Mail API 異常: {str(e)}")
173
  else: log_msg += "⚠️ 無Mail "
174
 
175
  if user_id and len(str(user_id)) > 5 and LINE_ACCESS_TOKEN: