Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
"""驗證琴房選擇"""
|
| 3 |
-
room_normalized = room_str.strip().upper()
|
| 4 |
-
for room in AVAILABLE_ROOMS:
|
| 5 |
-
if room.upper() in room_normalized or room_normalized in room.upper():
|
| 6 |
-
return room, None
|
| 7 |
-
return None, f"❌ 請選擇:{', '.join(AVAILABLE_ROOMS)}"import json, os, glob, pathlib, time, re
|
| 8 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from fastapi.responses import HTMLResponse, JSONResponse
|
|
@@ -518,21 +512,7 @@ def handle_booking_flow(user_id, user_message):
|
|
| 518 |
elif current_state == BookingState.ASKING_ROOM:
|
| 519 |
room, error = validate_room(user_message)
|
| 520 |
if error:
|
| 521 |
-
# 格式錯誤,保持在當前狀態
|
| 522 |
return error
|
| 523 |
-
|
| 524 |
-
# 檢查時段是否有衝突
|
| 525 |
-
has_conflict, conflict_msg = check_booking_conflict(
|
| 526 |
-
booking["date"],
|
| 527 |
-
booking["start_time"],
|
| 528 |
-
booking["end_time"],
|
| 529 |
-
room
|
| 530 |
-
)
|
| 531 |
-
|
| 532 |
-
if has_conflict:
|
| 533 |
-
# 有衝突,保持在當前狀態,讓使用者重新選擇琴房
|
| 534 |
-
return conflict_msg
|
| 535 |
-
|
| 536 |
booking["room"] = room
|
| 537 |
booking["state"] = BookingState.CONFIRMING
|
| 538 |
|
|
@@ -544,8 +524,7 @@ def handle_booking_flow(user_id, user_message):
|
|
| 544 |
f"👥 {booking['people']}人\n"
|
| 545 |
f"🎹 {booking['room']}\n"
|
| 546 |
f"{'='*20}\n\n"
|
| 547 |
-
f"
|
| 548 |
-
f"輸入「取消」重新填寫"
|
| 549 |
)
|
| 550 |
|
| 551 |
elif current_state == BookingState.CONFIRMING:
|
|
@@ -622,32 +601,6 @@ def get_bookings():
|
|
| 622 |
return {"total": 0, "bookings": [], "error": "讀取失敗"}
|
| 623 |
return {"total": 0, "bookings": []}
|
| 624 |
|
| 625 |
-
@app.get("/bookings/check")
|
| 626 |
-
def check_availability(date: str = None, room: str = None):
|
| 627 |
-
"""檢查特定日期或琴房的預約狀況"""
|
| 628 |
-
bookings_file = pathlib.Path("bookings/bookings.json")
|
| 629 |
-
if not bookings_file.exists():
|
| 630 |
-
return {"message": "無預約資料"}
|
| 631 |
-
|
| 632 |
-
with open(bookings_file, 'r', encoding='utf-8') as f:
|
| 633 |
-
try:
|
| 634 |
-
all_bookings = json.load(f)
|
| 635 |
-
except:
|
| 636 |
-
return {"error": "讀取失敗"}
|
| 637 |
-
|
| 638 |
-
# 篩選預約
|
| 639 |
-
filtered = all_bookings
|
| 640 |
-
if date:
|
| 641 |
-
filtered = [b for b in filtered if b.get('date') == date]
|
| 642 |
-
if room:
|
| 643 |
-
filtered = [b for b in filtered if b.get('room') == room]
|
| 644 |
-
|
| 645 |
-
return {
|
| 646 |
-
"date": date,
|
| 647 |
-
"room": room,
|
| 648 |
-
"count": len(filtered),
|
| 649 |
-
"bookings": filtered
|
| 650 |
-
}
|
| 651 |
@app.get("/bookings/latest", response_class=JSONResponse)
|
| 652 |
def get_latest_booking():
|
| 653 |
"""最新預約"""
|
|
|
|
| 1 |
+
import json, os, glob, pathlib, time, re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.responses import HTMLResponse, JSONResponse
|
|
|
|
| 512 |
elif current_state == BookingState.ASKING_ROOM:
|
| 513 |
room, error = validate_room(user_message)
|
| 514 |
if error:
|
|
|
|
| 515 |
return error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
booking["room"] = room
|
| 517 |
booking["state"] = BookingState.CONFIRMING
|
| 518 |
|
|
|
|
| 524 |
f"👥 {booking['people']}人\n"
|
| 525 |
f"🎹 {booking['room']}\n"
|
| 526 |
f"{'='*20}\n\n"
|
| 527 |
+
f"輸入「確認」送出"
|
|
|
|
| 528 |
)
|
| 529 |
|
| 530 |
elif current_state == BookingState.CONFIRMING:
|
|
|
|
| 601 |
return {"total": 0, "bookings": [], "error": "讀取失敗"}
|
| 602 |
return {"total": 0, "bookings": []}
|
| 603 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
@app.get("/bookings/latest", response_class=JSONResponse)
|
| 605 |
def get_latest_booking():
|
| 606 |
"""最新預約"""
|