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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -17,7 +17,8 @@ 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", "") # 空字串代表不使用 API,直接走 GAS
 
21
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
22
 
23
  # --- 新增:LINE Pay 查詢用金鑰 ---
@@ -150,26 +151,25 @@ def send_confirmation_hybrid(booking_id):
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(
@@ -178,20 +178,16 @@ def send_confirmation_hybrid(booking_id):
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:
 
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", "")
21
+ RESEND_API_KEY = os.getenv("RESEND_API_KEY", "")
22
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
23
 
24
  # --- 新增:LINE Pay 查詢用金鑰 ---
 
151
  # 執行發送
152
  if email and "@" in email:
153
  mail_sent = False
154
+
155
+ # 優先:Resend API
156
+ if RESEND_API_KEY:
157
  try:
 
158
  response = requests.post(
159
+ "https://api.resend.com/emails",
160
+ headers={"Authorization": f"Bearer {RESEND_API_KEY}", "Content-Type": "application/json"},
161
+ json={"from": "Cié Cié Taipei <onboarding@resend.dev>", "to": [email], "subject": mail_subject, "html": mail_html},
162
+ timeout=15
163
  )
164
+ if response.status_code in (200, 201):
165
+ log_msg += "✅ Mail送出 "
166
  mail_sent = True
167
  else:
168
+ print(f"❌ Resend 錯誤: {response.status_code} - {response.text}")
 
 
169
  except Exception as e:
170
+ print(f"❌ Resend 異常: {str(e)}")
171
 
172
+ # Fallback:GAS
173
  if not mail_sent and GAS_MAIL_URL:
174
  try:
175
  response = requests.post(
 
178
  timeout=15
179
  )
180
  if response.status_code == 200:
181
+ log_msg += "✅ Mail送出(GAS) "
182
  mail_sent = True
183
  else:
184
  log_msg += f"⚠️ Mail異常(HTTP {response.status_code}) "
 
 
 
 
185
  except Exception as e:
186
+ log_msg += "❌ Mail失敗 "
187
  print(f"❌ GAS Mail 異常: {str(e)}")
188
 
189
+ if not mail_sent:
190
+ log_msg += "⚠️ 未設定發信服務 "
191
  else: log_msg += "⚠️ 無Mail "
192
 
193
  if user_id and len(str(user_id)) > 5 and LINE_ACCESS_TOKEN: