Update app.py
Browse files
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", "")
|
|
|
|
| 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 |
-
|
| 154 |
-
|
|
|
|
| 155 |
try:
|
| 156 |
-
api_mail_url = f"{API_BASE_URL}/api/send_email"
|
| 157 |
response = requests.post(
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
)
|
| 162 |
-
if response.status_code
|
| 163 |
-
log_msg +=
|
| 164 |
mail_sent = True
|
| 165 |
else:
|
| 166 |
-
print(f"❌
|
| 167 |
-
except requests.exceptions.Timeout:
|
| 168 |
-
print(f"❌ Mail API 逾時,改用 GAS...")
|
| 169 |
except Exception as e:
|
| 170 |
-
print(f"❌
|
| 171 |
|
| 172 |
-
#
|
| 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 +=
|
| 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 +=
|
| 191 |
print(f"❌ GAS Mail 異常: {str(e)}")
|
| 192 |
|
| 193 |
-
if not mail_sent
|
| 194 |
-
log_msg +=
|
| 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:
|