DeepLearning101 commited on
Commit
95d95da
·
verified ·
1 Parent(s): 9bf23ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -22
app.py CHANGED
@@ -17,7 +17,7 @@ TAIPEI_TZ = timezone(timedelta(hours=8))
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 查詢用金鑰 ---
@@ -149,27 +149,49 @@ def send_confirmation_hybrid(booking_id):
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:
 
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", "") # 空字串代表不使用 API,直接走 GAS
21
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
22
 
23
  # --- 新增:LINE Pay 查詢用金鑰 ---
 
149
 
150
  # 執行發送
151
  if email and "@" in email:
152
+ mail_sent = False
153
+ # 優先使用 API 發信
154
+ if API_BASE_URL:
155
+ try:
156
+ api_mail_url = f"{API_BASE_URL}/api/send_email"
157
+ response = requests.post(
158
+ api_mail_url,
159
+ json={"to": email, "subject": mail_subject, "htmlBody": mail_html, "name": "Cié Cié Taipei"},
160
+ timeout=10
161
+ )
162
+ if response.status_code == 200:
163
+ log_msg += f"✅ Mail送出(API) "
164
+ mail_sent = True
165
+ else:
166
+ print(f"❌ Mail API 錯誤: {response.status_code} - {response.text},改用 GAS...")
167
+ except requests.exceptions.Timeout:
168
+ print(f"❌ Mail API 逾時,改用 GAS...")
169
+ except Exception as e:
170
+ print(f"❌ Mail API 異常: {str(e)},改用 GAS...")
171
+
172
+ # API 失敗時退回 GAS
173
+ if not mail_sent and GAS_MAIL_URL:
174
+ try:
175
+ response = requests.post(
176
+ GAS_MAIL_URL,
177
+ json={"to": email, "subject": mail_subject, "htmlBody": mail_html, "name": "Cié Cié Taipei"},
178
+ timeout=15
179
+ )
180
+ if response.status_code == 200:
181
+ log_msg += f"✅ Mail送出(GAS) "
182
+ mail_sent = True
183
+ else:
184
+ log_msg += f"⚠️ Mail異常(HTTP {response.status_code}) "
185
+ print(f"❌ GAS Mail 錯誤: {response.status_code} - {response.text}")
186
+ except requests.exceptions.Timeout:
187
+ log_msg += f"⏱️ Mail逾時 "
188
+ print(f"❌ GAS Mail 逾時")
189
+ except Exception as e:
190
+ log_msg += f"❌ Mail失敗 "
191
+ print(f"❌ GAS Mail 異常: {str(e)}")
192
+
193
+ if not mail_sent and not GAS_MAIL_URL and not API_BASE_URL:
194
+ log_msg += f"⚠️ 未設定發信服務 "
195
  else: log_msg += "⚠️ 無Mail "
196
 
197
  if user_id and len(str(user_id)) > 5 and LINE_ACCESS_TOKEN: