Spaces:
Sleeping
Sleeping
OnlyBiggg
commited on
Commit
·
a9c1303
1
Parent(s):
8c43b4e
fi
Browse files- .gitignore +1 -1
- app/dialogflow/api/v1/dialogflow.py +64 -27
- app/dialogflow/services/dialog_service.py +0 -1
- utils/format_data_dialog.py +50 -9
.gitignore
CHANGED
|
@@ -16,4 +16,4 @@ venv/
|
|
| 16 |
.pytest_cache/
|
| 17 |
note.txt
|
| 18 |
|
| 19 |
-
|
|
|
|
| 16 |
.pytest_cache/
|
| 17 |
note.txt
|
| 18 |
|
| 19 |
+
process_data/
|
app/dialogflow/api/v1/dialogflow.py
CHANGED
|
@@ -4,7 +4,7 @@ from fastapi.responses import JSONResponse, RedirectResponse, HTMLResponse # typ
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from fastapi.templating import Jinja2Templates
|
| 6 |
from app.dialogflow.services.dialog_service import dialog_service
|
| 7 |
-
from utils.format_data_dialog import
|
| 8 |
|
| 9 |
from common.external.external_api import api
|
| 10 |
from app.dialogflow.services.origin_codes import origin_codes, get_origin_id_and_code
|
|
@@ -394,30 +394,56 @@ async def time_trip(request: Request) -> Response:
|
|
| 394 |
session_info = body.get("sessionInfo", {})
|
| 395 |
parameters = session_info.get("parameters")
|
| 396 |
trip_list: List[Dict[str, any]] = parameters.get("trip_list", [])
|
| 397 |
-
raw_route_id = parameters.get("route_id"
|
| 398 |
route_name = parameters.get("route_name", "")
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
| 400 |
route_id = int(raw_route_id) if raw_route_id else None
|
|
|
|
|
|
|
| 401 |
time_list = []
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
payload={
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
|
|
|
| 418 |
]
|
| 419 |
-
|
| 420 |
-
}
|
| 421 |
return DialogFlowResponseAPI(text=text, payload=payload ,parameters=parameters)
|
| 422 |
except Exception as e:
|
| 423 |
print(e)
|
|
@@ -452,15 +478,17 @@ async def is_valid_select_time(request: Request) -> Response:
|
|
| 452 |
route_ids = await dialog_service.search_all_route_ids(origin_code=origin_code, from_id=origin_id, orign_ids=origin_ids, dest_code=dest_code, to_id=dest_id, dest_ids=dest_ids)
|
| 453 |
|
| 454 |
|
| 455 |
-
time_list:
|
| 456 |
-
time_select = parameters.get("time-select"
|
| 457 |
-
route_name = parameters.get("route_name"
|
|
|
|
|
|
|
| 458 |
|
| 459 |
-
if time_select
|
| 460 |
-
time_select =
|
| 461 |
for time in time_list:
|
| 462 |
if time_select == time["time"]:
|
| 463 |
-
id = int(time["
|
| 464 |
trip = await dialog_service.search_trip_by_id(id, from_time, to_time, route_ids, ticket_count)
|
| 465 |
departure_date = trip["raw_departure_date"]
|
| 466 |
parameters = {
|
|
@@ -469,10 +497,19 @@ async def is_valid_select_time(request: Request) -> Response:
|
|
| 469 |
"departure_date": departure_date,
|
| 470 |
"trip": trip,
|
| 471 |
}
|
| 472 |
-
text = [f'
|
| 473 |
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
parameters = {
|
|
|
|
| 476 |
"is_valid_time": False
|
| 477 |
}
|
| 478 |
text = []
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from fastapi.templating import Jinja2Templates
|
| 6 |
from app.dialogflow.services.dialog_service import dialog_service
|
| 7 |
+
from utils.format_data_dialog import extra_time_dialogflow, get_weekday_name, find_surrounding_times
|
| 8 |
|
| 9 |
from common.external.external_api import api
|
| 10 |
from app.dialogflow.services.origin_codes import origin_codes, get_origin_id_and_code
|
|
|
|
| 394 |
session_info = body.get("sessionInfo", {})
|
| 395 |
parameters = session_info.get("parameters")
|
| 396 |
trip_list: List[Dict[str, any]] = parameters.get("trip_list", [])
|
| 397 |
+
raw_route_id = parameters.get("route_id")
|
| 398 |
route_name = parameters.get("route_name", "")
|
| 399 |
+
raw_time = parameters.get("time-select")
|
| 400 |
+
time_list = parameters.get("time_list", [])
|
| 401 |
+
is_has_time = parameters.get("is_has_time", False)
|
| 402 |
+
|
| 403 |
route_id = int(raw_route_id) if raw_route_id else None
|
| 404 |
+
|
| 405 |
+
text = []
|
| 406 |
time_list = []
|
| 407 |
+
quick_time_reply = []
|
| 408 |
+
parameters = {}
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
if raw_time:
|
| 413 |
+
time = extra_time_dialogflow(raw_time)
|
| 414 |
+
if isinstance(time, list):
|
| 415 |
+
quick_time_reply = time
|
| 416 |
+
parameters["time_select"] = None
|
| 417 |
+
parameters["is_time_ambiguous"] = True
|
| 418 |
+
text = [f"Quý khách dự định đi vào lúc"]
|
| 419 |
+
else:
|
| 420 |
+
text = []
|
| 421 |
+
if is_has_time:
|
| 422 |
+
text = [f"Quý khách lựa chọn thời gian chuyến {route_name}\n" + " | ".join([item["time"] for item in time_list])]
|
| 423 |
+
parameters["is_has_time"] = None
|
| 424 |
+
else:
|
| 425 |
+
parameters["is_has_time"] = True
|
| 426 |
+
return DialogFlowResponseAPI(text=text,parameters=parameters)
|
| 427 |
+
else:
|
| 428 |
+
for trip in trip_list:
|
| 429 |
+
if (trip["route_id"]) == route_id:
|
| 430 |
+
time_list.append({"time": trip["departure_time"], "trip_id": trip["id"]})
|
| 431 |
+
quick_time_reply = time_list
|
| 432 |
+
text = [f"Quý khách lựa chọn thời gian chuyến {route_name}\n" + " | ".join([item["time"] for item in time_list])]
|
| 433 |
+
|
| 434 |
+
parameters["time_list"] = time_list
|
| 435 |
payload={
|
| 436 |
+
"richContent": [
|
| 437 |
+
[
|
| 438 |
+
{
|
| 439 |
+
"type": "chips",
|
| 440 |
+
"options": [
|
| 441 |
+
{"text": time["time"]} for time in (quick_time_reply)
|
| 442 |
+
]
|
| 443 |
+
}
|
| 444 |
+
]
|
| 445 |
]
|
| 446 |
+
}
|
|
|
|
| 447 |
return DialogFlowResponseAPI(text=text, payload=payload ,parameters=parameters)
|
| 448 |
except Exception as e:
|
| 449 |
print(e)
|
|
|
|
| 478 |
route_ids = await dialog_service.search_all_route_ids(origin_code=origin_code, from_id=origin_id, orign_ids=origin_ids, dest_code=dest_code, to_id=dest_id, dest_ids=dest_ids)
|
| 479 |
|
| 480 |
|
| 481 |
+
time_list: list[Dict[str, any]] = parameters.get("time_list", [])
|
| 482 |
+
time_select = parameters.get("time-select")
|
| 483 |
+
route_name = parameters.get("route_name")
|
| 484 |
+
is_has_time = parameters.get("is_has_time")
|
| 485 |
+
|
| 486 |
|
| 487 |
+
if time_select:
|
| 488 |
+
time_select = extra_time_dialogflow(time_select)
|
| 489 |
for time in time_list:
|
| 490 |
if time_select == time["time"]:
|
| 491 |
+
id = int(time["trip_id"])
|
| 492 |
trip = await dialog_service.search_trip_by_id(id, from_time, to_time, route_ids, ticket_count)
|
| 493 |
departure_date = trip["raw_departure_date"]
|
| 494 |
parameters = {
|
|
|
|
| 497 |
"departure_date": departure_date,
|
| 498 |
"trip": trip,
|
| 499 |
}
|
| 500 |
+
text = [f'Quý khách chọn chuyến **{time_select}** | **{route_name}**']
|
| 501 |
return DialogFlowResponseAPI(text=text, parameters=parameters)
|
| 502 |
+
if is_has_time:
|
| 503 |
+
time_list = find_surrounding_times(time_list, time_select)
|
| 504 |
+
parameters = {
|
| 505 |
+
"time_list": time_list,
|
| 506 |
+
"is_valid_time": False
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
return DialogFlowResponseAPI(parameters=parameters)
|
| 510 |
|
| 511 |
parameters = {
|
| 512 |
+
"time_select": None,
|
| 513 |
"is_valid_time": False
|
| 514 |
}
|
| 515 |
text = []
|
app/dialogflow/services/dialog_service.py
CHANGED
|
@@ -67,7 +67,6 @@ class DialogService:
|
|
| 67 |
raise Exception("Error fetching seats data")
|
| 68 |
|
| 69 |
async def search_all_route_ids(self, origin_code: str = None, from_id: int = None, orign_ids: int = None, dest_code: str = None, to_id: str = None, dest_ids: str = None):
|
| 70 |
-
print(from_id)
|
| 71 |
route_ids = await self.search_route_ids_one_way_by_metadata(origin_code, from_id, orign_ids, dest_code, to_id, dest_ids)
|
| 72 |
if len(route_ids) == 0:
|
| 73 |
route_ids = await self.search_route_ids_one_way_by_origin_dest_code(origin_code, dest_code)
|
|
|
|
| 67 |
raise Exception("Error fetching seats data")
|
| 68 |
|
| 69 |
async def search_all_route_ids(self, origin_code: str = None, from_id: int = None, orign_ids: int = None, dest_code: str = None, to_id: str = None, dest_ids: str = None):
|
|
|
|
| 70 |
route_ids = await self.search_route_ids_one_way_by_metadata(origin_code, from_id, orign_ids, dest_code, to_id, dest_ids)
|
| 71 |
if len(route_ids) == 0:
|
| 72 |
route_ids = await self.search_route_ids_one_way_by_origin_dest_code(origin_code, dest_code)
|
utils/format_data_dialog.py
CHANGED
|
@@ -1,17 +1,46 @@
|
|
| 1 |
from datetime import datetime
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
Format time dictionary to string.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
else:
|
| 16 |
hours = int(time["hours"])
|
| 17 |
minutes = int(time["minutes"])
|
|
@@ -36,4 +65,16 @@ def get_weekday_name(date: datetime) -> tuple:
|
|
| 36 |
weekday_ids = date.weekday()
|
| 37 |
weekday = vietnam_weekdays.get(weekday_ids) if vietnam_weekdays.get(weekday_ids) else ""
|
| 38 |
|
| 39 |
-
return date.strftime("%d-%m-%Y"), weekday
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from datetime import datetime
|
| 2 |
+
from bisect import bisect_left
|
| 3 |
|
| 4 |
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def find_surrounding_times(time_list: list, time: str) -> list:
|
| 8 |
+
"""
|
| 9 |
+
Find the surrounding times in a sorted list of times.
|
| 10 |
+
:param time_list: List of time strings in "HH:mm" format.
|
| 11 |
+
:param time: Time string to find the surrounding times for.
|
| 12 |
+
:return: List of surrounding times in "HH:mm" format.
|
| 13 |
+
"""
|
| 14 |
+
index = bisect_left(time_list, time)
|
| 15 |
+
|
| 16 |
+
start = max(0, index - 2)
|
| 17 |
+
end = min(len(time_list), index + 2)
|
| 18 |
+
|
| 19 |
+
return time_list[start:end]
|
| 20 |
+
|
| 21 |
+
def extra_time_dialogflow(time: dict) -> str | list:
|
| 22 |
"""
|
| 23 |
Format time dictionary to string.
|
| 24 |
+
Nếu time có ambiguous (past, future) => trả về list [past_time_str, future_time_str]
|
| 25 |
+
Nếu time rõ ràng => trả về string "HH:mm"
|
| 26 |
"""
|
| 27 |
+
def is_time_ambiguous(time_obj: dict) -> bool:
|
| 28 |
+
return any(time_obj.get(key) for key in ['past', 'future', 'partial'])
|
| 29 |
+
|
| 30 |
+
if is_time_ambiguous(time):
|
| 31 |
+
past_time = time.get("past", {})
|
| 32 |
+
future_time = time.get("future", {})
|
| 33 |
+
|
| 34 |
+
past_hours = int(past_time.get("hours", 0))
|
| 35 |
+
past_minutes = int(past_time.get("minutes", 0))
|
| 36 |
+
past_time_str = f"{past_hours:02d}:{past_minutes:02d}"
|
| 37 |
+
|
| 38 |
+
future_hours = int(future_time.get("hours", 0))
|
| 39 |
+
future_minutes = int(future_time.get("minutes", 0))
|
| 40 |
+
future_time_str = f"{future_hours:02d}:{future_minutes:02d}"
|
| 41 |
+
|
| 42 |
+
return sorted([past_time_str, future_time_str])
|
| 43 |
+
|
| 44 |
else:
|
| 45 |
hours = int(time["hours"])
|
| 46 |
minutes = int(time["minutes"])
|
|
|
|
| 65 |
weekday_ids = date.weekday()
|
| 66 |
weekday = vietnam_weekdays.get(weekday_ids) if vietnam_weekdays.get(weekday_ids) else ""
|
| 67 |
|
| 68 |
+
return date.strftime("%d-%m-%Y"), weekday
|
| 69 |
+
|
| 70 |
+
def get_time_now_json() -> dict:
|
| 71 |
+
"""
|
| 72 |
+
Get the current time in JSON format.
|
| 73 |
+
:return: Dictionary with keys 'hours' and 'minutes', 'second'.
|
| 74 |
+
"""
|
| 75 |
+
now = datetime.now()
|
| 76 |
+
return {
|
| 77 |
+
"hours": now.strftime("%H"),
|
| 78 |
+
"minutes": now.strftime("%M"),
|
| 79 |
+
"seconds": now.strftime("%S")
|
| 80 |
+
}
|