Spaces:
Sleeping
Sleeping
OnlyBiggg
commited on
Commit
·
9988c8e
1
Parent(s):
8985d0d
feat: add api select trip and time trip
Browse files
app/api/__pycache__/routes.cpython-39.pyc
CHANGED
|
Binary files a/app/api/__pycache__/routes.cpython-39.pyc and b/app/api/__pycache__/routes.cpython-39.pyc differ
|
|
|
app/api/routes.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, APIRouter, HTTPException, Request, Response # type: ignore
|
| 2 |
from fastapi.responses import JSONResponse, RedirectResponse, HTMLResponse # type: ignore
|
| 3 |
from datetime import datetime, timedelta
|
|
@@ -188,6 +189,8 @@ async def price(request: Request):
|
|
| 188 |
return DialogFlowResponseAPI(text=text, payload=payload)
|
| 189 |
except:
|
| 190 |
return DialogFlowResponseAPI(text=["Hệ thống xảy ra lỗi. Quý khách vui lòng thử lại sau hoặc liên hệ Trung tâm tổng đài 1900 6067 để được hỗ trợ."])
|
|
|
|
|
|
|
| 191 |
@router.post('/trip/list')
|
| 192 |
async def booking_trip(request: Request) -> Response:
|
| 193 |
body = await request.json()
|
|
@@ -218,25 +221,35 @@ async def booking_trip(request: Request) -> Response:
|
|
| 218 |
if total > 0:
|
| 219 |
list_raw_departure_times = sorted(list(set([ trip["raw_departure_time"] for trip in data])))
|
| 220 |
schedule_time_trip = "**" + "** | **".join(map(str, list_raw_departure_times)) + "**"
|
|
|
|
| 221 |
routes_name = []
|
| 222 |
for trip in data:
|
| 223 |
if trip["route"] and trip["route"]["name"] not in routes_name:
|
| 224 |
routes_name.append(trip["route"]["name"])
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
payload={
|
| 227 |
"richContent": [
|
| 228 |
[
|
| 229 |
{
|
| 230 |
"type": "chips",
|
| 231 |
"options": [
|
| 232 |
-
{"text": name} for name in routes_name
|
| 233 |
]
|
| 234 |
}
|
| 235 |
]
|
| 236 |
]
|
| 237 |
}
|
| 238 |
parameters = {
|
| 239 |
-
"trip_list":
|
|
|
|
| 240 |
}
|
| 241 |
return DialogFlowResponseAPI(text=text, payload=payload,parameters=parameters)
|
| 242 |
|
|
@@ -271,6 +284,58 @@ async def booking_trip(request: Request) -> Response:
|
|
| 271 |
},
|
| 272 |
}
|
| 273 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
@router.get("/")
|
| 275 |
def home():
|
| 276 |
return "Hello World!"
|
|
|
|
| 1 |
+
from typing import Dict, List
|
| 2 |
from fastapi import FastAPI, APIRouter, HTTPException, Request, Response # type: ignore
|
| 3 |
from fastapi.responses import JSONResponse, RedirectResponse, HTMLResponse # type: ignore
|
| 4 |
from datetime import datetime, timedelta
|
|
|
|
| 189 |
return DialogFlowResponseAPI(text=text, payload=payload)
|
| 190 |
except:
|
| 191 |
return DialogFlowResponseAPI(text=["Hệ thống xảy ra lỗi. Quý khách vui lòng thử lại sau hoặc liên hệ Trung tâm tổng đài 1900 6067 để được hỗ trợ."])
|
| 192 |
+
|
| 193 |
+
|
| 194 |
@router.post('/trip/list')
|
| 195 |
async def booking_trip(request: Request) -> Response:
|
| 196 |
body = await request.json()
|
|
|
|
| 221 |
if total > 0:
|
| 222 |
list_raw_departure_times = sorted(list(set([ trip["raw_departure_time"] for trip in data])))
|
| 223 |
schedule_time_trip = "**" + "** | **".join(map(str, list_raw_departure_times)) + "**"
|
| 224 |
+
trips = []
|
| 225 |
routes_name = []
|
| 226 |
for trip in data:
|
| 227 |
if trip["route"] and trip["route"]["name"] not in routes_name:
|
| 228 |
routes_name.append(trip["route"]["name"])
|
| 229 |
+
trips.append({
|
| 230 |
+
"route_name": trip["route"]["name"],
|
| 231 |
+
"route_id": trip["route_id"],
|
| 232 |
+
"id": trip["id"],
|
| 233 |
+
"departure_date": trip["raw_departure_date"],
|
| 234 |
+
"departure_time": trip["raw_departure_time"],
|
| 235 |
+
"kind": trip["seat_type_name"]
|
| 236 |
+
})
|
| 237 |
+
text = ["\n".join(f"{i+1}. {name}" for i, name in enumerate(routes_name))]
|
| 238 |
payload={
|
| 239 |
"richContent": [
|
| 240 |
[
|
| 241 |
{
|
| 242 |
"type": "chips",
|
| 243 |
"options": [
|
| 244 |
+
{"text": name} for name in (routes_name)
|
| 245 |
]
|
| 246 |
}
|
| 247 |
]
|
| 248 |
]
|
| 249 |
}
|
| 250 |
parameters = {
|
| 251 |
+
"trip_list": trips,
|
| 252 |
+
"routes_name": routes_name
|
| 253 |
}
|
| 254 |
return DialogFlowResponseAPI(text=text, payload=payload,parameters=parameters)
|
| 255 |
|
|
|
|
| 284 |
},
|
| 285 |
}
|
| 286 |
)
|
| 287 |
+
|
| 288 |
+
@router.post('trip/check-trip-select')
|
| 289 |
+
async def is_valid_select_trip(request: Request) -> Response:
|
| 290 |
+
body = await request.json()
|
| 291 |
+
raw_input = body.get("text", "")
|
| 292 |
+
print('raw input:', raw_input)
|
| 293 |
+
session_info = body.get("sessionInfo", {})
|
| 294 |
+
parameters = session_info.get("parameters")
|
| 295 |
+
routes_name = parameters.get("routes_name")
|
| 296 |
+
if raw_input in routes_name:
|
| 297 |
+
trip_list: List[Dict[str, any]] = parameters.get("trip_list")
|
| 298 |
+
for trip in trip_list:
|
| 299 |
+
if trip["route_name"] == raw_input:
|
| 300 |
+
route_id = trip["route_id"]
|
| 301 |
+
break
|
| 302 |
+
|
| 303 |
+
parameters = {
|
| 304 |
+
"is_valid_trip": True,
|
| 305 |
+
"route_name": raw_input,
|
| 306 |
+
"route_id": route_id | None
|
| 307 |
+
}
|
| 308 |
+
else:
|
| 309 |
+
parameters = {
|
| 310 |
+
"is_valid_trip": False,
|
| 311 |
+
}
|
| 312 |
+
return DialogFlowResponseAPI(parameters=parameters)
|
| 313 |
+
|
| 314 |
+
@router.post('trip/time-trip-list')
|
| 315 |
+
async def time_trip(request: Request) -> Response:
|
| 316 |
+
try:
|
| 317 |
+
body = await request.json()
|
| 318 |
+
session_info = body.get("sessionInfo", {})
|
| 319 |
+
parameters = session_info.get("parameters")
|
| 320 |
+
trip_list: List[Dict[str, any]] = parameters.get("trip_list", [])
|
| 321 |
+
raw_route_id = parameters.get("route_id", None)
|
| 322 |
+
route_id = int(raw_route_id) if raw_route_id else None
|
| 323 |
+
time_list = []
|
| 324 |
+
for trip in trip_list:
|
| 325 |
+
if trip["route_id"] == route_id:
|
| 326 |
+
time_list.append(trip["departure_time"])
|
| 327 |
+
parameters = {
|
| 328 |
+
"time_list": time_list
|
| 329 |
+
}
|
| 330 |
+
text = [" | ".join(map(str, time_list))]
|
| 331 |
+
return DialogFlowResponseAPI(text=text)
|
| 332 |
+
except Exception as e:
|
| 333 |
+
print(e)
|
| 334 |
+
return DialogFlowResponseAPI(text=["Hệ thống xảy ra lỗi. Quý khách vui lòng thử lại sau hoặc liên hệ Trung tâm tổng đài 1900 6067 để được hỗ trợ."])
|
| 335 |
+
|
| 336 |
+
return DialogFlowResponseAPI(parameters=parameters)
|
| 337 |
+
|
| 338 |
+
|
| 339 |
@router.get("/")
|
| 340 |
def home():
|
| 341 |
return "Hello World!"
|
app/types/__pycache__/Respone.cpython-39.pyc
CHANGED
|
Binary files a/app/types/__pycache__/Respone.cpython-39.pyc and b/app/types/__pycache__/Respone.cpython-39.pyc differ
|
|
|