Spaces:
Sleeping
Sleeping
OnlyBiggg
commited on
Commit
·
c506a3f
1
Parent(s):
d8fb01c
feat: /seat /check_seat /pickup /dropoff /ticketinfo
Browse files- .gitignore +1 -0
- Dockerfile +0 -7
- app/dialogflow/api/v1/dialogflow.py +229 -32
- app/dialogflow/services/dialog_service.py +103 -4
- core/conf.py +1 -1
.gitignore
CHANGED
|
@@ -13,3 +13,4 @@ venv/
|
|
| 13 |
.python-version
|
| 14 |
.ruff_cache/00
|
| 15 |
.pytest_cache/
|
|
|
|
|
|
| 13 |
.python-version
|
| 14 |
.ruff_cache/00
|
| 15 |
.pytest_cache/
|
| 16 |
+
note.txt
|
Dockerfile
CHANGED
|
@@ -2,13 +2,6 @@ FROM python:3.10.16-slim
|
|
| 2 |
|
| 3 |
RUN adduser --disabled-password --gecos '' appuser
|
| 4 |
|
| 5 |
-
|
| 6 |
-
ENV API_BASE_URL=https://api-dev.futabus.vn
|
| 7 |
-
|
| 8 |
-
ENV API_BASE_URL_ACCESS_TOKEN=https://api-dev.futabus.vn/identity/api/token/anonymous-token
|
| 9 |
-
|
| 10 |
-
ENV ENVIRONMENT=dev
|
| 11 |
-
|
| 12 |
WORKDIR /app
|
| 13 |
|
| 14 |
COPY . .
|
|
|
|
| 2 |
|
| 3 |
RUN adduser --disabled-password --gecos '' appuser
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
COPY . .
|
app/dialogflow/api/v1/dialogflow.py
CHANGED
|
@@ -157,7 +157,7 @@ async def price(request: Request):
|
|
| 157 |
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ợ."])
|
| 158 |
|
| 159 |
|
| 160 |
-
@router.post('/trip/list')
|
| 161 |
async def booking_trip(request: Request) -> Response:
|
| 162 |
body = await request.json()
|
| 163 |
raw_departure_city, raw_destination_city, raw_ticket_number, raw_date, raw_time_of_day = dialog_service.get_param_from_dialogflow(body)
|
|
@@ -172,21 +172,10 @@ async def booking_trip(request: Request) -> Response:
|
|
| 172 |
route_dep_to_des = await dialog_service.search_route_ids_from_province(departure_code,destination_code)
|
| 173 |
route_des_to_dep = await dialog_service.search_route_ids_from_province(destination_code,departure_code)
|
| 174 |
routes_ids = list(set(route_dep_to_des + route_des_to_dep))
|
| 175 |
-
payload = {
|
| 176 |
-
"from_time": from_time,
|
| 177 |
-
"to_time": to_time,
|
| 178 |
-
"route_ids": routes_ids,
|
| 179 |
-
"ticket_count": ticket_count,
|
| 180 |
-
"sort_by": ["price", "departure_time"]
|
| 181 |
-
}
|
| 182 |
try:
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
total = response["data"]["total"]
|
| 187 |
-
if total > 0:
|
| 188 |
-
list_raw_departure_times = sorted(list(set([ trip["raw_departure_time"] for trip in data])))
|
| 189 |
-
schedule_time_trip = "**" + "** | **".join(map(str, list_raw_departure_times)) + "**"
|
| 190 |
trips = []
|
| 191 |
routes_name = []
|
| 192 |
for trip in data:
|
|
@@ -255,7 +244,7 @@ async def booking_trip(request: Request) -> Response:
|
|
| 255 |
}
|
| 256 |
)
|
| 257 |
|
| 258 |
-
@router.post('/trip/check-
|
| 259 |
async def is_valid_select_trip(request: Request) -> Response:
|
| 260 |
body = await request.json()
|
| 261 |
raw_input = body.get("text", "")
|
|
@@ -278,8 +267,7 @@ async def is_valid_select_trip(request: Request) -> Response:
|
|
| 278 |
"route_name": raw_input,
|
| 279 |
"kind": kind,
|
| 280 |
"route_id": route_id,
|
| 281 |
-
"way_id": way_id,
|
| 282 |
-
|
| 283 |
}
|
| 284 |
else:
|
| 285 |
parameters = {
|
|
@@ -301,7 +289,7 @@ async def time_trip(request: Request) -> Response:
|
|
| 301 |
time_list = []
|
| 302 |
for trip in trip_list:
|
| 303 |
if (trip["route_id"]) == route_id:
|
| 304 |
-
time_list.append(trip["departure_time"])
|
| 305 |
parameters = {
|
| 306 |
"time_list": time_list
|
| 307 |
}
|
|
@@ -312,7 +300,7 @@ async def time_trip(request: Request) -> Response:
|
|
| 312 |
{
|
| 313 |
"type": "chips",
|
| 314 |
"options": [
|
| 315 |
-
{"text": time} for time in (time_list)
|
| 316 |
]
|
| 317 |
}
|
| 318 |
]
|
|
@@ -329,23 +317,42 @@ async def is_valid_select_time(request: Request) -> Response:
|
|
| 329 |
body = await request.json()
|
| 330 |
session_info = body.get("sessionInfo", {})
|
| 331 |
parameters = session_info.get("parameters")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
time_list: List[Dict[str, any]] = parameters.get("time_list", [])
|
| 333 |
time = parameters.get("time")
|
| 334 |
route_name = parameters.get("route_name")
|
| 335 |
raw_input = body.get("text","")
|
| 336 |
if raw_input:
|
| 337 |
raw_input = raw_input.strip()
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
"
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
"is_valid_time": False
|
| 347 |
}
|
| 348 |
-
|
| 349 |
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 350 |
except Exception as e:
|
| 351 |
print(e)
|
|
@@ -359,12 +366,15 @@ async def seats_trip(request: Request) -> Response:
|
|
| 359 |
session_info = body.get("sessionInfo", {})
|
| 360 |
parameters = session_info.get("parameters")
|
| 361 |
|
| 362 |
-
|
| 363 |
route_id: int = parameters.get("route_id", None)
|
| 364 |
departure_date: str = parameters.get("departure_date", None)
|
| 365 |
departure_time: str = parameters.get("departure_time", None)
|
| 366 |
kind: str = parameters.get("kind", None)
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
| 368 |
text=["Vui lòng chọn ghế"]
|
| 369 |
payload={
|
| 370 |
"richContent": [
|
|
@@ -379,12 +389,199 @@ async def seats_trip(request: Request) -> Response:
|
|
| 379 |
]
|
| 380 |
}
|
| 381 |
parameters = {
|
| 382 |
-
"
|
| 383 |
}
|
| 384 |
return DialogFlowResponseAPI(text=text, payload=payload, parameters=parameters)
|
| 385 |
except Exception as e:
|
| 386 |
print(e)
|
| 387 |
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ợ."])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
@router.get("/")
|
| 389 |
def home():
|
| 390 |
return "Hello World!"
|
|
|
|
| 157 |
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ợ."])
|
| 158 |
|
| 159 |
|
| 160 |
+
@router.post('/trip/route/list')
|
| 161 |
async def booking_trip(request: Request) -> Response:
|
| 162 |
body = await request.json()
|
| 163 |
raw_departure_city, raw_destination_city, raw_ticket_number, raw_date, raw_time_of_day = dialog_service.get_param_from_dialogflow(body)
|
|
|
|
| 172 |
route_dep_to_des = await dialog_service.search_route_ids_from_province(departure_code,destination_code)
|
| 173 |
route_des_to_dep = await dialog_service.search_route_ids_from_province(destination_code,departure_code)
|
| 174 |
routes_ids = list(set(route_dep_to_des + route_des_to_dep))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
+
data = await dialog_service.search_trip(from_time, to_time, routes_ids, ticket_count)
|
| 177 |
+
total = len(data)
|
| 178 |
+
if total > 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
trips = []
|
| 180 |
routes_name = []
|
| 181 |
for trip in data:
|
|
|
|
| 244 |
}
|
| 245 |
)
|
| 246 |
|
| 247 |
+
@router.post('/trip/route/check-route-select')
|
| 248 |
async def is_valid_select_trip(request: Request) -> Response:
|
| 249 |
body = await request.json()
|
| 250 |
raw_input = body.get("text", "")
|
|
|
|
| 267 |
"route_name": raw_input,
|
| 268 |
"kind": kind,
|
| 269 |
"route_id": route_id,
|
| 270 |
+
"way_id": way_id,
|
|
|
|
| 271 |
}
|
| 272 |
else:
|
| 273 |
parameters = {
|
|
|
|
| 289 |
time_list = []
|
| 290 |
for trip in trip_list:
|
| 291 |
if (trip["route_id"]) == route_id:
|
| 292 |
+
time_list.append({"time": trip["departure_time"], "id": trip["id"]})
|
| 293 |
parameters = {
|
| 294 |
"time_list": time_list
|
| 295 |
}
|
|
|
|
| 300 |
{
|
| 301 |
"type": "chips",
|
| 302 |
"options": [
|
| 303 |
+
{"text": time["time"]} for time in (time_list)
|
| 304 |
]
|
| 305 |
}
|
| 306 |
]
|
|
|
|
| 317 |
body = await request.json()
|
| 318 |
session_info = body.get("sessionInfo", {})
|
| 319 |
parameters = session_info.get("parameters")
|
| 320 |
+
|
| 321 |
+
raw_departure_city, raw_destination_city, raw_ticket_number, raw_date, raw_time_of_day = dialog_service.get_param_from_dialogflow(body)
|
| 322 |
+
|
| 323 |
+
date = raw_date.strftime('%m-%d-%Y')
|
| 324 |
+
from_time, to_time = dialog_service.process_dates_to_timestamp(raw_date)
|
| 325 |
+
ticket_count = int(raw_ticket_number) if raw_ticket_number else 1
|
| 326 |
+
|
| 327 |
+
departure_code = origin_codes.get(raw_departure_city)
|
| 328 |
+
destination_code = origin_codes.get(raw_destination_city)
|
| 329 |
+
|
| 330 |
+
route_dep_to_des = await dialog_service.search_route_ids_from_province(departure_code,destination_code)
|
| 331 |
+
route_des_to_dep = await dialog_service.search_route_ids_from_province(destination_code,departure_code)
|
| 332 |
+
routes_ids = list(set(route_dep_to_des + route_des_to_dep))
|
| 333 |
+
|
| 334 |
time_list: List[Dict[str, any]] = parameters.get("time_list", [])
|
| 335 |
time = parameters.get("time")
|
| 336 |
route_name = parameters.get("route_name")
|
| 337 |
raw_input = body.get("text","")
|
| 338 |
if raw_input:
|
| 339 |
raw_input = raw_input.strip()
|
| 340 |
+
for time in time_list:
|
| 341 |
+
if raw_input == time["time"]:
|
| 342 |
+
id = int(time["id"])
|
| 343 |
+
trip = await dialog_service.search_trip_by_id(id, from_time, to_time, routes_ids, ticket_count)
|
| 344 |
+
parameters = {
|
| 345 |
+
"is_valid_time": True,
|
| 346 |
+
"departure_time": raw_input,
|
| 347 |
+
"trip": trip,
|
| 348 |
+
}
|
| 349 |
+
text = [f' Quý khách chọn chuyến {raw_input} | {route_name}']
|
| 350 |
+
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 351 |
+
|
| 352 |
+
parameters = {
|
| 353 |
"is_valid_time": False
|
| 354 |
}
|
| 355 |
+
text = []
|
| 356 |
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 357 |
except Exception as e:
|
| 358 |
print(e)
|
|
|
|
| 366 |
session_info = body.get("sessionInfo", {})
|
| 367 |
parameters = session_info.get("parameters")
|
| 368 |
|
| 369 |
+
trip = parameters.get("trip", None)
|
| 370 |
route_id: int = parameters.get("route_id", None)
|
| 371 |
departure_date: str = parameters.get("departure_date", None)
|
| 372 |
departure_time: str = parameters.get("departure_time", None)
|
| 373 |
kind: str = parameters.get("kind", None)
|
| 374 |
+
|
| 375 |
+
trip_id = int(trip["id"]) if trip else None
|
| 376 |
+
seats = await dialog_service.seats_trip(route_id, trip_id, departure_date, departure_time, kind)
|
| 377 |
+
seats_empty = [ seat for seat in seats if seat["bookStatus"] == 0 ]
|
| 378 |
text=["Vui lòng chọn ghế"]
|
| 379 |
payload={
|
| 380 |
"richContent": [
|
|
|
|
| 389 |
]
|
| 390 |
}
|
| 391 |
parameters = {
|
| 392 |
+
"seat_list": seats
|
| 393 |
}
|
| 394 |
return DialogFlowResponseAPI(text=text, payload=payload, parameters=parameters)
|
| 395 |
except Exception as e:
|
| 396 |
print(e)
|
| 397 |
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ợ."])
|
| 398 |
+
@router.post('/trip/check-seat-select')
|
| 399 |
+
async def is_valid_select_seat(request: Request) -> Response:
|
| 400 |
+
try:
|
| 401 |
+
body = await request.json()
|
| 402 |
+
session_info = body.get("sessionInfo", {})
|
| 403 |
+
parameters = session_info.get("parameters")
|
| 404 |
+
|
| 405 |
+
trip = parameters.get("trip", None)
|
| 406 |
+
route_id: int = parameters.get("route_id", None)
|
| 407 |
+
departure_date: str = parameters.get("departure_date", None)
|
| 408 |
+
departure_time: str = parameters.get("departure_time", None)
|
| 409 |
+
kind: str = parameters.get("kind", None)
|
| 410 |
+
seat: str = parameters.get("seat", None)
|
| 411 |
+
|
| 412 |
+
trip_id = int(trip["id"]) if trip else None
|
| 413 |
+
|
| 414 |
+
is_valid = await dialog_service.is_valid_select_seat(seat, route_id, trip_id, departure_date, departure_time, kind)
|
| 415 |
+
if is_valid:
|
| 416 |
+
parameters = {
|
| 417 |
+
"is_valid_seat": True,
|
| 418 |
+
"seat": seat
|
| 419 |
+
}
|
| 420 |
+
text = [f"Quý khách chọn ghế {seat}"]
|
| 421 |
+
else:
|
| 422 |
+
parameters = {
|
| 423 |
+
"is_valid_seat": False,
|
| 424 |
+
"seat": None
|
| 425 |
+
}
|
| 426 |
+
text = [f"Ghế {seat} đã được đặt. Quý khách vui lòng chọn ghế khác"]
|
| 427 |
+
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 428 |
+
|
| 429 |
+
except Exception as e:
|
| 430 |
+
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ợ."])
|
| 431 |
+
|
| 432 |
+
@router.post('/trip/stop/pickup')
|
| 433 |
+
async def pickup(request: Request) -> Response:
|
| 434 |
+
body = await request.json()
|
| 435 |
+
session_info = body.get("sessionInfo", {})
|
| 436 |
+
parameters = session_info.get("parameters")
|
| 437 |
+
trip = parameters.get("trip", None)
|
| 438 |
+
route_id: int = trip["route_id"]
|
| 439 |
+
way_id: int = trip["way_id"]
|
| 440 |
+
pickup_list = await dialog_service.pickup_list(route_id, way_id)
|
| 441 |
+
|
| 442 |
+
text=["Quý khách vui lòng chọn điểm đón"]
|
| 443 |
+
payload={
|
| 444 |
+
"richContent": [
|
| 445 |
+
[
|
| 446 |
+
{
|
| 447 |
+
"type": "chips",
|
| 448 |
+
"options": [
|
| 449 |
+
{"text": pickup["name"]} for pickup in (pickup_list)
|
| 450 |
+
]
|
| 451 |
+
}
|
| 452 |
+
]
|
| 453 |
+
]
|
| 454 |
+
}
|
| 455 |
+
parameters = {
|
| 456 |
+
"pickup_list": pickup_list
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
return DialogFlowResponseAPI(text=text, payload=payload, parameters=parameters)
|
| 460 |
+
|
| 461 |
+
@router.post('/trip/check-pickup-select')
|
| 462 |
+
async def is_valid_select_pickup(request: Request) -> Response:
|
| 463 |
+
body = await request.json()
|
| 464 |
+
session_info = body.get("sessionInfo", {})
|
| 465 |
+
parameters = session_info.get("parameters")
|
| 466 |
+
trip = parameters.get("trip", None)
|
| 467 |
+
route_id: int = trip["route_id"]
|
| 468 |
+
way_id: int = trip["way_id"]
|
| 469 |
+
raw_input = (body.get("text",""))
|
| 470 |
+
pickup = raw_input.strip()
|
| 471 |
+
|
| 472 |
+
is_valid = await dialog_service.is_valid_pickup(pickup, route_id, way_id)
|
| 473 |
+
|
| 474 |
+
if is_valid:
|
| 475 |
+
parameters = {
|
| 476 |
+
"is_valid_pickup": True,
|
| 477 |
+
"pick_up": pickup
|
| 478 |
+
}
|
| 479 |
+
text = [f"Quý khách chọn điểm đón {pickup}"]
|
| 480 |
+
else:
|
| 481 |
+
parameters = {
|
| 482 |
+
"is_valid_pickup": False,
|
| 483 |
+
}
|
| 484 |
+
text = [f"Điểm đón không hợp lệ. Quý khách vui lòng chọn điểm đón khác"]
|
| 485 |
+
|
| 486 |
+
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 487 |
+
|
| 488 |
+
@router.post('/trip/stop/dropoff')
|
| 489 |
+
async def dropoff(request: Request) -> Response:
|
| 490 |
+
body = await request.json()
|
| 491 |
+
session_info = body.get("sessionInfo", {})
|
| 492 |
+
parameters = session_info.get("parameters")
|
| 493 |
+
trip = parameters.get("trip", None)
|
| 494 |
+
route_id: int = trip["route_id"]
|
| 495 |
+
way_id: int = trip["way_id"]
|
| 496 |
+
dropoff_list = await dialog_service.dropoff_list(route_id, way_id)
|
| 497 |
+
|
| 498 |
+
text=["Quý khách vui lòng chọn điểm trả khách"]
|
| 499 |
+
payload={
|
| 500 |
+
"richContent": [
|
| 501 |
+
[
|
| 502 |
+
{
|
| 503 |
+
"type": "chips",
|
| 504 |
+
"options": [
|
| 505 |
+
{"text": dropoff["name"]} for dropoff in (dropoff_list)
|
| 506 |
+
]
|
| 507 |
+
}
|
| 508 |
+
]
|
| 509 |
+
]
|
| 510 |
+
}
|
| 511 |
+
parameters = {
|
| 512 |
+
"dropoff_list": dropoff_list
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
return DialogFlowResponseAPI(text=text, payload=payload, parameters=parameters)
|
| 516 |
+
|
| 517 |
+
@router.post('/trip/check-dropoff-select')
|
| 518 |
+
async def is_valid_select_dropoff(request: Request) -> Response:
|
| 519 |
+
body = await request.json()
|
| 520 |
+
session_info = body.get("sessionInfo", {})
|
| 521 |
+
parameters = session_info.get("parameters")
|
| 522 |
+
trip = parameters.get("trip", None)
|
| 523 |
+
route_id: int = trip["route_id"]
|
| 524 |
+
way_id: int = trip["way_id"]
|
| 525 |
+
raw_input = (body.get("text",""))
|
| 526 |
+
dropoff = raw_input.strip()
|
| 527 |
+
|
| 528 |
+
is_valid = await dialog_service.is_valid_dropoff(dropoff, route_id, way_id)
|
| 529 |
+
|
| 530 |
+
if is_valid:
|
| 531 |
+
parameters = {
|
| 532 |
+
"is_valid_dropoff": True,
|
| 533 |
+
"drop_off": dropoff
|
| 534 |
+
}
|
| 535 |
+
text = [f"Quý khách chọn điểm trả khách {dropoff}"]
|
| 536 |
+
else:
|
| 537 |
+
parameters = {
|
| 538 |
+
"is_valid_dropoff": False,
|
| 539 |
+
}
|
| 540 |
+
text = [f"Điểm trả khách không hợp lệ. Quý khách vui lòng chọn điểm trả khách khác"]
|
| 541 |
+
|
| 542 |
+
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 543 |
+
|
| 544 |
+
@router.post('/trip/ticket/info')
|
| 545 |
+
async def response_ticket_info(request: Request) -> Response:
|
| 546 |
+
body = await request.json()
|
| 547 |
+
session_info = body.get("sessionInfo", {})
|
| 548 |
+
parameters = session_info.get("parameters")
|
| 549 |
+
|
| 550 |
+
user_name = parameters.get("user_name", None)
|
| 551 |
+
phone_number = parameters.get("phone_number", None)
|
| 552 |
+
email = parameters.get("email", None)
|
| 553 |
+
seat = parameters.get("seat", None)
|
| 554 |
+
pickup = parameters.get("pick_up", None)
|
| 555 |
+
dropoff = parameters.get("drop_off", None)
|
| 556 |
+
|
| 557 |
+
trip = parameters.get("trip", None)
|
| 558 |
+
|
| 559 |
+
route_name = trip["route"]["name"]
|
| 560 |
+
time = trip["raw_departure_time"]
|
| 561 |
+
date = trip["raw_departure_date"]
|
| 562 |
+
price = trip["price"]
|
| 563 |
+
|
| 564 |
+
text = [
|
| 565 |
+
f"""
|
| 566 |
+
**Thông tin hành khách**
|
| 567 |
+
|
| 568 |
+
Họ và tên {user_name}
|
| 569 |
+
Số điện thoại {phone_number}
|
| 570 |
+
Email {email}
|
| 571 |
+
|
| 572 |
+
**Thông tin lượt đi**
|
| 573 |
+
|
| 574 |
+
Tuyến xe {route_name}
|
| 575 |
+
Thời gian xuất bến {time} {date}
|
| 576 |
+
Số ghế {seat}
|
| 577 |
+
Điểm lên xe {pickup}
|
| 578 |
+
Điểm trả khách {dropoff}
|
| 579 |
+
Tổng tiền lượt đi {price} VND
|
| 580 |
+
"""
|
| 581 |
+
]
|
| 582 |
+
return DialogFlowResponseAPI(text=text)
|
| 583 |
+
|
| 584 |
+
|
| 585 |
@router.get("/")
|
| 586 |
def home():
|
| 587 |
return "Hello World!"
|
app/dialogflow/services/dialog_service.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from datetime import datetime, timedelta
|
| 2 |
|
| 3 |
from common.external.external_api import api
|
|
|
|
| 4 |
|
| 5 |
class DialogService:
|
| 6 |
@staticmethod
|
|
@@ -19,7 +20,6 @@ class DialogService:
|
|
| 19 |
to_time = datetime.combine(today, datetime.max.time()) - timedelta(microseconds=1)
|
| 20 |
|
| 21 |
return int(from_time.timestamp()) * 1000 , int(to_time.timestamp()) * 1000
|
| 22 |
-
|
| 23 |
|
| 24 |
def get_param_from_dialogflow(self, body: any):
|
| 25 |
session_info = body.get("sessionInfo", {})
|
|
@@ -49,13 +49,112 @@ class DialogService:
|
|
| 49 |
"departureTime": departure_time,
|
| 50 |
"kind": kind,
|
| 51 |
}
|
| 52 |
-
response = api.get(api_base=
|
| 53 |
seats = response["data"]
|
| 54 |
-
|
| 55 |
-
return seats_empty, seats
|
| 56 |
except Exception as e:
|
| 57 |
print(e)
|
| 58 |
raise Exception("Error fetching seats data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
dialog_service: DialogService = DialogService()
|
| 61 |
|
|
|
|
| 1 |
from datetime import datetime, timedelta
|
| 2 |
|
| 3 |
from common.external.external_api import api
|
| 4 |
+
from core.conf import settings
|
| 5 |
|
| 6 |
class DialogService:
|
| 7 |
@staticmethod
|
|
|
|
| 20 |
to_time = datetime.combine(today, datetime.max.time()) - timedelta(microseconds=1)
|
| 21 |
|
| 22 |
return int(from_time.timestamp()) * 1000 , int(to_time.timestamp()) * 1000
|
|
|
|
| 23 |
|
| 24 |
def get_param_from_dialogflow(self, body: any):
|
| 25 |
session_info = body.get("sessionInfo", {})
|
|
|
|
| 49 |
"departureTime": departure_time,
|
| 50 |
"kind": kind,
|
| 51 |
}
|
| 52 |
+
response = api.get(api_base=settings.API_BASE_URL_VATO, endpoint=f"/seats/{route_id}/{trip_id}" , params=params)
|
| 53 |
seats = response["data"]
|
| 54 |
+
return seats
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
print(e)
|
| 57 |
raise Exception("Error fetching seats data")
|
| 58 |
+
|
| 59 |
+
async def is_valid_select_seat(self, seat: str,route_id: int, trip_id:int, departure_date: str, departure_time: str, kind: str):
|
| 60 |
+
if seat is None:
|
| 61 |
+
return False
|
| 62 |
+
seats = await self.seats_trip(route_id, trip_id, departure_date, departure_time, kind)
|
| 63 |
+
for seat_data in seats:
|
| 64 |
+
if seat_data['chair'] == seat and seat_data['bookStatus'] == 'available':
|
| 65 |
+
return True
|
| 66 |
+
return False
|
| 67 |
+
|
| 68 |
+
@staticmethod
|
| 69 |
+
async def search_trip(from_time: int, to_time: int, route_ids: list[int], ticket_count: int = 1):
|
| 70 |
+
try:
|
| 71 |
+
payload = {
|
| 72 |
+
"from_time": from_time,
|
| 73 |
+
"to_time": to_time,
|
| 74 |
+
"route_ids": route_ids,
|
| 75 |
+
"ticket_count": ticket_count,
|
| 76 |
+
"sort_by": ["price", "departure_time"]
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
response = await api.post("/search/trips", payload=payload)
|
| 80 |
+
data = response["data"]["items"]
|
| 81 |
+
return data
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(e)
|
| 84 |
+
raise Exception("Error fetching trip data")
|
| 85 |
+
async def search_trip_by_id(self, trip_id: int, from_time: int, to_time: int, route_ids: list[int], ticket_count: int = 1):
|
| 86 |
+
trip = self.search_trip(from_time, to_time, route_ids, ticket_count)
|
| 87 |
+
for item in trip:
|
| 88 |
+
if trip_id == item["id"]:
|
| 89 |
+
return item
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
@staticmethod
|
| 93 |
+
async def stops(route_id: int, way_id: int):
|
| 94 |
+
try:
|
| 95 |
+
params = {
|
| 96 |
+
"wayId": way_id,
|
| 97 |
+
}
|
| 98 |
+
response = await api.get(api_base=settings.API_BASE_URL_VATO, endpoint=f"/stops/{route_id}", params=params)
|
| 99 |
+
data = response["data"]
|
| 100 |
+
return data
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(e)
|
| 103 |
+
raise Exception("Error fetching stops data")
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
async def pickup_list(self, route_id: int, way_id: int):
|
| 107 |
+
try:
|
| 108 |
+
data = self.stops(route_id, way_id)
|
| 109 |
+
if not data:
|
| 110 |
+
return []
|
| 111 |
+
|
| 112 |
+
pickup_list = []
|
| 113 |
+
|
| 114 |
+
for pickup in data:
|
| 115 |
+
if pickup["type"] == 0 or ( pickup["type"] == -1 and pickup["presentBeforeMinutes"] < 0 ):
|
| 116 |
+
pickup_list.append(pickup)
|
| 117 |
+
return pickup_list
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(e)
|
| 121 |
+
raise Exception("Error fetching pickup list data")
|
| 122 |
+
|
| 123 |
+
async def is_valid_pickup(self, pickup: str, route_id: int, way_id: int):
|
| 124 |
+
if pickup is None:
|
| 125 |
+
return False
|
| 126 |
+
pickup_list = await self.pickup_list(route_id, way_id)
|
| 127 |
+
for pickup_data in pickup_list:
|
| 128 |
+
if pickup_data['name'] == pickup:
|
| 129 |
+
return True
|
| 130 |
+
return False
|
| 131 |
+
|
| 132 |
+
async def dropoff_list(self, route_id: int, way_id: int):
|
| 133 |
+
try:
|
| 134 |
+
data = self.stops(route_id, way_id)
|
| 135 |
+
if not data:
|
| 136 |
+
return []
|
| 137 |
+
|
| 138 |
+
dropoff_list = []
|
| 139 |
+
|
| 140 |
+
for dropoff in data:
|
| 141 |
+
if dropoff["type"] == 1 or ( dropoff["type"] == -1 and dropoff["presentBeforeMinutes"] >= 0 ):
|
| 142 |
+
dropoff_list.append(dropoff)
|
| 143 |
+
return dropoff_list
|
| 144 |
+
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print(e)
|
| 147 |
+
raise Exception("Error fetching dropoff list data")
|
| 148 |
|
| 149 |
+
async def is_valid_dropoff(self, dropoff: str, route_id: int, way_id: int):
|
| 150 |
+
if dropoff is None:
|
| 151 |
+
return False
|
| 152 |
+
dropoff_list = await self.dropoff_list(route_id, way_id)
|
| 153 |
+
for dropoff_data in dropoff_list:
|
| 154 |
+
if dropoff_data['name'] == dropoff:
|
| 155 |
+
return True
|
| 156 |
+
return False
|
| 157 |
+
|
| 158 |
+
|
| 159 |
dialog_service: DialogService = DialogService()
|
| 160 |
|
core/conf.py
CHANGED
|
@@ -22,6 +22,7 @@ class Settings(BaseSettings):
|
|
| 22 |
|
| 23 |
|
| 24 |
API_BASE_URL: str
|
|
|
|
| 25 |
API_BASE_URL_ACCESS_TOKEN: str
|
| 26 |
|
| 27 |
# DATABASE_TYPE: Literal['mysql', 'postgresql']
|
|
@@ -99,7 +100,6 @@ class Settings(BaseSettings):
|
|
| 99 |
@model_validator(mode='before')
|
| 100 |
@classmethod
|
| 101 |
def check_env(cls, values: Any) -> Any:
|
| 102 |
-
"""生产环境下禁用 OpenAPI 文档和静态文件服务"""
|
| 103 |
if values.get('ENVIRONMENT') == 'pro':
|
| 104 |
values['FASTAPI_OPENAPI_URL'] = None
|
| 105 |
values['FASTAPI_STATIC_FILES'] = False
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
API_BASE_URL: str
|
| 25 |
+
API_BASE_URL_VATO: str
|
| 26 |
API_BASE_URL_ACCESS_TOKEN: str
|
| 27 |
|
| 28 |
# DATABASE_TYPE: Literal['mysql', 'postgresql']
|
|
|
|
| 100 |
@model_validator(mode='before')
|
| 101 |
@classmethod
|
| 102 |
def check_env(cls, values: Any) -> Any:
|
|
|
|
| 103 |
if values.get('ENVIRONMENT') == 'pro':
|
| 104 |
values['FASTAPI_OPENAPI_URL'] = None
|
| 105 |
values['FASTAPI_STATIC_FILES'] = False
|